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

The program will read a file with city information that includes city id, locati

ID: 3762543 • Letter: T

Question

The program will read a file with city information that includes city id, location(x,y) and city name; after reading the file the program will display a menu; using the menu, the user will choose two cities (either by id or name) and compute the linear (Euclidean) distance between the two chosen cities.

Given a file with the following format:

int float float string

For example:

1 42.4 73.45 Albany, N.Y.

2 35.05 106.39 Albuquerque, N.M.

3 35.11 101.5 Amarillo, Tex.

4 61.13 149.54 Anchorage, Alaska

5 33.45 84.23 Atlanta, Ga.

6 30.16 97.44 Austin, Tex.

Where

- Column 1 is the ID – Unique integer value. It may not be continuous

- Column 2 is X coordinate,

- Column 3 is Y coordinate,

- Column 4 is city name. It may include state/province and country

Write a program to

1) Read the data from the input file:

(a) Store the information using parallel arrays: one array for ID, one for X coordinates, one for Y coordinates and one for string names. Assume that the maximum number of entries(i.e. cities) is 1000.

2) Display a menu

a. Enter City 1

b. Enter City 2

c. Compute Distance

d. Exit

Make sure your options are letters( numbers are not allowed)

3) When option a) or b) are selected, the program should determine automatically if the user entered the city id or the city name

4) If option c) is chosen:

a. Find the index for each city, either using the id array or the city name array

b. Compute the linear distance

Explanation / Answer

Complete Program:

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

#define MAX 6

int main()
{
   int ids[MAX];
   float xcoords[MAX];
   float ycoords[MAX];
   char names[MAX][20];

   char* filename = "cities.txt";

   FILE *infile = fopen(filename, "r");

   if(infile == NULL)
   {
       printf("The %s file cannot be opened! ", filename);
         exit(EXIT_FAILURE);
   }

   int number;
   int count = 0;
  
   int status = fscanf(infile, "%d", &number);  
   while(status != EOF)
   {      
       ids[count] = number;
       fscanf(infile, "%f", &xcoords[count]);
       fscanf(infile, "%f ", &ycoords[count]);
       fgets(names[count], 20, infile);  
  
       count++;
       status = fscanf(infile, "%d", &number);  
   }

   printf("%-3s%8s%8s %-20s ", "ID", "X-coord", "Y-coord", "CityName");
   printf("--------------------------------------- ");
   for(int i = 0; i < count; i++)
   {
       printf("%-3d%8.2f%8.2f %-20s ", ids[i], xcoords[i], ycoords[i], names[i]);
   }

   float x1 = 0.0;
   float y1 = 0.0;
   float x2 = 0.0;
   float y2 = 0.0;
   float distance = 0.0;
   char option;
  
   do
   {
       printf(" -----MENU----- ");
       printf("a. Enter City 1 ");
       printf("b. Enter City 2 ");
       printf("c. Compute Distance ");
       printf("d. Exit ");
       printf("Choose an option: ");
       scanf("%c", &option);
      
       switch(option)
       {
           case 'a':
               printf("Enter the city id: ");
               scanf("%d", &number);

               while(number < 1 || number > count)
               {
                   printf("Enter the city id (1-%d): ", count);
                   scanf("%d", &number);
               }

               x1 = xcoords[number - 1];
               y1 = ycoords[number - 1];
               break;

           case 'b':
               printf("Enter the city id: ");
               scanf("%d", &number);

               while(number < 1 || number > count)
               {
                   printf("Enter the city id (1-%d): ", count);
                   scanf("%d", &number);
               }

               x2 = xcoords[number - 1];
               y2 = ycoords[number - 1];
               break;

           case 'c':
               distance = sqrt(pow((x2 - x1), 2.0) + pow((y2 - y1), 2.0));
               printf("Linear distance between (%.2f, %.2f) and (%.2f, %.2f) is %.2f ", x1, y1, x2, y2, distance);
               break;

           case 'd':
               printf("Thank you ");
               break;

           default:
               printf("Invalid option! ");
       }

   }while(option != 'd');

   fclose(infile);
   return 0;
}

Input File: cities.txt

1 42.4 73.45 Albany, N.Y.
2 35.05 106.39 Albuquerque, N.M.
3 35.11 101.5 Amarillo, Tex.
4 61.13 149.54 Anchorage, Alaska.
5 33.45 84.23 Atlanta, Ga.
6 30.16 97.44 Austin, Tex.