cs1713-test1-prog1.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2020, 2019, 2018, 2017 Girish M
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. *
  18. */
  19. /*
  20. Problem statement: https://www.isical.ac.in/~pdslab/2017/exams/labtest1.pdf
  21. */
  22. #include "common.h"
  23. typedef struct STUDENT{
  24. char rollNo[6];
  25. unsigned int atd_per;
  26. float agg_per;
  27. char fname[100];
  28. char lname[100];
  29. } STUDENT;
  30. int initStudentRecord(char* txtFile, STUDENT* st)
  31. {
  32. FILE* fp;
  33. char *line = NULL;
  34. size_t len = 80;
  35. size_t read;
  36. char* saveptr1, *saveptr2;
  37. int count = 0;
  38. const char* delim = " ";
  39. float highest_agg = 0.0;
  40. float lowest_agg = 100.0;
  41. char fnameHighest[100];
  42. char lnameHighest[100];
  43. char fnameLowest[100];
  44. char lnameLowest[100];
  45. if(NULL == (fp = fopen(txtFile,"r")))
  46. {
  47. printf("\nError opening file\n");
  48. }
  49. else
  50. {
  51. while((read = getline(&line, &len, fp)) != -1)
  52. {
  53. strcpy(st[count].rollNo, strtok(line, delim));
  54. st[count].atd_per = atoi(strtok(NULL, delim));
  55. st[count].agg_per = atof(strtok(NULL, delim));
  56. strcpy(st[count].fname,strtok(NULL, delim));
  57. strcpy(st[count].lname,strtok(NULL, delim));
  58. if(st[count].agg_per > highest_agg)
  59. {
  60. strcpy(fnameHighest, st[count].fname);
  61. strcpy(lnameHighest, st[count].lname);
  62. highest_agg = st[count].agg_per;
  63. }
  64. else if(st[count].agg_per <= lowest_agg)
  65. {
  66. strcpy(fnameLowest, st[count].fname);
  67. strcpy(lnameLowest, st[count].lname);
  68. lowest_agg = st[count].agg_per;
  69. }
  70. count++;
  71. }
  72. printf("\nStudent with highest aggregate percentage score: %s %s\n", fnameHighest, lnameHighest);
  73. printf("\nStudent with lowest aggregate percentage score: %s %s\n", fnameLowest, lnameLowest);
  74. }
  75. fclose(fp);
  76. return count;
  77. }
  78. int main(int argc, char* argv[])
  79. {
  80. if(argc == 2)
  81. {
  82. STUDENT st[30];
  83. if(initStudentRecord(argv[1], st) == 30)
  84. {
  85. }
  86. else
  87. printf("%s not in correct format", argv[1]);
  88. }
  89. else
  90. {
  91. printf("\nUsage: ./cs1713-test1-prog1.o file.txt\n");
  92. }
  93. return 0;
  94. }