C: How to use strtok to output grades from input file & find average, minimum and maximum? -


i'm trying output grades of assignments input file, output average, minimum , maximum grades each assignment.

as example, input file class 4 students , 3 assignments may like:

8.5, 10.5, 90.5 49.5, 99, 97 88, 88, 100 88.5, 99, 0 

and output should be

student grades input file: 8.5, 10.5, 90.5 49.5, 99.0, 97.0 88.0, 88.0, 100.0 88.5, 99.0, 0.0  assignment # 1 stats avg = 58.6 min = 8.5 max = 88.5  assignment # 2 stats avg = 74.1 min = 10.5 max = 99.0  assignment # 3 stats avg = 71.9 min = 0.0 max = 100.0 

so far, i'm stuck on first part have output contents of file. have far, on or functions appreciated.

int main(int argc, char *argv[]) {     file *input_file;     int line_number = 0;     char *next_field;     char line[max_students+1];      input_file = fopen(argv[1], "r");      //output grades input file     while (1) {         fgets(line, max_students, input_file);         if (feof(input_file))             break;         next_field = strtok(line, " \n");       while (next_field != null) {             printf("==================================/n");             printf("student grades input file/n");             printf("==================================/n");             printf("%s,", next_field);              next_field = strtok(null, " \n");         }     } 

you tokenize , output this:

#include <stdlib.h> #include <stdio.h> #include <string.h> #include <errno.h> #include <ctype.h>  #define max_line_len 1024  int main(int argc, char **argv)  {   int   line_number = 0;   file *input_file;   char  line[max_line_len + 1];   char *next_field;   char *end_field;    if (1 == argc)   {     fprintf(stderr, "usage: %s <filename>\n", argv[0]);     exit(1);   }    if (null == (input_file = fopen(argv[1], "r")))   {     perror(argv[1]);     exit(1);   }    fprintf(stdout, "student grades input file:\n");    while (fgets(line, sizeof(line), input_file))   {     int first = 1;      ++line_number;      (next_field = strtok(line, ",\n"); null != next_field; next_field = strtok(null, ",\n"))      {       errno = 0;        double grade = strtod(next_field, &end_field);        if (errno || end_field == next_field || *end_field)       {         fprintf(stderr, "warning: malformed input '%s'? skipping!\n", next_field);         continue;       }        fprintf(stdout, "%s%.1lf", (first ? "" : ", "), grade);       first = 0;        // todo: store grade (or @ least running min, max, avg) in structure stores assignments stats later processing , printing     }      if (!first)       fprintf(stdout, "\n");   }    return 0; } 

you'll need store stats each assignment in kind of structure can print out assignment stats after done reading in input.

note call strtok ",\n" rather " \n" believe comma important delimiter in input. @ least, that's typically true .csv files, common format delimiting data processed.

note, both of our approaches worrisome things on different kinds of input. example, tokenization wouldn't work if line looked "7.0,8.0,9.0,10.0\n", common in .csv files. program process 7.0 , ignore rest of line. program wouldn't object input "7.0,hithere 8.0, 9.0\n". again, program process "7.0 8.0 9.0, 10.0\n" same "7.0, 8.0, 9.0, 10.0\n" though commas missing former string. or bad? answer depends on input file format , how strict want on structure. former string, tokenization approach object 7.0 not being followed ',', '\n' or '\0' in original input, allow white space before number begins, strange asymmetric treatment of whitespace. blithely process "7.0,,,,10.0\n" without noticing "missing" fields on line.

so, need figure out how picky want , how of error handling want in program.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -