no arrays allowed help for standard deviation

Hello,

Totally new at this and im not sure how to re build my code without the use of arrays or vectors?

how do i properly add a cin.clear or cin.seekg(0) to have my while loop restart and perform the standard deviation calculation.


per Teacher


You are not allowed to write your own functions and, even if you know about them, you are not allowed to
use arrays. You may assume the file contains at least two values and only consists of floating-point
values, whitespace, newline characters, and the end of file character.
As you are not allowed to use arrays, you need to read all the values once to calculate the average then
use that average along with each value to calculate the standard deviation


here is the test data.

3.58 62
44 17
2.68 3.44 8.01
6.22
83
7



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

int count = 0;
double data;
double sum = 0;

double sum4 = 0;
double num[100];
int i = 0;



cout<<"***Standard deviation calculation*** "<<endl;


while(cin>>data)
{

++count;
sum = sum + data; // this will sum all the numbers for the mean
cout<< "Value"<<" #"<<count<<": "<<data<<endl;

num[i] = data;
i++;



}


double mean= sum / count; // this will calculate the mean

cout<<"The Mean found is: "<< mean <<endl;

double newnum; // this will hold the numbers to be able to minus the mean

for (int k = 0; k < count; k++)
{

newnum = pow((num[k] - mean), 2) ;
sum4 = sum4 + newnum; // this sum the deviation.

}


double SD= sqrt(sum4/count);

cout<<"Sample standard deviation : "<< SD <<endl;



return 0;

}
Edit & Run
Hello if your having difficulty why not check out my course I teach C++ for beginners all the way up to more complicated concepts: https://www.udemy.com/learn-cpp-from-scratch/?couponCode=CPPFORUM
Read from file, not std::cin.
You don't have to make a second pass to compute the standard deviation. All you must do is compute the sum(xi2) as well as the sum(xi) in the first pass. After that it's just algebra:
sum((xi - xbar)2)
= sum(xi2 - 2*xi*xbar + xbar2)
= sum(xi2) - 2*xbar*sum(xi) + N*xbar2
Topic archived. No new replies allowed.