C Programming only Write a complete program using C to get
***** C Programming only *****
Write a complete program, using C, to get data from file name DATA.TXT one line at a time until there is no more data in that file. The following is one sample line in DATA.TXT ( have as many record as you wish in DATA.TXT)
Name SSN quiz mid assignments participation final
LISA 111-11-1111 100 100 100 100 100
Jack 222-22-2222 80 80 100 90 100
Note that the first line is no in DATA.txt.
Your program should create a file name RESULT.txt that reports Name, SSN and a letter grade according to the following rules:
Total= 15% quiz + 15 %mid +40% assignments + 10% Participation+ 20%final
If total >= 90 then A else if total>=80 then B….
You can print same output to screen as well as RESULT.txt.
Solution
// C code
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// determine grade
char letterGrade(double total)
{
char grade;
if(total >= 90)
grade = \'A\';
else if(total >= 80)
grade = \'B\';
else if(total >= 70)
grade = \'C\';
else if(total >= 60)
grade = \'D\';
else
grade = \'F\';
return grade;
}
int main()
{
char name[20];
char ssn[20];
double quiz,mid,assignmets,participation,final;
double total;
char grade;
FILE *inputFile;
FILE *outputFile;
// open input file
if ((inputFile = fopen(\"DATA.txt\", \"r\")) == NULL)
{
// Program exits if file pointer returns NULL.
printf(\"Error! opening file\");
exit(1);
}
// open output file
outputFile = fopen(\"RESULT.txt\", \"w\");
if(outputFile == NULL)
{
printf(\"Error!\");
exit(1);
}
fprintf(outputFile, \"Name\\tSSN\\t\\t\\tGrade\ \");
while(1)
{
fscanf(inputFile,\"%s\", name);
fscanf(inputFile,\"%s\", ssn);
fscanf(inputFile,\"%lf\", &quiz);
fscanf(inputFile,\"%lf\", &mid);
fscanf(inputFile,\"%lf\", &assignmets);
fscanf(inputFile,\"%lf\", &participation);
fscanf(inputFile,\"%lf\", &final);
// determien total and grade
total = 0.15*quiz + 0.15*mid + 0.4*assignmets + 0.1*participation + 0.2*final;
grade = letterGrade(total);
// output to file
fprintf(outputFile,\"%s\\t\", name);
fprintf(outputFile,\"%s\\t\\t\",ssn);
fprintf(outputFile,\"%c\ \", grade);
// break when end of file is reached
if (feof(inputFile))
break;
}
// close both filess
fclose(inputFile);
fclose(outputFile);
return 0;
}
/*
DATA.txt
Lisa 111-11-1111 100 100 100 100 100
Jack 222-22-2222 80 80 100 90 100
Jason 333-33-3333 45 78 100 90 100
Alex 444-44-4444 80 80 67 90 100
Mike 555-55-5555 80 23 44 90 100
Danny 666-66-6666 23 23 44 90 44
RESULT.txt
Name SSN Grade
Lisa 111-11-1111 A
Jack 222-22-2222 A
Jason 333-33-3333 B
Alex 444-44-4444 C
Mike 555-55-5555 D
Danny 666-66-6666 F
*/


