Question: Create a C program: to reinforce the fact that knowing how to manipulate an array boils down to knowing how to manipulate the array's indices.

Create a C program: to reinforce the fact that knowing how to
manipulate an array boils down to knowing how to manipulate the
array's indices. The program will use an array as a lookup table
which means that a required value is stored and retrieved from the
table by specifying the index where the value is to be stored or
retrieved from. For example, the following table represents an array
of 5 floats. Each float refers to a GPA of a student where the GPA is
stored at some index i such that i+1 refers to the ID of the student
with that GPA. Note that currently, students with IDs 2 and 5 are the
only ones with GPAs. [Student with ID 2 is at position 1 in the array
and the one with ID 5 is at position 4 in the array.]The program will be designed so that it contains functions that you
will write. ALL function bodies are to be written BELOW the main
function but the prototypes for those functions must be written just
above the main.
Type in the following definition of a preprocessor constant above the
main:
#define MAX_STUDENTS 5
This will allow you to use the name MAX_STUDENTS anywhere you need to
use the constant 5 when referring to the number of students.
Function prototypes:
int menu();
Array Lookup
Page 2
void processChoice(int choice, float gpas[]);
void addGPA(float gpas[]);
void removeGPA(float gpas[]);
int countStudents(float gpas[]);
float calcAvgGPA(float gpas[]);
void displayGPAs(float gpas[]);
The algorithm for the main function follows: (Pay attention to
indentation in the algorithm.)
Declare a float array named gpas of size MAX_STUDENTS and set
all its contents to 0.
Declare an integer variable named choice.
Start a do/while loop as follows.
do:
Call a function named menu and collect what it returns in
the variable called choice. The function does not take any
parameters.
Call a function named processChoice and pass it the choice
and the array in that order. This function does not return
anything.
while choice is not equal to 6.
The algorithm for the menu function follows:
Declare an integer variable named ch.(ch stands for choice.)
Present the user with the following menu:
1) Add a student's GPA.
2) Remove a student's GPA.
3) Count number of students.
4) Display GPA data.
5) Calculate average GPA.
6) Quit
Collect the choice from the user in the variable named ch.
Return ch.
The algorithm for the processChoice function follows:
Use a switch statement to determine what choice was passed into
the function:
Array Lookup
Page 3
Case 1: call the addGPA function and pass it the gpas
array.
Case 2: call the removeGPA function and pass it the gpas
array.
Case 3: call the countStudents function and pass it the
gpas array. Collect what the function returns in an
integer variable and print the contents of that variable
with a nice, relevant message.
Case 4: call the displayGPAs function and give it the gpas
array.
Case 5: call the calcAvgGPA function and pass it the gpas
array. Collect what the function returns in a float
variable and print the contents of that variable with a
nice, relevant message.
Case 6: print "Bye!"
Default: print a message stating that the choice was
invalid.
The algorithm for the addGPA function follows:
This function should ask for an integer ID from the user with
values from 1 to MAX_STUDENTS.
If the user gives a value outside of the range, the function
should print an appropriate error message and then return.
Otherwise, the function should do the following:
Reduce the value of the ID entered by 1.
Check to see if the gpas array at the index referred to by
ID is 0. If it is, then the function should do:
Ask for a gpa between 0 and 4 and keep asking until
it gets a valid gpa.
Once the function gets a valid gpa, it should then
assign the new gpa to the position in the gpas array
as indexed by the ID.
Else (that position in gpas is already taken) so print a
message stating that the student already has a gpa.
The algorithm for the removeGPA function follows:
This function should ask for an integer ID from the user with
values from 1 to MAX_STUDENTS.
If the user gives a value outside of the range, the function
should print an appropriate error message and then return.
Array Lookup
Page 4
Otherwise, the function should do:
Reduce the value of the ID entered by 1.
Assign 0 to the position of the gpas array indexed by ID.
The description of the countStudents function follows:
The function should count the number of non-zero entries in the
array and return the count. Use a for loop to run down the
array up to, but not including MAX_STUDENTS.
The description of the displayGPAs function follows:
The function should display a header row with the words
"Student ID" and "GPA" in two neat columns. Then the function
should use a for loop to run down the array up to, but not
including MAX_STUDENTS. Within the loop, the function should
print the student IDs and corresponding GPAs neatly under the
c

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!