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

The following time table is given for a train schedules: DISTACE (Miles) CITY TI

ID: 3765667 • Letter: T

Question

The following time table is given for a train schedules:

DISTACE (Miles)                             CITY                                      TIME (p.m.)

0                                                       Chicago                                     4:00

93                                                     Niles                                           6:42

141                                                    Kalamazo                                 7:28  

Write an Ansi C program to read all the above information using arrays then calculate the remaining information in the table shown below. You must also use function (s) inside your programming logic.

                                   

DISTACE (Miles)      CITY             TIME (p.m.)    Time from        Elapsed

                                                                                        Last Stop          Time

0                                  Chicago          4:00                     0 (min)              0 (min)

93                                Niles                6:42                     162                     162

141                              Kalamazo       7:28                     46                       208

Also:

Read the given table from an external file.

Not only you are displaying the interactive out put of the above tables, the same out put should be written to another external file as well. Display all the external files.   

Explanation / Answer

Complete Program:

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

#define SIZE 100

int main()
{
int distance[SIZE];
char city[SIZE][20];
int time_hours[SIZE];
int time_min[SIZE];
int time_from[SIZE];
int time_elapsed[SIZE];
int count = 0;

FILE *infile;
infile = fopen("schedules.txt", "r");
if(infile == NULL)
{
        printf("Input file cannot be opened. ");
        return -1;
    }

FILE *outfile;
outfile = fopen("table.txt", "w");

int dist;
char cty[20];
int hour;
int min;

while(fscanf(infile, "%d %s %d:%d", &dist, cty, &hour, &min) > 0 && count < SIZE)
{
  distance[count] = dist;
  strcpy(city[count], cty);
  time_hours[count] = hour;
  time_min[count] = min;

  if(count == 0)
  {
   time_from[count] = 0;
   time_elapsed[count] = 0;
  }
  else
  {
   time_from[count] = (time_hours[count] * 60 + time_min[count]) - (time_hours[count - 1] * 60 + time_min[count - 1]);
   time_elapsed[count] = time_elapsed[count - 1] + time_from[count];
  }

  count++;
}

printf("%-12s%-25s%-8s%7s%13s ", "DISTACE", "CITY", "TIME", "FROM", "ELAPSED");
fprintf(outfile, "%-12s%-25s%-8s%7s%13s ", "DISTACE", "CITY", "TIME", "FROM", "ELAPSED");
for(int i = 0; i < count; i++)
{  
  printf("%-12d%-22s%4d:%d", distance[i], city[i], time_hours[i], time_min[i]);
  fprintf(outfile, "%-12d%-22s%4d:%d", distance[i], city[i], time_hours[i], time_min[i]);

  if(time_min[i] < 10)
  {
   printf("%-3s", "0");
   fprintf(outfile, "%-3s", "0");
  }
  else
  {
   printf("%-3s", "");
   fprintf(outfile, "%-3s", "");
  }

  printf("%8d%12d ", time_from[i], time_elapsed[i]);
  fprintf(outfile, "%8d%12d ", time_from[i], time_elapsed[i]);
}

fclose(infile);
fclose(outfile);

return 0;
}

Output on external file: table.txt

Output on console:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote