Text file gradetxt contains 10 students grade Each line with

Text file “grade.txt” contains 10 students grade. Each line with one grade.

Read all grades from the file “grade.txt” and write them as integers into a binary file (binaryGrade.dat).

Read grades from the binary file “binaryGrade.dat” and compute and display the average grade.

Solution

//source code

#include<stdio.h>
//#include<file.h>
void main()
{
        // a+=10 a = 9 b+= 8 b = 7 c+= 6 c = 5 d = 4 f = 3
        FILE *f1=fopen(\"grade.txt\",\"r\");
        FILE *f2=fopen(\"binaryGrade.dat\",\"w\");
        char grade[10][3];
        int i=0;
        while(fscanf(f1,\"%s\",grade[i]))
        {
                printf(\"%d\",i);
                printf(\"%s\",grade[i]);
                switch(grade[i][0])
                {
                        case \'a\':
                                if(grade[i][1]==\'+\')
                                        fprintf(f2,\"10\ \");
                                else
                                        fprintf(f2,\"9\ \");
                                break;
                        case \'b\':
                                if(grade[i][1]==\'+\')
                                        fprintf(f2,\"8\ \");
                                else
                                        fprintf(f2,\"7\ \");
                                break;
                        case \'c\':
                                if(grade[i][1]==\'+\')
                                        fprintf(f2,\"6\ \");
                                else
                                        fprintf(f2,\"5\ \");
                                break;
                        case \'d\':
                                fprintf(f2,\"4\ \");
                                break;              
                        case \'e\':
                                fprintf(f2,\"3\ \");
                                break;
                        default:
                                printf(\"Wrong Input\ \");              
                }
                i++;
                if(i==10)
                      break;
        }
        fclose(f2);
        FILE *f3=fopen(\"binaryGrade.dat\",\"r\");
        float avg=0;
        int j=0;
        while(fscanf(f3,\"%d\ \",&i))
        {
             avg = avg+i;
             j++;
                if(j==10)
                      break;
        }
        avg=avg/10;
        printf(\"\ average grade=%f\ \",avg);
}

//guide.txt

b
c
d
a
e
d
e
a+
b
b+

//binaryGrade.dat

7
5
4
9
3
4
3
10
7
8

Text file “grade.txt” contains 10 students grade. Each line with one grade. Read all grades from the file “grade.txt” and write them as integers into a binary f
Text file “grade.txt” contains 10 students grade. Each line with one grade. Read all grades from the file “grade.txt” and write them as integers into a binary f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site