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 floats. Each float refers to a GPA of a student where the GPA is
stored at some index i such that i refers to the ID of the student
with that GPA. Note that currently, students with IDs and are the
only ones with GPAs. Student with ID is at position in the array
and the one with ID is at position 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 MAXSTUDENTS
This will allow you to use the name MAXSTUDENTS anywhere you need to
use the constant when referring to the number of students.
Function prototypes:
int menu;
Array Lookup
Page
void processChoiceint choice, float gpas;
void addGPAfloat gpas;
void removeGPAfloat gpas;
int countStudentsfloat gpas;
float calcAvgGPAfloat gpas;
void displayGPAsfloat gpas;
The algorithm for the main function follows: Pay attention to
indentation in the algorithm.
Declare a float array named gpas of size MAXSTUDENTS and set
all its contents to
Declare an integer variable named choice.
Start a dowhile 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
The algorithm for the menu function follows:
Declare an integer variable named chch stands for choice.
Present the user with the following menu:
Add a student's GPA.
Remove a student's GPA.
Count number of students.
Display GPA data.
Calculate average GPA.
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
Case : call the addGPA function and pass it the gpas
array.
Case : call the removeGPA function and pass it the gpas
array.
Case : 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 : call the displayGPAs function and give it the gpas
array.
Case : 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 : 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 to MAXSTUDENTS.
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
Check to see if the gpas array at the index referred to by
ID is If it is then the function should do:
Ask for a gpa between and 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 to MAXSTUDENTS.
If the user gives a value outside of the range, the function
should print an appropriate error message and then return.
Array Lookup
Page
Otherwise, the function should do:
Reduce the value of the ID entered by
Assign to the position of the gpas array indexed by ID
The description of the countStudents function follows:
The function should count the number of nonzero entries in the
array and return the count. Use a for loop to run down the
array up to but not including MAXSTUDENTS.
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 MAXSTUDENTS. 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
