Can't get the right output..

Trying to calculate mean, standard deviation, & variance through data structures.

Output:

Mean:                -9.25596e+061
Standard Deviation:   2.2836e+046
Variance:             5.21481e+092

Code:

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

struct StatData
{
    double Data[10];
    double mean;
    double stdDev;
    double variance;
};

int main()
{
    const int SIZE=10;
    double sum=0;
    double stdDevSum=0;
    double temp=0;
    int i=0;
    int j=0;

    StatData calc;
    calc.Data[SIZE] = (24,21,24,43,31,41,42,34,41,45);

    for(i=0;i<SIZE;i++)
        sum=calc.Data[i]+sum;

    calc.mean = sum/SIZE;

    for(j=0;j<SIZE;j++)
        stdDevSum = pow((calc.mean - calc.Data[j]),2) + stdDevSum;

    temp = stdDevSum/SIZE;

    calc.stdDev = sqrt (temp);
    
    calc.variance = temp;

    cout << "Mean:                " << calc.mean << endl;
    cout << "Standard Deviation:   " << calc.stdDev << endl;
    cout << "Variance:             " << calc.variance << endl;

}

Recommended Answers

All 7 Replies

why not make functions and keep main() clean?
that way you will avoid many troubles

It's part of the assignment.

Well done!! I don't think I have seen this error before!!

What you have done is this: calc.Data[SIZE]=(24,21,43,24,43,31,41,42,34,41,45); That is your error... You have use round bracket e.g. ( ). That is not what you wanted. So let us see what happens.

You first evaluate the contents of the bracket. e.g. 24,etc. using the comma operator, that simple means that it evaluates to 45. Then you set calc.Data to 45. But since that is over the limit of the array boundary, [recall, arrays are indexed from 0 to SIZE-1] you in effect have a completely uninitialized array, and because of your structure, you set mean to be 45.

However, if it was a simple array you, could write this double arrayData[]={4,5,6,7,8}; But this is not allowed for the struct. Therefore, the quickest thing to do is to change the first loop to this:

const double arrayData[]={4,6,7,8,9,91};
for(int i=0;i<SIZE;i++)
{ 
   calc.Data[i]=arrayData[i];
   sum+=calc.Data[i];
}

I'm kind of confused, can you post the whole code so I don't do this wrong.

And by the way, I need those 10 numbers specifically for this problem... I'm confused where you got {4,6,7,8,9,91} from.

Never mind, got it!

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

struct StatData
{
    double Data[10];
    double mean;
    double stdDev;
    double variance;
};

int main()
{
    const int SIZE=10;
    double sum=0;
    double stdDevSum=0;
    double temp=0;
    int numCount = 0;
    int i=0;
    int j=0;

    StatData calc;
    calc.Data[SIZE];

    cout << "This program calculates the mean, standard deviation," 
        "and variance of " << SIZE << " numbers.";
    cout << "Enter " << SIZE << " numbers." << endl;

    while(numCount < SIZE)
    {
         cin >> calc.Data[numCount];
         numCount++;
    }

    for(i=0;i<SIZE;i++)
    {
        sum=calc.Data[i]+sum;
    }

    calc.mean = sum/SIZE;

    for(j=0;j<SIZE;j++)
    {
        stdDevSum = (pow((calc.Data[j] - calc.mean),2)) + stdDevSum;
    }

    temp = stdDevSum/SIZE;
    calc.stdDev = sqrt (temp);
    calc.variance = temp;

    cout << "Mean:                 " << calc.mean << endl;
    cout << "Standard Deviation:   " << calc.stdDev << endl;
    cout << "Variance:             " << calc.variance << endl;
}

Ok, sorry, I got 4,5,6 etc from being lazy and not wanting to type.

If you want to save yourself the typing ...
From line 29 to 36 of your second version of the code, you could replace it with this:

const double arrayData[]={24,21,43,24,43,31,41,42,34,41,45};

for(int i=0;i<SIZE;i++)
  calc.Data[i]=arrayData[i];

Note, that line 25 of your code has no purpose and should be deleted.

Otherwize a good effort.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>

using namespace std;

struct StatData
{
    double Data[10];
    double mean;
    double stdDev;
    double variance;
};

int main()
{
    double sum=0,mean=0;
    double stdDevSum=0;
    double temp=0;
    int i=0;
    int j=0;
    int Data[] = {24,21,24,43,31,41,42,34,41,45};

    for(i=0;i<10;i++)
        sum=Data[i]+sum;

    mean = sum/10;
cout << mean;

getch();
}

it works may be math function does not work
I saw that

calc.Data[SIZE] = (24,21,24,43,31,41,42,34,41,45);

this should be with this bracket {.....}
may be

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.