Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

schedule.txt Class Schedule Class Time Physics 15:30 Calculus 9:00 Biology 14:30

ID: 3853271 • Letter: S

Question

schedule.txt

Class Schedule
Class           Time
Physics           15:30
Calculus       9:00
Biology           14:30
Chemistry       11:30

2. High School Scheduler In this problem you will design and implement a program that tells you the time of each class The program will prompt the user for the class and you will output the time. For example, you will be provided with the following text file. Class Schedule: Class Physics Calculus Biology Study Hall Chemistrv lime 15:30 9:00 14:30 10:30 11:30 All of your code should be placed in one file, highSchoolScheduler.c; the program accepts the file name as a command line argument so that it is executable like this: ./highSchoolScheduler schedule.txt In the input text file the times will be in military times, and you will have to con- vert them to regular time. Your converted time should be displayed either in PM or AM Sample Output:

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
   FILE *fp;
   int th[5], tm[5];
   char apm[5][20];
   char temp[20], cl[20];

   int index;
   int hours, minutes;

   fp = fopen(argv[1], "r"); // opens the file in readonly mode
   if (fp == NULL)
   {
       printf ("Please provide a text file. ");
       return 0;
   }

   fscanf(fp, "%s", temp); // class is consumed
   fscanf(fp, "%s", temp); // schedule is consumed
   fscanf(fp, "%s", temp); // class is consumed
   fscanf(fp, "%s", temp); // time is consumed

   for (int i = 0; i < 5; i++)
   {
       fscanf (fp, "%s %d:%d", temp, &hours, &minutes);
       if (hours < 12)
       {
           th[i] = hours;
           tm[i] = minutes;
           strncpy(apm[i], "AM", 20);
       }

       else if (hours == 12)
       {
           th[i] = hours;
           tm[i] = minutes;
           strncpy(apm[i], "PM", 20);
       }

       else
       {
           th[i] = hours - 12;
           tm[i] = minutes;
           strncpy(apm[i], "PM", 20);
       }
   }

   fclose(fp);

   printf("What class do you want to search for? ");
   scanf ("%s", cl);
   if (strcmp(cl, "Physics") == 0)
   {
       index = 0;
   }
   else if (strcmp(cl, "Calculus") == 0)
   {
       index = 1;
   }
   else if (strcmp(cl, "Biology") == 0)
   {
       index = 2;
   }
   else if (strcmp(cl, "Study") == 0)
   {
       index = 3;
   }
   else if (strcmp(cl, "Chemistry") == 0)
   {
       index = 4;
   }

   // print the result
   printf("%s is at %d:%d %s ", cl, th[index], tm[index], apm[index]);

   return 0;
}

--------------------

OUTPUT:

What class do you want to search for? Physics
Physics is at 3:30 PM