I need to modify a pre existing code and it says to add a forloop array. The instructions are Create a program that will read-in FIVE GRADES FROM THE CONSOLE into an ARRAY for each specific student. Of course, you will have to read-in the name of the student as well as their grades. The pre existing code works fine its just that now I need to add an array for loop and have no idea what that is. Any help is greatly appreciated.
//This program will prompt the user for 5 numbers and output the average of these numbers
//and a letter grade
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int avg=0;
int sum=0;
int num1=0;
int num2=0;
int num3=0;
int num4=0;
int num5=0;
int w1=0;
int w2=0;
int w3=0;
int w4=0;
int w5=0;
int wa=0;
char letter;
letter='A';
letter='B';
letter='C';
letter='D';
letter='F';
string firstname;
//Prompt for 5 numbers and your name
cout << "Please input your name\n" << endl;
cin >> firstname;
cout << "Okay "<< firstname <<" please input five numbers each separated by a space: \n";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
//Calculate the average
sum = num1+num2+num3+num4+num5;
avg=sum/5;
cout << "These are the numbers: " << num1 << "," << num2 << "," << num3 << "," << num4 << "," << num5 << endl;
cout << "This is the sum: " << num1+num2+num3+num4+num5 << endl;
cout << "This is the average: " <<sum/5 << endl;
//Determine letter grade based on average
if(avg>=90)
letter = ('A');
elseif(avg>=80)
letter = ('B');
elseif(avg>=70)
letter = ('C');
elseif(avg>=60)
letter = ('D');
elseif (avg<60)
letter = ('F');
else cout << "There is an error";
cout << "This is the letter grade: " << letter << endl;
w1 = (num1 * .20);
w2 = (num2 * .20);
w3 = (num3 * .20);
w4 = (num4 * .20);
w5 = (num5 * .20);
wa = (w1 + w2 + w3 + w4 + w5);
cout << "The weighted average is: " << wa << endl;
system ("pause");
return 0;
}
You will have to get the name from the user as you do in your existing code. The only difference would be using a for loop to input each grade at a time instead of getting them all at once. I'm sure that you've gone over for loops, as well as arrays. What you would do is declare an int array, and use the loop to increment through the array and add in values. Keep in mind, once you modify the program to an array, you will need to change your codebase from where you display all the numbers back to the user and calculate the sum as well. I will help you with the first for loop. The rest of the modification should be simple.
1 2 3 4 5 6 7 8 9 10
constint maxSize = 5;
int studentGrades[maxSize] = { 0, 0, 0, 0, 0 };
for (int i = 0; i < maxSize; i++)
{
cout << "Enter grade number " << i;
int temp = 0;
cin >> temp;
studentGrades[i] = temp;
}