Help with Array please!

Create a program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. If the user enters an invalid number, reject it and ask again. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category by using a loop and a parallel array (for the grade names).


#include <iostream>
using namespace std;
int main()
{
int grade;
cout << "Input your grade (0-4): ";
cin >> grade;
cout << endl;
if (grade == 4){

cout << "Letter grade: A" << endl;
}
else if (grade ==3){
cout << "Letter grade: B" << endl << endl;
}
else if (grade ==2 ){
cout << "Letter grade: C" << endl << endl;
}
else if (grade ==1){
cout << "Letter grade: D" << endl << endl;
}
else if (grade ==0) {
cout << "Letter grade: F" << endl << endl;
}
else {
cout << "Invalid grade!" << endl;
}
system("pause");
return 0;
}

This is as far as i can figure out. I know it is lacking a lot but i am stumped. Please help i am the epitome of a N00B (new to coding). Thank you in advance!
Create a program that will accept any number of grades for an exam.... After all grades have been entered, allow the user to enter -1 to exit.


This tells me I need to create a loop to allow the user to input an undetermined number of grades until they input a sentinel value (-1). Since I don't know how many times I need to run the loop, I would use a while or do while loop, not a for loop.

The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. If the user enters an invalid number, reject it and ask again.


Once a user inputs a value for grade, I want to check if it's a valid input, >=-1 or <=4. If it's not valid, I want to re-prompt them. I might use a nested while loop to keep ask them to re-input.

Output the number of grades in each category by using a loop and a parallel array (for the grade names).


This tells me as the user enter grades, I need to keep track of how many of each grade type they're entering. It sounds like they want you to use two arrays, one to keep a count of each grade type and then one to hold the text that corresponds to the respective grade count.

1
2
3
4
5
6
7
8
9
//entry
prompt user to enter grade
while sentinel has not been entered
-- check for valid input value
-- while input valid is not valid, prompt user to re-enter grade
-- if grade entered, increment appropriate index of grade count array
-- ask user to enter another grade or -1 to quit
//output
-- for each index of the array, output the grade count and the corresponding grade text
Last edited on
How would i go about doing that? i keep referring to my text book but i cant figure out how.

i think i need to include? *int SENTINEL = -1;*

and for the While loop

int grade,numGrade;
numGrade=0
cout << "Input your grade (0-4): ";
cin >> grade;
cout << endl;

while(grade != -1) // the sentinel ?

i am a bit lost..
Last edited on
Which step are you having trouble with?
creating a sentinel controlled while loop is what im struggling with right now. The array concept is also confusing me
Study this. I added comments to help in the understanding. This is not the most elegant code I've ever written but I tried to not confuse you with C++ language features you haven't learned yet.

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
#include <iostream>
using namespace std;
int main()
{
	// create an array of numbers
	int nNumberOfGrades [6] = {0, 0, 0, 0, 0, 0};
        // I chose the size of 6 because that is the amount
	// of grade types you are calculating  A B C D E F
	// six different possible values to store
	// I intialized the array to 0 you could also use a loop to do this 
        // or another method

	int input = 1; // the input varible

	// creating an infinate loop that will only exit should the input -1 be entered
	for ( ; ; )
	{

		cout << "Enter the Grade as a numeral " << endl;
		cout << "0 = A\n"
			<< "1 = B\n"
			<< "2 = C\n"
			<< "3 = D\n"
			<< "4 = E\n"
			<< "5 = F\n"
			<< "Enter -1 when finished" << endl << endl;

		cin >> input;
		cout << endl;

		// this is the statement that checks for a -1 value and if true
		// the break statement will throw us out of loop
		if (input == -1)
		{
			break;
		}
		
		// if the value is withing the correct values of a grade
		// increment the number stored in the correct grade array
		if (input > -1 && input < 6)
		{
			nNumberOfGrades [input]++;
		}
		// or inform user of mistake and let the loop continue
		else
		{
			cout << "Invalid number. Try Again" << endl << endl;
		}
	}
	
	int grade;

	// another loop that loops through possible grades and displays the 
        // number of grades incremented
	// in the respective grade slot
	for (grade = 0; grade < 6; grade++)
	{
		if (grade == 0)
		{
			cout << "Number of grades : " << nNumberOfGrades [grade] << " Letter grade: A" << endl << endl;
		}
		else if (grade == 1)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: B" << endl << endl;
		}
		else if (grade == 2)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: C" << endl << endl;
		}
		else if (grade == 3)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: D" << endl << endl;
		}
		else if (grade == 4)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: E" << endl << endl;
		}
		else if (grade == 5)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: F" << endl << endl;
		}

	}
	system("pause");
	return 0;
}
Last edited on
Right, you can do something like this to run a while loop

1
2
3
4
5
6
7
8
	cout << "enter value (enter sentinel to quit): ";
	cin >> value;	
	while (value!=sentinel)
	{
		//do stuff
		cout << "enter another value (enter sentinel to quit): ";
		cin >> value;
	}



Arrays are a collection of data where you access an individual element in the array by its index number. Array indices start at 0 and run up to one less than the size of the array. So in the case of the grades, this works out well because the grade values of 0,1,2,3,4 correspond to the index values of a 5 element array. So if they enter 4, the grade variable will equal 4 and you can use MyArray[grade] to access and increment the array value at index 4.


