fork download
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define MAX_STUDENTS 100
  6.  
  7.  
  8. typedef struct {
  9. int id;
  10. char name[50];
  11. float cse103;
  12. float mat101;
  13. float eng101;
  14. float cgpa;
  15. } Student;
  16.  
  17.  
  18. float calculate_cgpa(float cse, float mat, float eng) {
  19. float total_credits = 4.5f + 3.0f + 3.0f;
  20. float weighted_sum = (cse * 4.5f) + (mat * 3.0f) + (eng * 3.0f);
  21. return weighted_sum / total_credits;
  22. }
  23.  
  24. int main() {
  25. Student students[MAX_STUDENTS];
  26. int count = 0;
  27.  
  28.  
  29. FILE *inputFile = stdin;
  30. if (inputFile == NULL) {
  31. printf("Error: Could not open student_input.txt file.\n");
  32. return 1;
  33. }
  34.  
  35.  
  36. while (count < MAX_STUDENTS &&
  37. fscanf(inputFile, "%d %s %f %f %f",
  38. &students[count].id,
  39. students[count].name,
  40. &students[count].cse103,
  41. &students[count].mat101,
  42. &students[count].eng101) == 5) {
  43.  
  44.  
  45. students[count].cgpa = calculate_cgpa(
  46. students[count].cse103,
  47. students[count].mat101,
  48. students[count].eng101
  49. );
  50. count++;
  51. }
  52. fclose(inputFile);
  53.  
  54. FILE *outputFile = stdout;
  55. if (outputFile == NULL) {
  56. printf("Error: Could not open output.txt file for writing.\n");
  57. return 1;
  58. }
  59.  
  60.  
  61. const char *border = "\n";
  62.  
  63. printf("%-5s %-10s %-8s %-8s %-8s %-6s\n", "ID", "Name", "CSE103", "MAT101", "ENG101", "CGPA");
  64. printf("%s", border);
  65.  
  66. fprintf(outputFile, "%-5s %-10s %-8s %-8s %-8s %-6s\n", "ID", "Name", "CSE103", "MAT101", "ENG101", "CGPA");
  67. fprintf(outputFile, "%s", border);
  68.  
  69.  
  70. for (int i = 0; i < count; i++) {
  71. printf("%-5d %-10s %-8.2f %-8.2f %-8.2f %-6.2f\n",
  72. students[i].id, students[i].name, students[i].cse103,
  73. students[i].mat101, students[i].eng101, students[i].cgpa);
  74.  
  75. fprintf(outputFile, "%-5d %-10s %-8.2f %-8.2f %-8.2f %-6.2f\n",
  76. students[i].id, students[i].name, students[i].cse103,
  77. students[i].mat101, students[i].eng101, students[i].cgpa);
  78. }
  79.  
  80. printf("%s", border);
  81. fprintf(outputFile, "%s", border);
  82.  
  83. fclose(outputFile);
  84. printf("\nSuccessfully processed %d.txt\n", count);
  85.  
  86. return 0;
  87. }
  88.  
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
ID    Name       CSE103   MAT101   ENG101   CGPA  

ID    Name       CSE103   MAT101   ENG101   CGPA