Code:
#include<stdio.h>
#include<string.h>
void readinput(int i);
void writeoutput(int i);
typedef struct {
char name[80];
char matrix[80];
int age;
char sex[6];
} record;
record students[30];
main(void)
{
int i, n, maleaverage, femaleaverage,malesum = 0, femalesum = 0, mcount = 0, fcount = 0, bfcount = 0, bmcount = 0;
printf("how many student");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
readinput(i);
}
for (i = 0; i < n; ++i)
{
if (strcmp(students[i].sex, "male") == 0){
malesum = malesum + students[i].age;
++mcount;
}
if (strcmp(students[i].sex, "female") == 0){
femalesum += students[i].age;
++fcount;
}
}
maleaverage = malesum/mcount;
femaleaverage = femalesum/fcount;
for(i = 0; i < n; ++i){
if (strcmp(students[i].sex, "male") == 0){
if ( students[i].age < maleaverage){
++bmcount;
}
}
if (strcmp(students[i].sex, "female") == 0){
if (students[i].age < femaleaverage){
++bfcount;
}
}
}
for (i = 0; i < n; ++i){
writeoutput(i);
}
printf("\n Average male age %d",maleaverage);
printf("\n Below Average of male age have %d students", bmcount);
printf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);
}
void readinput(int i)
{
printf("\nStudents no. %d\n", i+1);
printf(" Name ");
scanf(" %29[^\n]", students[i].name);
printf(" Matrix ");
scanf("%s", students[i].matrix);
printf(" sex ");
scanf("%s", &students[i].sex);
printf(" Age ");
scanf("%d", &students[i].age);
return;
}
void writeoutput(int i)
{
printf("\nName %s", students[i].name);
printf("\n Matrix %s", students[i].matrix);
printf("\n Sex %s", students[i].sex);
printf("\n Age %d\n", students[i].age);
return;
}
my exe crashed when i only enter 1 type of sex..i think is becuz of
Code:
printf("\n Average male age %d",maleaverage);
printf("\n Below Average of male age have %d students", bmcount);
printf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);
how do i modified this part into
Code:
if(strcmp(students[i].age, "female) == 0)
rintf("\n Average female age %d\n", femaleaverage);
printf("\n Below Average of female age have %d students\n", bfcount);
[/CODE]
but i dont want use the for( i = 0; i < n; ++i) because i only want print out 1 general average.. i dun wan every students oso have 1 average.. so what shud i do?