No Output on screen

Hello there, How do i get my program to display the average and computed standard deviation as written in the code.

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
#include <iostream>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;
// Program reads input from a file with numbers of type double
// displays their average and standard deviation  on the screen.
int main()
{
    ifstream fin;
    fin.open("inputData.dat");
    ofstream fout;
    fout.open("outputData.dat");

    double next, sum = 0 ;
    int count = 0, avg = 0, stdDev = 0, variance;

    while(fin >> next)
    {
        cout<<setw(6)<<next<<endl;

    }
    while (fin >> next )
    {

        fin>>next;
        sum = sum + next;
        count ++;
        avg =  sum / count ;
        variance = pow((avg - next),2);
        stdDev = sqrt(variance);
        cout<<" The Average of the numbers is "<<avg<<endl;
        cout<<" whilst their standard deviation is "<<stdDev<<endl;
    }

    fin.close();
    fout.close();

    return 0;

}
Posting the same question within 20 minutes of each other will not yield faster replies. It is a waste of other people's time. Don't do it.

1
2
3
4
5
    while(fin >> next)
    {
        cout<<setw(6)<<next<<endl;

    }

This will read everything in inputData.dat, so when you do
1
2
3
4
5
6
7
8
9
10
11
12
    while (fin >> next )
    {

        fin>>next;
        sum = sum + next;
        count ++;
        avg =  sum / count ;
        variance = pow((avg - next),2);
        stdDev = sqrt(variance);
        cout<<" The Average of the numbers is "<<avg<<endl;
        cout<<" whilst their standard deviation is "<<stdDev<<endl;
    }

there will be nothing to read, as you've already read everything.
Your logic is also incorrect here.
First, you want to read all the numbers in the file to a vector.
Then you loop through all the values in that vector to calculate the average.
Then you loop through the vector again, calculate the squared differences from the average, of each number. This is your variance.
Your standard deviation calculation is correct, however.

Reading and storing the numbers in a vector.
1
2
3
4
5
vector<double> numbers;     // to store all the numbers in inputData.dat
while(fin >> next)
{
    numbers.push_back(next);    // insert next into the vector
}


Looping through all the numbers in the vector.
numbers.size() returns the number of elements (what your count variable is used for, but now it's unnecessary) in numbers.
1
2
3
4
5
for(int i = 0; i < numbers.size(); i++)
{
    sum += numbers[i];
}
average = sum / numbers.size();


A less verbose and clearer way of looping through the vector would be to use a range-based for loop.
1
2
3
4
5
// for every d in numbers
for( double d : numbers )
{
    sum += d;
}


Now you can apply the same method and logic to calculate the variance.
Last edited on
Thank you for your prompt response and help. Housekeeping issues noted.

I have incorporated the additions in the code with the following errors ;

1) "request for member 'size' in 'numbers', which is of non class type 'int'

2) error: expected primary-expression before 'double'

3) error : request for member 'push_back' in 'numbers' which is of non class type ' int'

4) vector was not declared in this scope
Could you post your code? Make sure you've included the vector header.
Ok, here it is , i have included #include<initializer_list> for the vector header, please confirm if this is correct ? Thank you...

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
61
62
63

#include <iostream>
#include<cstdlib>
#include<fstream>
#include<iomanip>
#include<cmath>
#include<initializer_list>
using namespace std;
// Program reads input from a file with numbers of type double
// sums them up, computes and displays their average and standard deviation
// on the screen
int main()
{
    ifstream fin;
    fin.open("inputData.dat");
    ofstream fout;
    fout.open("outputData.dat");

    double next, sum = 0 ;
    int i, numbers , avg = 0, stdDev = 0,  variance;


for( i = 0; i < numbers.size(); i++)
{
    sum += numbers[i];
}

avg = sum / numbers.size();

    while(fin >> next)
    {
        cout<<setw(6)<<next<<endl;

    }
    while (fin >> next )
    {

        fin>>next;
        sum = sum + next;
       // count ++;
        avg =  sum / numbers.size() ;
        variance = pow((avg - next),2);
        stdDev = sqrt(variance);
        cout<<" The Average of the numbers is "<<avg<<endl;
        cout<<" whilst their standard deviation is "<<stdDev<<endl;
    }
vector <double> numbers;     // to store all the numbers in inputData.dat
    while(fin >> next)
{
    numbers.push_back(next);    // insert next into the vector
}
for( i = 0; i < numbers.size(); i++)
{
    sum += numbers[i];
}
average = sum / numbers.size();
    fin.close();
    fout.close();

    return 0;

}
Ok, here it is , i have included #include<initializer_list> for the vector header, please confirm if this is correct ?

It's #include <vector> for the vector header.

Also, you don't need to explicitly close your files on line 57 and 58. This is automatically handled by C++.
Topic archived. No new replies allowed.