Currently student grade records are stored parallel arrays. const int NG = 4; st
ID: 655338 • Letter: C
Question
Currently student grade records are stored parallel arrays.
const int NG = 4;
string names[] = {"Amy Adams", "Bob Barr", "Carla Carr",
"Dan Dobbs", "Elena Evans"
};
int exams[][NG]= { {98,87,93,88},
{78,86,82,91},
{66,71,85,94},
{72,63,77,69},
{91,83,76,60}
};
We want to convert to an array of structures with additional exam average field.
Your tasks are:
1. define a structure for student grade records.
2. declare an array of records.
3. populate from the parallel arrays.
4. define a function to compute average and
populate average field.
5. define a function to display name and average.
6. demonstrate your functions and program
Explanation / Answer
#include <stdio.h>
struct student
{
char name[50];
int marks[4];
float average;
};
void getStudentDetails(struct student[]);
void calcAverage(struct student[]);
void displayStudent(struct student[]);
const int NG = 4;
int nn=5;
char names[10][50] = {"Amy Adams", "Bob Barr", "Carla Carr",
"Dan Dobbs", "Elena Evans"
};
int exams[10][4]= { {98,87,93,88},
{78,86,82,91},
{66,71,85,94},
{72,63,77,69},
{91,83,76,60}
};
void main()
{
struct student s[10]; // creating array of structures
clrscr();
getStudentDetails(s);
calcAverage(s);
displayStudent(s);
getch();
}
void getStudentDetails(struct student s[])
{
int i,j;
printf("Enter information of students: ");
for(i=0;i<nn;i++)
{
strcpy(s[i].name,names[i]);
for(j=0;j<NG;j++)
s[i].marks[j]=exams[i][j];
}
}
void calcAverage(struct student s[])
{
float sum=0.0;
int i,j;
for(i=0;i<nn;i++)
{
for(j=0;j<NG;j++)
{
sum+=s[i].marks[j];
}
s[i].average=sum/NG;
sum=0.0;
}
}
void displayStudent(struct student s[])
{
int i;
printf("Exam Result: ");
for(i=0;i<nn;i++)
{
printf(" Student %d: ",i+1);
printf(" Name: %s",s[i].name);
printf(" Average: %f",s[i].average);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.