Hello can some one help me on this ? Write a menu driven program that allows the
ID: 3676081 • Letter: H
Question
Hello can some one help me on this ?
Write a menu driven program that allows the user to choose among the following options: Read a list of students Display the list of students Sort the list of students by id Exit The menu options are described as follows: Read list of students: When this option is chosen, the main() function will call the function read() that reads from the file "students.txt" a list of student ids, names and scores into three parallel arrays IDS, NAMES and SCORES that are passed by pointer. When this option is chosen, the main () function calls the function display() that displays students information (IDs, Names, and Scores) passed to it by pointer. When this option chosen the main() function calls the function sonBylDO ()that sorts the arrays IDS and NAMES and SCORES, by increasing student id. These ,arrays should be passed by pointer.Explanation / Answer
#include<conio.h>
#include<stdio.h>
#define MAX 1000
struct node
{
int IDS;
char *NAMES;
float SCORES;
}rec[MAX];
int i;
void Read()
{
FILE *fp;
fp= fopen("students.txt", "r");
if(fp==NULL)
{
printf( " Error in Opening File");
exit(0);
}
i=0;
while(!feof(fp))
{
fscanf(fp,"%d %s %f",&rec[i].IDS,rec[i].NAMES,&rec[i].SCORES);
i++;
}
}
void display()
{
int j;
for(j=0;j<=i;j++)
{
printf(" %d %s %f",rec[i].IDS,rec[i].NAMES,rec[i].SCORES);
}
}
void sort()
{
struct node temp;
int a,j;
for(a=0a<i;a++)
{
for(j=0;j<a;j++)
{
if(rec[a].IDS<rec[j].IDS)
{
temp.IDS=rec[a].IDS;
strcpy(temp.NAMES,rec[a].NAMES);
temp.SCORES=rec[a].SCORES;
rec[a].IDS=rec[j].IDS;
strcpy(rec[a].NAMES,rec[j].NAMES);
rec[a].SCORES=rec[j].SCORES;
rec[j].IDS=temp.IDS;
strcpy(rec[j].NAMES,temp.NAMES);
rec[j].SCORES=temp.scores;
}
}
}
}
void main()
{
struct node rec[MAX];
int ch;
while(1)
{
clrscr();
printf(" 1. Read List of Students");
printf(" 2. Display List of Students");
printf(" 3. Sort List of students by ID");
printf(" 4. Exit");
printf(" Enter your choice");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
Read();
break;
case 2:
display();
break;
case 3:
sort();
break;
case 4: exit(0);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.