Please write code in C, Thank you! CSV (comma-separated values) is a popular fil
ID: 3909962 • Letter: P
Question
Please write code in C, Thank you!
CSV (comma-separated values) is a popular file format to store tabular kind of data. Each record is in a separate line and each field in a record is separated (delimited) by a comma. For example, if we had a tabular data as shown in the following table. James Martin Mary Thompson John Garcia 90 80 82 84 70 74 78 90 91 92 92 This data has three records, each record with 5 fields. A CSV file containing the above data would look as follows James Martin, 90,80,70,90 Mary Thompson, 91,82,74,91 John Garcia, 92,84,78,92 Some CSV files also contain a header on the first line signifying name of each field. A CSV file can be imported and exported by almost all popular spreadsheet or data processing applications Since it is a simple text file, we can also view it in any text editor. For this project, write a C program, calc_grade.c, to take input filename and output filename from the command line The program should read in the input CSV file, gradebook.csv, (passed from command line) which contains names and grades of all 7 projects, midterm, and final exam for 10 (hypothetical) students in this class. Notice that this CSV file has a header, 10 records, and 10 fields. Your program must calculate and output the name, average of project component, average of exam component, weighted total, and the grade letter for each student in output CSV file (filename passed from command line). Please refer below (or the syllabus) on how to perform the calculations. Your output CSV file must have a header, 10 records, and 5 fields, as shown below IMPORTANT: 1. Take input filename (argv[1]) and output filename (argv[2]) from the command line. 2. Out of the seven projects, the lowest grade is dropped when calculating average of project component. 3. Average of exam component is simply the average of midterm and final 4. Remember that project makes 40%, midterm makes 30%, and final makes 30% of the 5. 6. weighted total Limit the calculated values to 2 decimal places when writing to the file. You may use the logic in project 1 to calculate the grade letter.Explanation / Answer
here is your program : ------------->>>>>>>>>>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Record_{
char name[50];
int project[7];
int mid;
int final;
double pTotal;
double exam;
double weighted;
char grade;
}Record;
void readHeader(FILE *fp){
char str[500];
if(!feof(fp))
fgets(str,500,fp);
}
void writeHeader(FILE *fp){
fprintf(fp,"%s,%s,%s,%s,%s","Name","Project","Exam","Weighted Total","Grade Letter");
printf("%s,%s,%s,%s,%s","Name","Project","Exam","Weighted Total","Grade Letter");
}
Record* readOneRecord(FILE *fp){
char str[500];
if(!feof(fp))
fgets(str,500,fp);
else
return NULL;
Record *r = (Record *)malloc(sizeof(Record));
char *word = strtok(str,",");
if(word != NULL){
strcpy(r->name,word);
}
int i;
for(i = 0;i<7;i++){
word = strtok(NULL,",");
if(word != NULL){
r->project[i] = atoi(word);
}
}
word = strtok(NULL,",");
if(word != NULL){
r->mid = atoi(word);
}
word = strtok(NULL,",");
if(word != NULL){
r->final = atoi(word);
}
return r;
}
void calculate(Record *r){
r->pTotal = 0.0;
int i;
for(i = 0;i<7;i++){
r->pTotal = r->pTotal + r->project[i];
}
r->pTotal = r->pTotal/7;
r->exam = (r->mid + r->final)/2;
r->weighted = (40*r->pTotal/100) + (30*r->mid/100) + (30*r->mid/100);
if(r->weighted > 90){
r->grade = 'A';
}else if(r->weighted > 80){
r->grade = 'B';
}else if(r->weighted > 70){
r->grade = 'C';
}else if(r->weighted > 60){
r->grade = 'D';
}else{
r->grade = 'F';
}
}
void writeOneRecord(FILE *fp,Record *r){
fprintf(fp," %s,%0.2lf,%0.2lf,%0.2lf,%c",r->name,r->pTotal,r->exam,r->weighted,r->grade);
printf(" %s,%0.2lf,%0.2lf,%0.2lf,%c",r->name,r->pTotal,r->exam,r->weighted,r->grade);
}
int main(int argc,char *argv[]){
if(argc < 3){
printf("Please Provide The input/output filename as command line argument !!! ");
return -1;
}
FILE *fp1;
FILE *fp2;
fp1 = fopen(argv[1],"r");
if(fp1 == NULL){
printf("Input File Opening Error Check File !!! ");
return -1;
}
fp2 = fopen(argv[2],"w");
if(fp2 == NULL){
printf("Output File Creating Error!!!");
return -1;
}
Record *records = NULL;
readHeader(fp1);
writeHeader(fp2);
while(!feof(fp1)){
records = readOneRecord(fp1);
if(feof(fp1)){
fclose(fp1);
fclose(fp2);
return 0;
}
if(records != NULL){
calculate(records);
writeOneRecord(fp2,records);
}
}
fclose(fp1);
fclose(fp2);
return 0;
}