write a C program that computes the average grade of 0 or mo

write a C program that computes the average grade of 0 or more students.

- the user typing the wrong student count several times (negative values), until a positive value is input If the user types no valid grades (i.e. non-negative numbers), then the average returned from function studentAverage is -1 and an average of -1 is displayed:

#include // function declarations:

double readGrade();

double studentAverage(int crtStudent);

int processStudentScores();

// =========================================================

// function definitions:

/** * Read from the terminal and return a double number. * It assumes the user input is a valid real number. * * @return a double number */

double readGrade() { // ... } /** *

Prints a prompt that uses the index (crtStudent) of the current student: * \"Enter grades for student #3, separated by \' \'. Enter -1 to end: \" *

* Reads grades (type double, >=0) from the terminal until it encounters a

* negative value and then returns the average of the non-negative grades.

* The grade list is terminated with a negative value (e.g. -1).

* E.g. if the user enters 3 grades (-1 is the special termination value): 90 80 70 -1

* then the program returns (90 + 80 + 70) / 3 *

* @returns the average grade if the number of grades read >0 or return -1.0 if * no grades were read.

*/ double studentAverage(int crtStudent) { int count; double sum; double grd; double avg;

// initialize variables

// ...

// print the prompt, like in:

// Enter student #2 grades, separated by \' \'. Enter -1 to end:

// The student index (as in #2 above) is given by formal parameter crtStudent

// ...

// Read grd using the readGrade function // in a loop as long as grd >= 0.

// Accumulate total in variable sum, increment count

// ...

// Hint: at the end of the loop variable count must be equal with the number of // non-negative grades (i.e. >=0) read and accumulated in variable sum

// compute the average IF count > 0. If count == 0 then avg is assigned -1

// ...

// return avg

// ... } /** * This function reads a number (integer studentCount).

* Variable studentCount must be >= 0.

* Then, in a loop repeated studentCount times, it reads the grade list, computes the

* grade average with the studentAverage function and displays a message to the

* user, like this: * \"The average of student #1 is 80.00.\"

* The average grade must be displayed with 2 decimals. *

* @returns the number of students processed */

int processStudentScores() {

double avg;

int studentCount;

int i;

// initialize variables

// ...

// do:

// print prompt \"Enter the number of students (>=0): \"

// read variable studentCount

// while studentCount is negative

// ...

// loop studentCount times:

// call function studentAverage with param i (0..studentCount-1)

// and save returned value to variable avg

// print message :

// The average of student #(put here student index i) is (avg)

// ...

// return the number of total of students processed:

// ... }

int main() {

processStudentScores();

return 0;

}

Solution

// C code to determine average score of student

#include <stdio.h>
#include <stdlib.h>

double readGrade();
double studentAverage(int crtStudent);
int processStudentScores();

double readGrade()
{
double grd;
scanf(\"%lf\",&grd);

return grd;
}

double studentAverage(int crtStudent)
{
int count = 0;
double sum = 0;
double grd;
double avg;

printf(\"\ Enter student #%d grades, separated by \' \'. Enter -1 to end: \",crtStudent);
while(1)
{
grd = readGrade();

if(grd < 0)
break;
else
{
sum = sum + grd;
count++;
}
}

if(count > 0)
avg = sum/count;
else
avg = -1;

return avg;
}

int processStudentScores()
{
double avg;
int studentCount;
int i;

while(1)
{
printf(\"Enter the number of students (>=0): \");
scanf(\"%d\",&studentCount);

if(studentCount >= 0)
break;
else
printf(\"Invalid Input\ \");
}

for (i = 0; i < studentCount; ++i)
{
avg = studentAverage(i+1);
printf(\"The average of student #%d is %0.2lf\ \",(i+1),avg);
}

return studentCount;
}

int main()
{
processStudentScores();
return 0;
}

/*
output:

Enter the number of students (>=0): 3

Enter student #1 grades, separated by \' \'. Enter -1 to end: 90 80 70 -1
The average of student #1 is 80.00

Enter student #2 grades, separated by \' \'. Enter -1 to end: 56 88 76 56 -1
The average of student #2 is 69.00

Enter student #3 grades, separated by \' \'. Enter -1 to end: 67 87 45 66 87 -1
The average of student #3 is 70.40

*/

write a C program that computes the average grade of 0 or more students. - the user typing the wrong student count several times (negative values), until a posi
write a C program that computes the average grade of 0 or more students. - the user typing the wrong student count several times (negative values), until a posi
write a C program that computes the average grade of 0 or more students. - the user typing the wrong student count several times (negative values), until a posi
write a C program that computes the average grade of 0 or more students. - the user typing the wrong student count several times (negative values), until a posi

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site