*****C Programming only***** Write a complete program, using C, to get data from
ID: 3774227 • Letter: #
Question
*****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.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c[1000];
FILE *fptr;
char Name[10][100];
char SSN[10][20];
int i=0;
int quiz[10], mid[10], assignment[10], participation[10], final_marks[10];
if ((fptr = fopen("data.txt", "r")) == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}
/*Enter number of line in data.txt*/
int count =3;
printf(" ");
// reads text until newline
for(i=0; i<count; i++)
fscanf(fptr,"%s %s %d %d %d %d %d", &Name[i], &SSN[i], &quiz[i], &mid[i], &assignment[i], &participation[i], &final_marks[i]);
fclose(fptr);
fptr = fopen("RESULT.txt", "w");
if (fptr == NULL)
{
printf("File does not exists ");
return;
}
float Total=0;
char ch1;
for(i=0; i<count; i++)
{
Total= (15.0/100.0)*quiz[i] + (15.0/100.0)*mid[i] +(40.0/100.0)*assignment[i] + (10.0/100)* participation[i] + (20.0/100.0)*final_marks[i];
if(Total >= 90)
ch1='A';
else if (Total>=80)
ch1='B';
else
ch1='C';
fprintf(fptr, "Name:%s , SSN No: %s, Grade:%c", Name[i],SSN[i],ch1);
printf("Name:%s, SSN No:%s, Grade:%c", Name[i],SSN[i],ch1);
fprintf(fptr, " ");
printf(" ");
}
fclose(fptr);
return 0;
}
------------------------------------------------
create a data.txt file in same location
paste these details in data.txt
LISA 111-11-1111 100 100 100 100 100
Jack 222-22-2222 80 80 100 90 100
Jajra 222-3-2426 70 70 100 90 95
------------------------------------------------------
output sample:-
Name:LISA, SSN No:111-11-1111, Grade:A
Name:Jack, SSN No:222-22-2222, Grade:A
Name:Jajra, SSN No:222-3-2426, Grade:B
------------------------------------------
same output will be generate in result.txt
---------------------------------------------------------------------------------------------
If you have any query, please feel free to ask.
Thanks a lot.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.