Error in Array

Enter score of 200 students.Each score is associated with and index number in the list.
Calculate and display the average score, highest score and lowest score in the screen as well as the index number in the name list corresponding to the highest score and lowest score respectively.

My ERROR : The min value was not right. its show something like 8.00+e....


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
  #include <iostream>
#include  <cmath>
using namespace std;
int main()
{
	const int students=300;
	int a[students];
	double sum=0,average=0;
	double max,min;
	int i,n;
	int count=0;

	cout<<"How many students you want to enter ? [200:MAXIMUM] : ";
	cin>>n;
	for(i=1; i<=n; i++)
	{
		cout<<"Score #"<<i<<" : ";
		cin>>a[i];
	}

     max=a[0];
     min=a[0];

	for(i=1; i<=n; i++)
	{
	    sum+=a[i];
	    average=sum/i;

		if(min>a[i])
		{
			min=a[i];
		}
		else if(max<a[i])
		{
			max=a[i];
		}
	}
	cout<<endl;
	cout<<"The average score of "<<i-1<<" is "<<average<<endl;
	cout<<"The highest score of "<<i-1<<" is "<<max<<endl;
	cout<<"The lowest score of "<<i-1<<" is "<<min<<endl;

	system("Pause");
	return 0;
}
Last edited on
A few things stand out. First, you set max and min to the first element of the array a[0] which is correct. However, thoughout the code you don't use the first element at all, hence in this context max and min contain garbage.

I'd advise avoiding declaring uninitialised variables such as max, min and i. Wait until you have an actual value, then declare the variables. Also limit the scope of variables as much as possible to where they are needed, declare i within the for loop. Also the average should be calculated just once at the end, not inside the loop.

change this:
1
2
3
4
5
    for(i=1; i<=n; i++)
    {
        cout<<"Score #"<<i<<" : ";
        cin>>a[i];
    }
to
1
2
3
4
5
    for (int i = 0; i<n; i++)    // loop starts from 0, ends at n-1
    {
        cout << "Score #" << i+1 << " : ";
        cin >> a[i];
    }

Similarly the second loop should start from 0.

After the second loop ends, remember to use n, not i-1
1
2
3
4
5
6
	double average = sum / n;  // declare average, calculate result.
	
	cout << endl;
	cout << "The average score of " << n << " is " << average << endl;
	cout << "The highest score of " << n << " is " << max     << endl;
	cout << "The lowest score of  " << n << " is " << min     << endl;


Last edited on
@chervil thanks for the reply. i followed your steps. And the min stiil give me error. Can i know how to i avoid declaring uninitialised variables ?
Perhaps my explanation wasn't clear, or something was missed. Could you show the latest version of your code which you now have.
@chervil thanks for the reply once again. Here my updated 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
44
45
#include <iostream>
#include  <cmath>
using namespace std;
int main()
{
	const int students=300;
	int a[students];
	double sum=0,average=0;
	double max,min;
	int i,n;
	int count=0;

	cout<<"How many students you want to enter ? [200:MAXIMUM] : ";
	cin>>n;
	for(i=1; i<=n; i++)
	{
		cout<<"Score #"<<i<<" : ";
		cin>>a[i];
	}

	 max=a[0];
     min=a[0];

	for(i=1; i<=n; i++)
	{
		sum+=a[i];

		if(min>a[i])
		{
			min=a[i];
		}
		else if(max<a[i])
		{
			max=a[i];
		}
	}
	average=sum/n;
	cout<<endl;
	cout<<"The average score of "<<n<<" is "<<average<<endl;
	cout<<"The highest score of "<<n<<" is "<<max<<endl;
	cout<<"The lowest score of "<<n<<" is "<<min<<endl;

	system("Pause");
	return 0;
}
Thanks for that.

Whatever I said before obviously was not clear.

Array subscripts start from 0. That's why both max and min are set to the first array element a[0];

But it also means that when reading in the data from the user, you should do this, starting from zero:
1
2
3
4
5
6
7
    cout << "How many students you want to enter ? [" << students << ":MAXIMUM] : ";
    cin >> n;
    for (int i=0; i<n; i++)
    {
        cout << "Score #" << i+1 << " : ";
        cin >> a[i];
    }

Note the i<=n is changed to i<n as well. It does mean that in order to display in human-friendly form, the value i+1 is displayed in the cout statement.

Incidentally, I changed the 200 to students in the information prompt at the start.

how to i avoid declaring uninitialised variables ?

Don't declare a variable until you are ready to assign a value to it. Rather than declaring max, min and i somewhere near the start of the code, don't declare until point of use.
1
2
    int max=a[0];
    int min=a[0];


The same applies to average. Rather than declare it at the start, wait till you actually need to use it.
 
    double average = sum/n;


At line 24:
 
    for(i=1; i<=n; i++)
You again need to change i<=n to i<n
It would be ok to start the loop from i=0, but since you already stored those as the current max and min, in this case it is ok to start counting from the second element.
 
    for(int i=1; i<n; i++)
Notice also I declared the variable i within the for loop. It's a good idea to limit the scope of variables as narrowly as possible, in order to avoid unexpected side-effects elsewhere.

Only n and sum need to be declared before being used (which you have done correctly).

I hope this is a bit clearer (maybe I should just post the complete code, but it seemed to me these were just a few small changes here and there).

@Chervil. Thank you very much! that was really a good explanation and advice. I fix the error following your steps. Thank you !
Topic archived. No new replies allowed.