Arrays to calculate grade info

Hi,

For some reason why my program is failing to build and I'm not able to rectify the issue. Would appreciate if someone can help me.

This program helps to calculate the mean, median and sort grades.

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
93
94
95
96
97
98
99
100
101
102
103
104

#include <iostream>
#include <iomanip>
using namespace std;
void bubbleSort (int v[],int n);
double calcMean (int v[],int n);
double calcMedian (int v[],int n);

int main () 
{
    const int size =10;
	int array[size];
	int i;
	for (i = 0; i < size; i++)
	{
        //ask use for input
        cout << "\nPlease Enter grade " << i+1 << " : ";
        cin >> array[i];
        //make sure that user enters a vlue between 0 and 100
        while(array[i] < 0 || array[i] >100)
        {
            cout << "Wrong input. Please enter a value between 0 and 100 only." << endl;
            cout << "\nPlease Reenter grade " << i+1 << " : ";
            cin >> array[i];
        }
    }
    
    //call function to calculate mean
	double mean = calcMean (array, size);
    cout << std::fixed << "\n\nThe mean of all the elements is : " << setprecision(2) << mean << endl;
    
    //call function to sort the array
	bubbleSort(array, size);
	//output the sorted array with 5 grades per line.
	cout << "\nThe sorted array is : " << endl;
	for (i = 0; i < size; i++)
    {	
        //put a line break after displaying 5 grades
	    if(i%5==0)
            cout << "\n";
        cout << array[i] << '\t';
     }
	
   //call function to calculate median
	double median = calcMedian (array, size);
    cout << "\n\nThe median of all the elements is : " << setprecision(2)	<< median << endl;
    
	system("PAUSE");
	return 0;
}

//function to calculate the mean of the array
double calcMean (int v[],int n)
{
       int total =0;
       //calculate the sum of all elements in the array
       for(int i =0 ; i<n; i++) 
               total = total + v[i];
       //calculate the mean of all the elements in the array
       double average = total/n;
       return average;
}

//function to calculate the median of the array
double calcMedian (int v[],int n)
{
       double median;

       //check if array size is even
       if(n%2==0)
       {   int middle = n/2;
           median = v[middle];
       }
       //else if array size is odd
       { 
              //there are two middle elements in array
              int middle1 = n/2;
              int middle2 = (n/2)-1;
              //find average of the two middle elements
              median = (v[middle1] + v[middle2])/2;
       }      
       return median;
}

//function to sort the array
void bubbleSort (int v[],int n)
// exchange the values in the array so that they appear in ascending order
{

	for (int i =0 ; i<n; i++) 
    {
		for (int j = 0; j < n-i-1; j++)
        {	
            // if out of order
			if (v[j] > v[j+1]) 
            {
				// then swap
				int temp = v[j];
				v[j] = v[j + 1];
				v[j + 1] = temp;
			}
		}
	}
}



Regards
@IU1

I was able to compile and run your program, with no problems, whatsoever. I am using Microsoft Visual C++ 2008 Express. What compiler are you using, and what errors do you get?
Hi, I'm using MS Visual C++ 2010 Express.

It gives me the following errors:

lab.cpp(61): error C2059: syntax error : '>'
lab.cpp(61): error C2143: syntax error : missing ';' before ')'
lab.cpp(61): error C2143: syntax error : missing ';' before ')'
lab.cpp(62): error C2143: syntax error : missing ';' before '{'
Last edited on
Hi, I started afresh in a completely new file and the errors listed above are gone.

Now the only error I get is:

lab.cpp(57): error C2065: 'i' : undeclared identifier
Not sure why I'm getting that? and not for the line above that as well
@IU1, I'm at an understanding loss here. I took your program as is, and was able to compile and run it, even using Dev-C++, after commenting out system("PAUSE"); ( Dev-C++ didn't understand it ) so I don't understand why it doesn't work for you, also.
Hi whitenite1, it says i is undeclared - that's weird since it is infact declared.


Thanks for all your help :) appreciate it.
One thing I learned about the Visual Studio software is that it has really unexpected behavior when it comes to opening and closing files and projects, and whatever else it likes to complain about. I ran your code and minus the #include for <stdlib.h> everything ran perfectly.

The only solutions I've seen before was to restart your IDE and see if that remedies your issue, maybe start a new project and copy paste your code into that main.cpp.

MSVS is very picky and is horrible with it's error codes. I suggest using caution when opening and closing projects and leaving the IDE running for a long period of time.
Topic archived. No new replies allowed.