Problem with Array

How can I use the numberStudents value as the limit for scores?

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
  cout << "Enter the number of students: ";
	int numberStudents = 0;
    cin >> numberStudents;

	double scores[] = {0};

	cout << "Enter " << numberStudents <<  " scores: ";
	for (int i = 0; i < numberStudents ; i++)
	{
		cin >> scores[i];
	
	}

	double best = scores[0];

	for (int i = 1; i < numberStudents ; i++)
	{
		if (scores[i] > best) best = scores[i];
	}

	for (int i = 0; i < numberStudents ; i++)
	{
		if (scores[i] >= best - 10)
			cout << "Student " << i << " is " << scores[i] << " and grade is A";
		else if (scores[i] >= best - 20)
			cout << "Student " << i << " is " << scores[i] << " and grade is B";
		else if (scores[i] >= best - 30)
			cout << "Student " << i << " is " << scores[i] << " and grade is C";
		else if (scores[i] >=  best - 40)
			cout << "Student " << i << " is " << scores[i] << " and grade is D";
		else
			cout << "Student " << i << " is " << scores[i] << " and grade is F";
		cout<< endl;
	}


Last edited on
double * scores = new double[numberStudents];


...

delete [] scores;
return 0;
}
Here is a well-written tutorial that explains what jonnin is suggesting:

http://www.cplusplus.com/doc/tutorial/dynamic/
Last edited on
How can you do it w/o dynamic?
double * scores = new double[numberStudents] is dynamic.
you can't.

arrays cannot be assigned a variable unless using a nonstandard compiler that supports this extension, and in that case, it is no longer really c++.

ye olde workaround for this is to do aggravating things like

double scores[1000000000000000000000000000]; //this is an exaggeration for humor.

making it challenging for the user to exceed your limitations.
Last edited on
Topic archived. No new replies allowed.