C Programming Problem 1 Student Grade Averages Write a C pro

C Programming

Problem 1. Student Grade Averages

Write a C program called p1.c that computes the average grade of 0 or more students.
Here is a sample running session of how the score averages program p1.c should run. User input is
highlighted with bold italic. After entering data, the user types the ENTER key.

Enter the number of students (>=0): 3
Enter grades for student #0, separated by \' \'. Enter -1 to end: 100 90 80 -1
The average of student #0 is 90.00.
Enter grades for student #1, separated by \' \'. Enter -1 to end: 99.5 93.2 0 96.5 -1
The average of student #1 is 72.30.
Enter grades for student #2, separated by \' \'. Enter -1 to end: 0 0 90 100 110 -1
The average of student #2 is 60.00.

Here is another example of the program’s execution, with the user typing the wrong student count
several times (negative values), until a positive value is input:

Enter the number of students (>=0): -3
Enter the number of students (>=0): -2
Enter the number of students (>=0): 1
Enter grades for student #0, separated by \' \'. Enter -1 to end: 90 50 100 -1
The average of student #0 is 80.00.

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:

Enter the number of students (>=0): 1
Enter grades for student #0, separated by \' \'. Enter -1 to end: -1
The average of student #0 is -1.00

1. Write a C program called p1.c that computes the average grade for 0 or more students. The
program is partially written and you only have to fill in the dots.

2. Use the skeleton file

3. Write the code that is missing from p1.c from places indicated by comments // ...

4. Your code must follow exactly the given template.

5. The input and output of the program must comply exactly with the specification from this file
and from source file p1.c.

Solution

// Computing grade averages

#include <stdio.h>

// 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() {
// ...
double temp;
scanf(\"%lf\",&temp);
return temp;
}


/**
* 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
// ...
count = 0;
sum = 0;
// 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
// ...
printf(\"Enter student #%d grades, separated by \' \'. Enter -1 to end:\",crtStudent);

// Read grd using the readGrade function
// in a loop as long as grd >= 0.
// Accumulate total in variable sum, increment count
// ...
while(1==1)
{
grd = readGrade();
if(grd<0)
break;
else
{
sum = sum+grd;
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
// ...
if(count==0)
avg = -1;
else
avg = sum/count;

// return avg
// ...
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
// ...
i = 0;

// do:
// print prompt \"Enter the number of students (>=0): \"
// read variable studentCount
// while studentCount is negative
// ...
do
{
printf(\"Enter the number of students (>=0): \");
scanf(\"%d\",&studentCount);
}while(studentCount<0);

// 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)
// ...

for(i=0;i<studentCount;i++)
{
avg = studentAverage(i);
printf(\"The average of student #%d is %lf\",i,avg);
}
// return the number of total of students processed:
// ...
return i;
}


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

OUTPUT:

Enter the number of students (>=0): -1

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

Enter student #0 grades, separated by \' \'. Enter -1 to end:50 100 -1

The average of student #0 is 75.000000

Process returned 0 (0x0) execution time : 9.068 s

Press ENTER to continue.

C Programming Problem 1. Student Grade Averages Write a C program called p1.c that computes the average grade of 0 or more students. Here is a sample running se
C Programming Problem 1. Student Grade Averages Write a C program called p1.c that computes the average grade of 0 or more students. Here is a sample running se
C Programming Problem 1. Student Grade Averages Write a C program called p1.c that computes the average grade of 0 or more students. Here is a sample running se
C Programming Problem 1. Student Grade Averages Write a C program called p1.c that computes the average grade of 0 or more students. Here is a sample running se

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site