Iostream and Cmath

It shows an error in the "range ;" in which I assume to enter in a number.
But it also shows an error "sum1=sum1+ ( )*( );"

Yes, I'm a newbie. Yes, I'm probably incredibly wrong and do not even know what I am asking. And yes, I only skimmed the 'beginners read this first' topic. But I'm pretty desperate, and HAVE put a lot of effort to finding out why this program doesn't work.
I am a train wreck and in danger of failing.
If you can, please, help...
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
64
65
66
67
68
69
#include<iostream>
#include<cmath>

using namespace std;



int main()
{
    const int size=56;
    double R[size]={45.3, 67.8, 34.3, 51.2, 48.5, 61.3, 59.3, 65.1,
                                49.3, 42.4, 63.5, 69.8, 71.2, 39.8, 55.5, 53.2,
                                56.7, 48.8, 61.5, 51.2, 58.9, 63.1, 67.5, 62.4,
                                52.4, 50.2, 49.8, 56.8, 59.7, 60.4, 45.8, 43.8,
                                51.3, 54.8, 55.1, 52.3, 56.2, 59.7, 63.0, 46.7,
                                63.1, 58.2, 41.9, 59.2, 57.2, 67.3, 68.2, 38.9,
                                51.3, 63.8, 53.4, 58.9, 56.3, 58.9, 53.2, 56.8},
                                sum=0, mean, max=0, min=100, range, sum1, variance, std;

    // Find average
    for(int count=0; count<=size-1; count++)
    {
        sum=sum+R[count];
        //cout<<sum<<" ";

        //Find  Maximum
        if (R[count]> max)
        {
            max=R[count];
        }

        //Find  Minimum



    }
    mean=sum/size;
    range=  ;
     

    // Find the variance and standard devisation

    for (int i=0; i<=size-1; i++)
    {
        sum1=sum1+(        )*(       );
        
    }

    variance=   
    std=sqrt(variance);





    cout<<"The mean is "<<mean<<endl;
    cout<<"The maximum is "<<max<<endl;


    
    
    
    
    //cout<<"R[55]= "<<R[size-1]<<endl;
    

    system("pause");
    return 0;
}
Line 49 is incomplete... you haven't assigned any value to variance... and what exactly are you trying in line 45? What are those empty brackets?
We could help you better, if you wouldn't post incomplete pieces of code... it really doesn't help us serve you at all.
Finding the maximum: You have the correct algorithm in your if statement ( lines 27-30 ). The problem is with your initialization of max which you have arbitrarily initialized to 0. What would happen if all the values in the array were negative? All the comparisons would be false so max would still be zero which would not be correct. You need to initialize max to the first element of the array immediately before the for loop on line 21, i.e.,
max = R[ 0 ];
You can do the same type of thing to find the minimum. Once you have found the maximum and minimum the range on line 38 should be easy to calculate.


The formula for calculating the variance is:

variance = ∑(xi - m )2 / N where m is the mean and N is the number of items.

You have the right idea that you need to square something on line 45

Line 45: sum1 = sum1 + ( R[count] - mean ) * ( R[count] - mean );

Line 49: variance = sum1 / size;

Also you need to initialize sum1 to zero before the loop.
Last edited on
Topic archived. No new replies allowed.