123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- /*
- * Copyright (C) 2020, 2019, 2018, 2017 Girish M
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- */
- /*
- Problem statement: https://www.isical.ac.in/~pdslab/2017/exams/labtest1.pdf
- */
- #include "common.h"
- typedef struct STUDENT{
- char rollNo[6];
- unsigned int atd_per;
- float agg_per;
- char fname[100];
- char lname[100];
- } STUDENT;
- int initStudentRecord(char* txtFile, STUDENT* st)
- {
- FILE* fp;
- char *line = NULL;
- size_t len = 80;
- size_t read;
- char* saveptr1, *saveptr2;
- int count = 0;
- const char* delim = " ";
- float highest_agg = 0.0;
- float lowest_agg = 100.0;
- char fnameHighest[100];
- char lnameHighest[100];
- char fnameLowest[100];
- char lnameLowest[100];
- if(NULL == (fp = fopen(txtFile,"r")))
- {
- printf("\nError opening file\n");
- }
- else
- {
- while((read = getline(&line, &len, fp)) != -1)
- {
- strcpy(st[count].rollNo, strtok(line, delim));
- st[count].atd_per = atoi(strtok(NULL, delim));
- st[count].agg_per = atof(strtok(NULL, delim));
- strcpy(st[count].fname,strtok(NULL, delim));
- strcpy(st[count].lname,strtok(NULL, delim));
- if(st[count].agg_per > highest_agg)
- {
- strcpy(fnameHighest, st[count].fname);
- strcpy(lnameHighest, st[count].lname);
- highest_agg = st[count].agg_per;
- }
- else if(st[count].agg_per <= lowest_agg)
- {
- strcpy(fnameLowest, st[count].fname);
- strcpy(lnameLowest, st[count].lname);
- lowest_agg = st[count].agg_per;
- }
- count++;
- }
- printf("\nStudent with highest aggregate percentage score: %s %s\n", fnameHighest, lnameHighest);
- printf("\nStudent with lowest aggregate percentage score: %s %s\n", fnameLowest, lnameLowest);
- }
- fclose(fp);
- return count;
- }
- int main(int argc, char* argv[])
- {
- if(argc == 2)
- {
- STUDENT st[30];
- if(initStudentRecord(argv[1], st) == 30)
- {
- }
- else
- printf("%s not in correct format", argv[1]);
- }
- else
- {
- printf("\nUsage: ./cs1713-test1-prog1.o file.txt\n");
- }
- return 0;
- }
|