Edit:
@CodeGoggles - there is no grade E :)


The instructor asks for parallel arrays - so when it comes to the output, there would be a second array holding the text grade values.
Last edited on
That is the easiest part of the problem. Here is the method I use (corny, but my preference):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

const int SENTINEL = -999;  

int main()
{
      std::cout << "Enter grade (0 - 4) or -999 to quit: ";
      int grade;
      std::cin >> grade;

      while(grade != SENTINEL)
      {
            // whatever I was needing done 
      }
      
      // polite good-bye for exiting program if I feel like it 
      return 0;
}


As for arrays, think of them as nothing more than a list (it can be a list of numbers (int, double, float), list of names (sting), or a list of letters (char)). Parallel arrays are basically two arrays (lists) where array1[index] and array2[index] are for the same thing. So, using the first post, you could have two arrays char grades[5] = {'A', 'B', 'C', 'D', 'F'}; and int gradeCount[5];. You would then keep track of each time a grade was given and increment the value in gradeCount[index] the just make a for loop to loop through both arrays and print them out.

Sorry, I'm trying to explain this while being distracted.
Thank You! Codegoggles now i see the structure i was missing as well as the sentinel.

Thank You! Wildblue you helped show me how to break down the program and with the structure.
Ok I just read that you need to do it with while loops this is an edited version
using while loops.


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
#include <iostream>
using namespace std;
int main()
{
	// create an array of numbers
	int nNumberOfGrades [6] = {0, 0, 0, 0, 0, 0};
        // I chose the size of 6 because that is the amount
	// of grade types you are calculating  A B C D E F
	// six different possible values to store
	// I intialized the array to 0 you could also use a loop to do this 
        // or another method

	int input = 1; // the input varible

	// creating an infinite loop that will only exit should the input -1 be entered
	while ( 1 )
	{

		cout << "Enter the Grade as a numeral " << endl;
		cout << "0 = A\n"
			<< "1 = B\n"
			<< "2 = C\n"
			<< "3 = D\n"
			<< "4 = E\n"
			<< "5 = F\n"
			<< "Enter -1 when finished" << endl << endl;

		cin >> input;
		cout << endl;

		// this is the statement that checks for a -1 value and if true
		// the break statement will throw us out of loop
		if (input == -1)
		{
			break;
		}
		
		// if the value is within the correct values of a grade
		// increment the number stored in the correct grade array
		if (input > -1 && input < 6)
		{
			nNumberOfGrades [input]++;
		}
		// or inform user of mistake and let the loop continue
		else
		{
			cout << "Invalid number. Try Again" << endl << endl;
		}
	}
	
	int grade = 0;

	// another loop that loops through possible grades and displays the 
        // number of grades incremented
	// in the respective grade slot
	while ( grade < 6)
	{
		if (grade == 0)
		{
			cout << "Number of grades : " << nNumberOfGrades [grade] << " Letter grade: A" << endl << endl;
		}
		else if (grade == 1)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: B" << endl << endl;
		}
		else if (grade == 2)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: C" << endl << endl;
		}
		else if (grade == 3)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: D" << endl << endl;
		}
		else if (grade == 4)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: E" << endl << endl;
		}
		else if (grade == 5)
		{
			cout << "Number of grades :" << nNumberOfGrades [grade] << " Letter grade: F" << endl << endl;
		}
         
        grade++; // Increments grade

	}
	system("pause");
	return 0;
}


You could take the menu output, out of the loop to stop it repeating or even add a extra conditional check to display menu again.
Last edited on
I know you solved it, but it appears you guys skipped a part of the requirements.
Create a program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. If the user enters an invalid number, reject it and ask again. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category by using a loop and a parallel array (for the grade names).

Here is what I came up with.
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
#include <iostream>

const int SENTINEL = -1;

// swap function to help keep track of 
// the number of times a grade is entered
void swap(int *gradeCount, int index)
{
	int tempCount = 0;
	tempCount = gradeCount[index];
	tempCount += 1;
	gradeCount[index] = tempCount;
}


int main()
{
	char gradeLetter[5] = { 'A', 'B', 'C', 'D' , 'F' };  	// letter grades
	int gradeCount[5] = { 0, 0, 0, 0, 0 };					// number of times a grade is entered
		
	std::cout << "Enter your grade (0-4) or -1 to quit: ";
	int grade;
	std::cin >> grade;
	
	while(grade != SENTINEL)
	{
		switch(grade)
		{
			case 0:
				swap(gradeCount, 0);
				break;
			case 1:
				swap(gradeCount, 1);
				break;
			case 2:
				swap(gradeCount, 2);
				break;
			case 3:swap(gradeCount, 3);
				break;
			case 4:
				swap(gradeCount, 4);
				break;
			default:
				std::cout << "Invalid option\n";
				break;
		}
		
		if(grade != SENTINEL)
		{			
			std::cout << "Enter your grade (0-4) or -1 to quit: ";
			std::cin >> grade;
		}
	}
	
	for(int i = 0; i <= 4; i++)
	{
		std::cout << "There are " << gradeCount[i] << " of letter grade " << gradeLetter[i] << std::endl;
	}
	
	return 0;
}
Last edited on
Topic archived. No new replies allowed.