In: Computer Science
Professor realizes that he hates his class and has decided to give each of his students a 20-point decrease in all their grades for no reason. Help him write the C++ code that will allow him to do this.
Consider the code on the next page. It creates an array of 10 structs called students which contains their name and their grade. It is nearly complete. Your task is the following:
#include <iostream>
using namespace std;
//struct that creates student
struct student
{
string name;
int grade;
};
//user-defined function prototypes
void subtractTwenty(student[10]);
void printArray(student[10]);
int main()
{
//array of 10 students
student arrayOfStudents[10];
//populate the 10 students
arrayOfStudents[0] = { "Alice", 78 };
arrayOfStudents[1] = { "Bobby", 90 };
arrayOfStudents[2] = { "Casey", 44 };
arrayOfStudents[3] = { "Devon", 57 };
arrayOfStudents[4] = { "Ethan", 12 };
arrayOfStudents[5] = { "Frank", 99 };
arrayOfStudents[6] = { "Grace", 70 };
arrayOfStudents[7] = { "Heidi", 80 };
arrayOfStudents[8] = { "Isaac", 14 };
arrayOfStudents[9] = { "Jason", 40 };
cout << endl << "Before subtracting 20: " << endl << endl;
printArray(arrayOfStudents);
subtractTwenty(arrayOfStudents);
cout << endl << "After subtracting 20: " << endl << endl;
printArray(arrayOfStudents);
cout << endl << endl;
return 0;
}
void subtractTwenty(student a[10])
{
}
void printArray(student a[10])
{
for (int i = 0; i < 10; i++)
{
cout << "Name: " << a[i].name << ", Grade: " << a[i].grade << endl;
}
}
Updated code is as follows:
#include <iostream>
using namespace std;
//struct that creates student
struct student
{
string name;
int grade;
};
//user-defined function prototypes
void subtractTwenty(student[10]);
void printArray(student[10]);
int main()
{
//array of 10 students
student arrayOfStudents[10];
//populate the 10 students
arrayOfStudents[0] = { "Alice", 78 };
arrayOfStudents[1] = { "Bobby", 90 };
arrayOfStudents[2] = { "Casey", 44 };
arrayOfStudents[3] = { "Devon", 57 };
arrayOfStudents[4] = { "Ethan", 12 };
arrayOfStudents[5] = { "Frank", 99 };
arrayOfStudents[6] = { "Grace", 70 };
arrayOfStudents[7] = { "Heidi", 80 };
arrayOfStudents[8] = { "Isaac", 14 };
arrayOfStudents[9] = { "Jason", 40 };
cout << endl << "Before subtracting
20: " << endl << endl;
printArray(arrayOfStudents);
subtractTwenty(arrayOfStudents);
cout << endl << "After subtracting
20: " << endl << endl;
printArray(arrayOfStudents);
cout << endl << endl;
return 0;
}
void subtractTwenty(student a[10])
{
for (int i = 0; i < 10; i++)
{
a[i].grade-=20;
if(a[i].grade<0)
{
a[i].grade=0;
}
}
}
void printArray(student a[10])
{
for (int i = 0; i < 10; i++)
{
cout << "Name: "
<< a[i].name << ", Grade: " << a[i].grade
<< endl;
}
}
Sample output:
Mention in comments if any mistakes or errors are found. Thank you.