Write a \"c program\" menu driven program with the following options: Before sta
ID: 3583777 • Letter: W
Question
Write a "c program" menu driven program with the following options:
Before start showing the menu options, you have to make a file which contains data of some students’ ids and grades using appropriate format. The number of lines of the file is unknown. First task is to store the data in two parallel arrays (arr_ids, arr_grades). Make the ids array of type long to handle large integer values.
Then the program will show a menu of choices as following:
1. Add a grade (read the id, grade from the user). The program will add the grade and
id only if the entered id does not exist already among the id’s present in the array. If it exists, the program shall inform the user by showing an appropriate message. (Assume that each id has only one grade, so duplicated id is not allowed).
2. Find a grade given an id (i.e. your program will read the id from the user and find the corresponding grade to display it).
3. Delete a user from the 2 arrays given the relevant id (so you have to delete both: id
and grade from the array).
4. Find the highest three grades and print them with the corresponding ids.
5. Find the lowest three grades and print them with the corresponding ids.
6. Finds the average of grades and display it on the screen.
7. Find grades which are greater than the average and print them with the corresponding ids.
8. Find the max grade among students and print its id with the corresponding grade.
9. Find the min grade among students and print its id with the corresponding grade.
10. Save (write) the id’s and grades after the updates you have done previously in a file using the same format used in the original data file.
11. Print the 2 arrays on the screen using the same format of the data file.
"Hint: it must be a c program".
Explanation / Answer
explaination of how the parts will be solved -
to make a file with random id and grades, use the following code.
#include <stdio.h>
int main()
{
FILE *fp;
int i;
for (i=0;i<10000;i++)
{
fprintf(fp,"%ld, %ld",rand(),rand()%100);
}
fclose(fp);
return 0;
}
this will randomly assign numbers to a file in the int, int format example - like 132313, 32 . since grades must be less than 100 we use rand%100.
------------------------------------------------------------------
now to read from the file and generate arrays of id and grades.
index 0 will denote the 0th student , so id[0] will be the id of the 0th student and grade[0] wiill give the grade for the 0th student. This is how we will make our parallel array.
---------------------------------------------------------
bassic explaination of the parts.
1.
take the id as an input.
apply sequential search on all ids , with the input id as the key.
if the id is found , prompt the user to add the grade.
2.
similar to 1 , use sequential search to find the id , annd then display its corresponding grade.
3.
take the id as an input, delete it from both the arrays
4 and 5 .
sequentially traverse the grade array to find the top 3 min and max scores.
6.
keep a counter, count = 0;
one by one add the grade to the cumulative sum, while incrementing count.
add the end of the file, sum will have the final sum of all grades, and count will have the number of students.
so, sum/count will give average.
7.
sequential traverse the array and compare the average with grades, and print them if they satisfy the condition.
similarly for other parts.
------------------------------------------------
c code
array_id[] and array_grades[] are already generated above.
hence we start from printin menu.
----------------------------------------------------
int main()
{
int choice;
printf("Press 1 to add a grade");
printf("Press 2 to find a grade");
printf("Press 3 to delete a id");
printf("Press 4 to find highest 3 grades");
printf("Press 5 to find lowest 3 grades");
printf("Press 6 to find average");
/* do the otther parts likewise */
printf("enter your choice");
scanf("%d",ch);
switch(ch)
{
case 1:
//read the id
long int id;
scanf("%ld",id);
//sequentially search the array_id;
int i=0;
while(i<100000)
{
if(id==array_id[i])
{
flag=1;
break;
}
i++;
}
if(flag==0) //id does not exist get the grade for that i'th studennt
{
scanf("%d",array_id[i]);
}
else
printf("student already exists");
}
}
case 2:
int i = 0;
long int id;
//enter id
scanf("%ld",id);
while(i < 100000)
{
if(id == arrayid[i])
{
printf("id found for %ld and student grade is %d",array_id[i],array_grade[i]);
break;
}
i++;
}
case 3:
//if the id is found simply change its value to -1 since grade can not be negative
long int id;
int i =0 ;
scanf("%ld ", id);
while(i<100000)
{
if(id==array_id[i])
{
array_id[i]=-1;
array_grade[i]=-1;
break;
}
i++;
}
case 4:
int max -1;
int c=3;
while(c--)
{
int i = 0;
int max_index;
while(i<100000)
{
if(max < array_grades[i])
{
max=array_grades[i];
max_index=i;
}
printf("highest grade for %ld and are %d",array_id[max_index], array_grade[max_index]);
}
}
case 5 :
//similary for min;
case 6:
long int sum=0;
long int count = 0;
int grade = 0;
FILE *fp;
int temp,i=0;
fp = fopen("one.txt", "r");
fscanf(fp,"%ld",temp);
whiile(!feof(fp)
{
sum=sum+array_grades[i];
i++;
fscanf(fp,"%ld",temp);
count++;
}
double average =sum / count;
printf("avg is %lf ", average);
}
return 0;
}
//do the other parts similary
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.