SHARE
TWEET

Untitled

a guest Dec 15th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. struct student_info {
  6.     int student_counter, *student_AM; /*store students data with struct*/
  7.     char *student_names, *student_full_names;
  8.     double *grades;
  9. }info;
  10.  
  11. struct students {
  12.     int max_AM, min_AM;
  13.     char max_name, max_full_name, min_name, min_full_name;
  14.     double minimum_grade, maximum_grade, average_grade;
  15. }input;
  16.  
  17. int main(int argc,const char *array[]) {
  18.         if (strcmp("input.txt", array[1])==0) {
  19.             int i,j, position_of_minimum_grade, position_of_maximum_grade, min_AM, temp_AM;
  20.             char temp_names, temp_full_names, max_name, max_full_name, max_AM, min_name, min_full_name;
  21.             float temp_grades, minimum_grade, maximum_grade, average_grade, sum_grades=0;
  22.             FILE *f;
  23.             f=fopen("input.txt","r");
  24.             if (f==(FILE *)NULL) {
  25.                 printf ("check 3\n");
  26.                 return -1;
  27.             } else {
  28.                 printf ("check 2\n");
  29.                 fscanf(f, "%d\n", &(info.student_counter)); /*read studentsX_in file*/
  30.                 info.student_names=(char *)calloc(info.student_counter, sizeof(char));
  31.                 info.student_full_names=(char *)calloc(info.student_counter, sizeof(char));
  32.                 info.student_AM=(int *)calloc(info.student_counter, sizeof(int));
  33.                 info.grades=(double *)calloc(info.student_counter, sizeof(double));
  34.                 fscanf(f, info.student_names, info.student_full_names, info.student_AM, info.grades); /*read more info about studentsX_in file*/
  35.                 fclose(f);
  36.             }
  37.             minimum_grade=(info.grades[0]); /*find min of student grades*/
  38.             position_of_minimum_grade=0;
  39.             maximum_grade=info.grades[0];
  40.             position_of_maximum_grade=0;
  41.             for (i=0; i<info.student_counter; i++) {
  42.                 sum_grades+=info.grades[i]; /*find sum of students grades*/
  43.             }
  44.             for (i=1; i<info.student_counter-1; i++) {
  45.                 if (minimum_grade>info.grades[i]) {
  46.                     minimum_grade=info.grades[i];
  47.                     position_of_minimum_grade=i; /*find position of min*/
  48.                 }
  49.             }
  50.             min_name=info.student_names[position_of_minimum_grade]; /*put info of student who has min grade in variables before grade[] is classificated*/
  51.             min_full_name=info.student_full_names[position_of_minimum_grade];
  52.             min_AM=info.student_AM[position_of_minimum_grade];
  53.             for (i=1; i<info.student_counter-1; i++) { /*find max of students grades*/
  54.                 if (maximum_grade<info.grades[i]) {
  55.                     maximum_grade=info.grades[i];
  56.                     position_of_maximum_grade=i; /*find position of max*/
  57.                 }
  58.             }
  59.             max_name=info.student_names[position_of_maximum_grade]; /*put info of student who has max grade in variables before grade[] is classsificated*/
  60.             max_full_name=info.student_full_names[position_of_maximum_grade];
  61.             max_AM=info.student_AM[position_of_maximum_grade];
  62.             average_grade=sum_grades/info.student_counter; /*find average grade*/
  63.             for (i=1; i<info.student_counter-1; i++) { /**/
  64.                 for (j==info.student_counter-1; j>i+1; j--) {
  65.                     if (info.grades[j-1]>info.grades[j]) {
  66.                         temp_grades=info.grades[j-1];
  67.                         info.grades[j-1]=info.grades[j];
  68.                         info.grades[j]=temp_grades;
  69.                         temp_names=info.student_names[j-1]; /*correspondance with full names and A.M.*/
  70.                         info.student_names[j-1]=info.student_names[j];
  71.                         info.student_names[j]=temp_names;
  72.                         temp_full_names=info.student_full_names[j-1];
  73.                         info.student_full_names[j-1]=info.student_full_names[j];
  74.                         info.student_full_names[j]=temp_full_names;
  75.                         temp_AM=info.student_AM[j-1];
  76.                         info.student_AM[j-1]=info.student_AM[j];
  77.                         info.student_AM[j]=temp_AM;
  78.                     } else if (info.grades[j-1]==info.grades[j]) {
  79.                         if (info.student_AM[j-1]>info.student_AM[j]) {
  80.                             temp_AM=info.student_AM[j-1];
  81.                             info.student_AM[j-1]=info.student_AM[j];
  82.                             info.student_AM[j]=temp_AM;
  83.                             temp_names=info.student_names[j-1]; /*correspondance with full names*/
  84.                             info.student_names[j-1]=info.student_names[j];
  85.                             info.student_names[j]=temp_names;
  86.                             temp_full_names=info.student_full_names[j-1];
  87.                             info.student_full_names[j-1]=info.student_full_names[j];
  88.                             info.student_full_names[j]=temp_full_names;
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         } else if (strcmp("output.txt", array[2])==0) {
  94.             FILE *foo;
  95.             foo=fopen("output.txt", "w");
  96.             if (foo==(FILE *)NULL) {
  97.                 return -1;
  98.             } else {
  99.                 fprintf (foo, "max: %d %d %d %.2f\n",input.max_name, input.max_full_name, input.max_AM, input.maximum_grade); /*write studentsX_out file*/
  100.                 fprintf (foo, "min: %d %d %d %.2f\n", input.min_name, input.min_full_name, input.min_AM, input.minimum_grade);
  101.                 fprintf (foo, "average: %.2f\n", input.average_grade);
  102.                 fprintf (foo, "%d %d %d %.2f\n", *info.student_names, *info.student_full_names, *info.student_AM, *info.grades);
  103.                 fclose(foo);
  104.             }
  105.         }
  106.         return 0;
  107. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top