Very new at this


I am very new at this but want to learn more, currently im working on the following code that calculates mean, maximum, minimum, and range
but im having a few errors and need help resolving them and understanding why it didnt work. All help is greatly appreciated.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90


#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};
    
    double sum=0, mean=0, max=0, min=100, range=0, sum1=0, variance=0, std=0;

    //cout<<R[1]<<endl;
    
    

    for(int count=0; count<=(size-1); count++)
    {
        // find the sumation of all the numbers
        sum=sum+R[count];

        // find the maximum
        if (R[count]>max)
            max=R[count];

        // find the minimum
        if (R[count]   min)
            min=R[count];


    }
    mean=
    range=


    // find the variance
    for(int V=0; V<=size-1; V++)
    {

        sum1=sum1+(      )*(      );

    }

    variance=sum1/(    );
    std=sqrt(variance);
    
    
        cout<<"The mean is "<<mean<<endl;
        cout<<"The maximum is "<<max<<endl;
        cout<<"The minimum is "<<min<<endl;
        cout<<"The range is "<<range<<endl;
        cout<<"The variance is "<<variance<<endl;
        cout<<"The standard deviation is "<<std<<endl;

                    

    /*
    double x, y, sum, diff, mul, div;

    cout<<"Hello! Welcome to EGR120."<<endl<<endl;
    cout<<"Today is June 7, 2012."<<endl;
    cout<<"Please enter two numbers"<<endl;

    cin>>x>>y;
    sum=x+y;
    diff=x-y;
    mul=x*y;
    div=x/y;

    cout<<"The sum of "<<x<<" and "<<y<<" is "<<sum<<endl;
    cout<<"The difference of "<<x<<" and "<<y<<" is "<<diff<<endl;
    cout<<"The product of "<<x<<" and "<<y<<" is "<<mul<<endl;
    cout<<"The division of "<<x<<" and "<<y<<" is "<<div<<endl;

    */
        
    system("pause");

}


First off, welcome to the forums and you picked a great language to start with. However, one of the biggest things with C++ is understanding all of the syntax being able to debug your errors. But no worries, I'll help you out with this one. I'll start with the lines that are not commented.

Trying to compile it, I get these errors:
Code Blocks Compiler wrote:
C:\...\main.cpp||In function 'int main()':|
C:\...\main.cpp|35|error: expected ')' before 'min'|
C:\...\main.cpp|45|error: expected primary-expression before 'for'|
C:\...\main.cpp|45|error: expected ';' before 'for'|
C:\...\main.cpp|45|error: 'V' was not declared in this scope|
C:\...\main.cpp|45|error: expected ';' before ')' token|
C:\...\main.cpp|52|error: expected primary-expression before ')' token|
C:\...\main.cpp|85|error: 'system' was not declared in this scope|
||=== Build finished: 7 errors, 0 warnings ===|


The first error is on line 35. if (R[count] min) You're just simply missing your comparison operator, <, >, ==, or !=.

The second through the fifth error are all on lines 45. But there is two issues for this for statement. Lines 40 and 41 don't do anything and cause errors.
1
2
    mean=
    range=


Commenting those lines out gives me errors at line 48, you have a lot of spaces between the parenthesis, but no variables inserted in between.

If code is unfinished, it's best to comment them out until you can figure out what needs to be inserted to complete it.

We have this same issue on line 52, there seems to be a missing variable in the parenthesis in all of these cases.

And lastly, I got an error with the system("pause"); statement which you may or may not get. IF you get this error, simple include the line at the beginning of your code#include <stdlib.h> with the rest of your preprocessor directives, i.e. #include statements.

Hope this helps get you pointed into the right direction.
No sure if the formulas are correct, but it should be something close.

Mike

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

int main()
{
	double sum=0, max, min, var=0, ave, std_dev;
	int i=0;
	double arr[]={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};


	for(i=0;i<SIZE;i++)//gets the sum
		sum+=arr[i];
	cout<<"Sum is: "<<sum<<endl;
	ave=sum/SIZE;
	cout<<"Average: "<<ave<<endl;

	for(i=0;i<SIZE;i++){//variance
		var=var+pow((arr[i]-ave),2);
	}
	cout<<"Variance is: "<<var/SIZE<<endl;
	max=arr[0];
	for(int i=0;i<SIZE;i++){
		if(max<arr[i+1])
			max=arr[i+1];
		
	}
	std_dev=sqrt(var);
	cout<<"Standard deviation is: "<<std_dev<<endl;

	cout<<"MAX is: "<<max<<endl;
	min=arr[0];
	for(int i=0;i<SIZE;i++){
		if(min>arr[i])
			min=arr[i];
	}
	cout<<"MIN is: "<<min<<endl;


	_getch();
	return 0;
}	
I think I've seen this before. It's a fill-in-the-blanks C++ homework assignment from somewhere.
Now that you mention it, I have seen this once or twice before. Not specifically this one, but I just helped a friend with their assignment the other day.

These are the easy ones too...

Thanks guys.

Still working on it.

I want to have the program calculate the mean and average of the set of numbers.

Im not sure how to code it which is why its basically blank.
Mean and average are the same exact thing. All you do is add all the numbers together and then divide...simple.

I meant variance.

I know its you have to take the distance each number is from the mean.
I've honestly never heard of the term variance, but rather standard deviation. But a simple google search gave me this.

http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance

I'll look into it.
Thanks
Ok,
so far i've gotten this.

Still have some errors but got rid of a few.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

#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};
    
    double sum=0, mean=0, max=0, min=100, range=0, sum1=0, variance=0, std=0;

    //cout<<R[1]<<endl;
    
    

    for(int count=0; count<=(size-1); count++)
    {
        // find the sumation of all the numbers
        sum=sum+R[count];

        // find the maximum
        if (R[count]>max)
            max=R[count];

        // find the minimum
        if (R[count] < min)
            min=R[count];


		// find the mean of all the numbers
        mean=sum/R[count];

		// find the range
        range=max-min;


		// find the variance
		variance=range-mean/R[count];
	}

    for(int V=0; V<=size-1; V++)
    {

        sum1=sum1+(      )*(      );

    }

    variance=sum1/(    );
    std=sqrt(variance);
    
    
        cout<<"The mean is "<<mean<<endl;
        cout<<"The maximum is "<<max<<endl;
        cout<<"The minimum is "<<min<<endl;
        cout<<"The range is "<<range<<endl;
        cout<<"The variance is "<<variance<<endl;
        cout<<"The standard deviation is "<<std<<endl;

                    

    /*
    double x, y, sum, diff, mul, div;

    cout<<"Hello! Welcome ."<<endl<<endl;
    cout<<"Today is June 12, 2012."<<endl;
    cout<<"Please enter two numbers"<<endl;

    cin>>x>>y;
    sum=x+y;
    diff=x-y;
    mul=x*y;
    div=x/y;

    cout<<"The sum of "<<x<<" and "<<y<<" is "<<sum<<endl;
    cout<<"The difference of "<<x<<" and "<<y<<" is "<<diff<<endl;
    cout<<"The product of "<<x<<" and "<<y<<" is "<<mul<<endl;
    cout<<"The division of "<<x<<" and "<<y<<" is "<<div<<endl;
	
    */
        
    system("pause");

}


Still trying to figure out what to put in the ( ) blanks
Topic archived. No new replies allowed.