Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

name major 0 Math CS 2Bio 3 Chem 4 Math CS credits 0 Mary 1Fred 2Pat 3 Jackie 4

ID: 2246412 • Letter: N

Question

name major 0 Math CS 2Bio 3 Chem 4 Math CS credits 0 Mary 1Fred 2Pat 3 Jackie 4 Steph 5Jo 0 38 1 72 2 24 3 58 4 97 583 string name [100]; string major[100]: double credits[100]; int numstuds; 4. Assume that we want to store information about students. Assume that elements 0- numstuds in each of the arrays declared above have been given values like those shown. Write program segments to do each of the following: (see code above for syntax) a. Given the following prototype and call statement, write a function that will display a list of all of the Math majors by name. (see sample syntax on the back page) Prototype: Call: Function Header: void listMathMaj( string name [1, string major[l, int numstud); listMathMaj( name, major, numstud); void listMath Maj( string name [, string majorlI, int numstud);

Explanation / Answer

4 a.) Function body

void listMathMaj(string name[], string major[], int numstud)

{

int i;

for(i = 0; i < numstud; i++)

{

if(major[i] == "Math")

cout << name[i] << endl;

}

}

4 b.) Call statement

double average = calculateAvgCredits(credits, numstud);

The complete Function:

double calculateAvgCredits(double credits[], int numstud)

{

int i;

double sum = 0;

for(i = 0; i < numstud; i++)

sum += credits[i];

double avg = sum / numstud;

return avg;

}

4 a.) Function body

void listMathMaj(string name[], string major[], int numstud)

{

int i;

for(i = 0; i < numstud; i++)

{

if(major[i] == "Math")

cout << name[i] << endl;

}

}