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

Program #1 The program will read a file with city information that includes city

ID: 3764052 • Letter: P

Question

Program #1

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.

Table 1: File Format

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.(20 points)

2) Display a menu (10 points ): a. Enter City 1 b. Enter City 2 c. Compute Distance d. Exit Notice: o 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 (20 points)

4) If option c) is chosen: a. Find the index for each city, either using the id array(10 points) or the city name array(35 points) b. Compute the linear distance (5 points)

Deliverables You will turn in C source code file. The C source code file should:

1) Comply with all of the formatting requirements already discussed.

) Query the user(input)

3) Display the output

4) Perform the operations described

As for the text file............

Thank you for your help in this!

Explanation / Answer

// NOTE : use -lm while compile the program to link math libraries

//gcc -o cities.c -lm

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

// function to calculate linear distance between two co-ordinates
double calculateDistance(double x1,double y1,double x2,double y2)
{
   double distance;
   // formula to calculate Euclidean distance
   distance= sqrt(pow((x2-x1),2)+pow((y2-y1),2));
   return distance;
}

int main()
{
   int cityID[1000];
   double X_coordinate[1000];
   double Y_coordinate[1000];
   char *cityName[1000];
   int id;
   char *city;
   int index=0, size=0;
   int exitMenu = 0, flag = 0;
   char option;
   double distance, x1, y1, x2, y2;
  
   char* filename = "cities.txt";
   FILE *fp;
   fp = fopen(filename,"r");
   if(!fp)
   {
       // print error and exit if file is not accessible
       printf("Not able to open data file ");
       return 0;
   }
   // read city id, x=coordinate, y-coordinate and city name
   while(fscanf(fp,"%d",&id)!=EOF)
   {
       fscanf(fp,"%lf",&x1);
       fscanf(fp,"%lf",&y1);
       fgets(city,25,fp);
       cityID[size] = id;
       X_coordinate[size] = x1;
       Y_coordinate[size] = y1;
       cityName[size] = malloc(25);
       strcpy(cityName[size],city);
       size++;
   }
   // close file
   fclose(fp);
  
   // loop until exit is selected in menu
   while(!exitMenu)
   {
       // print menu/options
       printf("-----------Menu----------- ");
       printf("a. Enter City 1 ");
       printf("b. Enter City 2 ");
       printf("c. Compute Distance ");
       printf("d. Exit ");
       printf("Enter your option : ");
       // read option as a char
       scanf("%c",&option);
      
       switch(option)
       {
           case 'a':
               // ask user to enter city id / city name
               printf("Enter a City id or name(City name should be entered with State/Province) : ");
               // read input as string
               scanf("%s",city);
               // determine if input is string or integer by checking each digit
               for(index=0;city[index] != '';index++)
               {
                   flag = isdigit(city[index]);
                   if(!flag)
                       break;
               }
               // if input is integer
               if(flag)
               {
                   // convert string to integer
                   id=atoi(city);
                   // search for id in cityID array
                   for(index=0;index<size;index++)
                       if(cityID[index]==id)
                       {
                           // if found, copy x and y co-ordinates to x1 and y1
                           x1 = X_coordinate[index];
                           y1 = Y_coordinate[index];
                           break;
                       }
               }
               else
               {
                   // else, search in cityName array
                   for(index=0;index<size;index++)
                       if(strcmp(city, cityName[index])==0)
                       {
                           // if found copy x and y co-ordinates to x1 and y1
                           x1 = X_coordinate[index];
                           y1 = Y_coordinate[index];
                           break;
                       }
               }
               break;
           case 'b':
               // same as case a but copy x and y co-ordinates to x2 and y2.
               printf("Enter a City id or name(City name should be entered with State/Province) : ");
               scanf("%s",city);
               for(index=0;city[index] != '';index++)
               {
                   flag = isdigit(city[index]);
                   if(!flag)
                       break;
               }
               if(flag)
               {
                   id=atoi(city);
                   for(index=0;index<size;index++)
                       if(cityID[index]==id)
                       {
                           x2 = X_coordinate[index];
                           y2 = Y_coordinate[index];
                           break;
                       }
               }
               else
               {
                   for(index=0;index<size;index++)
                       if(strcmp(city, cityName[index])==0)
                       {
                           x2 = X_coordinate[index];
                           y2 = Y_coordinate[index];
                           break;
                       }
               }
               break;
           case 'c':
               // call calculateDistance() with co-ordinates copied in case a and case b
               distance = calculateDistance(x1,y1,x2,y2);
               //print the calculated distance
               printf("Distance between two cities selected is %lf",distance);
               break;
           case 'd':
               // set exitMenu to 1 to exit while loop
               exitMenu = 1;
           default:
               // if it is other than valid options print error message
           printf("Invalid option. Select you option again ");
   }
   }
   return 0;
}

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