PLEASE HELP! Write a program that will read a list of student last names and thr
ID: 665533 • Letter: P
Question
PLEASE HELP!
Write a program that will read a list of student last names and three integer value scores in the range 0-100.
The names and scores should be stored in an array of the following data structures:
char[] - name
int[] - three test scores
double - student average
After reading in ALL the data, the program should calculate the average for each student and store it in the appropriate field/member. The program should then sort the array by the students' last name and then print the last name and average
Explanation / Answer
#include<stdio.h>
#include<string.h>
#define SIZE 50
struct student {
char name[30];
int scores[3];
double studentavg;
};
void main() {
int i, j, max, count, total, n, a[SIZE], ni;
struct student s[SIZE];
char temp[30];
double temp2;
printf("Total Number of Students: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf(" Enter name of the student : ");
scanf("%s", &s[i].name);
}
for (i = 0; i < n; i++) {
s[i].studentavg=0;
for (j = 0; j < 3; j++) {
printf(" Enter marks of student %d for subject %d : ", i, j);
scanf("%d", &s[i].scores[j]);
s[i].studentavg=s[i].scores[j]+s[i].studentavg;
}
s[i].studentavg=s[i].studentavg/3;
}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(s[i].name, s[j].name) > 0)
{
strcpy(temp, s[i].name);
strcpy(s[i].name, s[j].name);
strcpy(s[j].name, temp);
temp2=s[i].studentavg;
s[i].studentavg=s[j].studentavg;
s[j].studentavg=temp2;
}
}
}
for(i=0;i<n;i++)
{
printf("Name: %s Average: %f ",s[i].name,s[i].studentavg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.