Can anyone fix my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone fix my code?

I can not solve this question please help me out Q: 40 students in a class appeared in their examination. Their mark sheets have been given to you. The grade column of the mark sheet contains the Grade (A, B, C or F) obtained by the student. Write a function to calculate the total number of students who got F grade. Use Array to store grade [char grade [40]]. Grade.txt B A My ans: #include<stdio.h> #include<string.h> struct student { char grade[2]; }; int gradef(struct student a[], int si) { int c=0; for (int i = 0; i < 40; i++) { if(a[i].g!=F) { c++; } } return 0; } int main() { struct student s[40]; freopen ("Grade.txt","r",stdin); for (char i = 0; i < 40; i++) { scanf("%s",s[i].grade); } for (char i = 0; i < 40; i++) { printf("%s",s[i].grade); } char n = gradeF(s,40); printf("%s", n): }

1st Dec 2021, 5:03 AM
Mustaq Mahmud Tafhim
Mustaq Mahmud Tafhim - avatar
3 Answers
0
#include<stdio.h> #include<string.h> struct student { char grade[2]; }; int gradef(struct student a[], int si) { int c=0; for (int i = 0; i < 40; i++) { if(a[i].grade[0]=='F') { c++; } } return c; } int main() { struct student s[40]; for (int i = 0; i < 40; i++) { scanf("%c\n",&s[i].grade[0]); } int n = gradef(s,40); printf("%d", n); return 0; }
1st Dec 2021, 5:59 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Mustaq Mahmud Tafhim If you want to read from file you don't have to use scanf again. Also for accessing index always use int. Don't receive int into a char variable. There are so many silly mistakes, check carefully and learn.
1st Dec 2021, 6:01 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
0
Yes I would have used FILE *fptr and fopen () with fscanf() probably. Also the struct in itself is unnecessary, you only need FILE and char are[]. Hope that helps.
3rd Dec 2021, 5:23 AM
Joseph Fruin
Joseph Fruin - avatar