Name, Project 1,Project 2, Project 3, Project 4,Project 5,Project 6,Project 7, M
ID: 3910315 • Letter: N
Question
Name, Project 1,Project 2, Project 3, Project 4,Project 5,Project 6,Project 7, Midterm, Final James Martin,100,80,70,90,100,100,100,95,84 Mary Thompson,100,100,100,91,100,100,100,75, 85 John Garcia,80,88,72,60,100,80,40, 68, 72 Patricia Martinez, 20,40, 60, 50, 20,70, 60, 93, 87 Robert Robinson, 70, 50,55,65, 75, 75, BO, 60,70 Jennifer Clark, 95,90,90,95,90,95, 90, 95, 90 Michael Rodriguez, 96,92,94, 96,92, 96,92,96,92 Linda Lewis, 97,94, 98,97,94,97,94, 97, 94 William Lee, 98,96,102,98,96,98, 96,98, 96 Elizabeth Walker, 20,0,0,80,60,40,40,70,50Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
char name[50];
float project[7];
float midterm, final;
float projavg, avgexam, weighttot;
char grade;
}student;
/* Function will read the content of csv file & store it into array of structure passed from main() */
void readfile(char *filenm,student *p) /* function will receive file name & address of structure array as parameter */
{
int i=0,j;
FILE *fp;
char buf[1024], fields[1024], *token;
fp = fopen(filenm, "r"); /* Open the file for reading */
if(fp==NULL)
{
printf("File can not be opened ");
exit(0);
}
fgets(fields, 1024, fp); /* Read field heading from file */
while(i<10)
{
fgets(buf, 1024, fp); /* read the content of file line by line */
/* strtok() function will parse the fields from line (buf) seprated by comma */
token = strtok(buf, ",");
strcpy((p+i)->name,token);
for(j=0;j<7;j++)
{
token = strtok(NULL, ",");
(p+i)->project[j] = atof(token);
} /* end of for loop */
token = strtok(NULL, ",");
(p+i)->midterm = atof(token);
token = strtok(NULL, ",");
(p+i)->final = atof(token);
i++;
} /* end of while loop */
close(fp); /* close the file */
} /* end of function */
/* This function will perform the desired calculation */
void calculate(student *p)
{
int i=0,j;
float min, projsum;
while(i<10)
{
min = (p+i)->project[0];
projsum = min;
for(j=1;j<7;j++)
{
if( (p+i)->project[j] < min )
min = (p+i)->project[j];
projsum = projsum + (p+i)->project[j];
} /* end of for loop */
(p+i)->projavg = (projsum-min)/6; /* calculate project average after dropping lowest project grade */
(p+i)->avgexam = ( (p+i)->midterm + (p+i)->final ) / 2; /* calculate average of exam component */
(p+i)->weighttot = .40 * (p+i)->projavg + .30 * (p+i)->midterm + .30 * (p+i)->final; /* calculate weighted total */
/* calculate the letter grade */
if( (p+i)->weighttot >= 90 && (p+i)->weighttot <= 100 )
(p+i)->grade = 'A';
else
if( (p+i)->weighttot >= 80 && (p+i)->weighttot <= 89 )
(p+i)->grade = 'B';
else
if( (p+i)->weighttot >= 70 && (p+i)->weighttot <= 79 )
(p+i)->grade = 'C';
else
if( (p+i)->weighttot >= 60 && (p+i)->weighttot <= 69 )
(p+i)->grade = 'D';
else
if( (p+i)->weighttot >= 0 && (p+i)->weighttot <= 59 )
(p+i)->grade = 'F';
i++;
} /* end of while loop */
} /* end of function */
/* Function will write the content of structure array into output csv file */
void writefile(char *filenm,student *p)
{
FILE *fp;
int i=0;
fp = fopen(filenm, "w");
fprintf(fp,"Name,Project,Exam,Weighted total,Grade Letter "); /* writing heading into file */
while(i<10)
{ /* writing fields into file seprated by comma */
fprintf(fp,"%s,%.2f,%.2f,%.2f,%c ", (p+i)->name, (p+i)->projavg, (p+i)->avgexam, (p+i)->weighttot, (p+i)->grade );
i++;
}
fclose(fp);
} /* end of function */
int main(int argc, char *argv[])
{
student s[10];
if (argc < 3){
printf("Wrong number of command line arguments ");
exit(0);
}
readfile(argv[1],s); /* calling function to read the filename stored in argv[1] */
calculate(s); /* calling function to perform desired calculation */
writefile(argv[2],s); /*calling function to write the structure array into file */
return 1;
}
gradebook.csv
Name,Project 1,Project 2,Project 3,Project 4,Project 5,Project 6,Project 7,Midterm,Final
James Martin,100,80,70,90,100,100,100,95,84
Mary Thompson,100,100,100,91,100,100,100,75,85
John Garcia,80,88,72,60,100,80,40,68,72
Patricia Martinez,20,40,60,50,20,70,60,93,87
Robert Robinson,70,50,55,65,75,75,80,60,70
Jennifer Clark,95,90,90,95,90,95,90,95,90
Michael Rodriguez,96,92,94,96,92,96,92,96,92
Linda Lewis,97,94,98,97,94,97,94,97,94
William Lee,98,96,102,98,96,98,96,98,96
Elizabeth Walker,20,0,0,80,60,40,40,70,50
output.csv
Name,Project,Exam,Weighted total,Grade Letter
James Martin,95.00,89.50,91.70,A
Mary Thompson,100.00,80.00,88.00,B
John Garcia,80.00,70.00,74.00,C
Patricia Martinez,50.00,90.00,74.00,C
Robert Robinson,70.00,65.00,67.00,D
Jennifer Clark,92.50,92.50,92.50,A
Michael Rodriguez,94.33,94.00,94.13,A
Linda Lewis,96.17,95.50,95.77,A
William Lee,98.00,97.00,97.40,A
Elizabeth Walker,40.00,60.00,52.00,F
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.