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

Lab Description: The purpose of this assignment is to provide practice programmi

ID: 3683931 • Letter: L

Question

Lab Description:

The purpose of this assignment is to provide practice programming using structs and pointers. Your program will accept user input and store it in a dynamic array of structs. You will first define a structure to describe a location. Your program will request an array size from the user, allocate memory from the heap for an array called LocationArray, and then read input from the user for as many locations as the user wishes. If the user wishes to add more locations than the size of the array will allow, you should increase the size of the array and continue to add more locations. See below instructions for specific details.

Lab Specifications:

A. Define a struct appropriate for holding location information. At minimum, this should include a Location Name (a string containing no more than 15 characters), a Description (a string containing no more than 50 characters) and a Latitude and Longitude (both floating point values).

B. Ask the user for the number of locations, and use this number as the initial size of your LocationArray. (Hint: for easier testing, use a small number, such as two or three.)

C. Allocate an appropriate amount of memory from the heap to create a dynamic array (LocationArray) of Location structs, large enough to hold the number of locations given by the user.

D. Provide a menu that allows the user to choose among the following options:

1. Add additional locations to the Location Aray

2. Print the current list of locations (print all elements of each Location struct)

3. Quit the program

E. Create a function called "ResizeArray" to be used whenever the number of locations to be added to LocationArray would exceed the current bounds of the array. The user should not be aware that the size of the array is changing. Rather, s/he should simply be allowed to keep adding locations until s/he is done, and ResizeArray should be called (transparently to the user) whenever the number of locations to be added would exceed the bounds of the array so that the user may add as many locations as s/he likes. Each call to ResizeArray should double the size of the existing LocationArray. If by any chance all heap memory is exhausted, an appropriate error message should be issued to the user. Make sure you test your function by adding more locations than originally requested at the start of your program.

F. Be sure to include comments within your code that explain in high-level terms what the various parts of your code are doing.

G. In addition to those specifically required, use whatever functions, parameters and return statements you wish to organize your code and ensure that it works correctly.

Improvement:

Add a menu option and function to determine whether a certain location is in the array, The function will prompt the user to enter a location name, search the LocationArray to see if it contains that location, and if found, print the location's information to the screen. If the location is not in the array, an appropriate message should be displayed to the user.

Explanation / Answer

Answer:

Note: programming language is not mentioned. hence i used c++.

#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
using namespace std;
struct LocationArray
{
   string locationName;
   string locationdescription;
   float latitude;
   float longitude;
}*location;
void ResizeArray(LocationArray* &location, int &locationCnt, int &locArySize)
{
   LocationArray* newArray=new LocationArray[2*locationCnt];
   locArySize=2*locationCnt;
   for(int kk=0;kk<locationCnt;kk++)
   {
       newArray[kk]=location[kk];
   }
   delete[] location;
   location=newArray;
}

int main()
{
   int locArySize;
   cout<<"Entet location Array Size:"<<endl;
   cin>>locArySize;
   location =new LocationArray[locArySize];
   int locationCnt=0;
   string t,t2;
   while(1)
   {
       cout<<"1. Add location:"<<endl;
       cout<<"2. Print list "<<endl;
       cout<<"3. Exit"<<endl;
       int ch;
       cout<<" Ur choice:"<<endl;
       cin>>ch;
       if(ch==1)
       {
           if(locationCnt<locArySize)
           {
               cout<<"Enter location,description,latitude,longitude"<<endl;
               getline(cin,t);
               location[locationCnt].locationName=t;
               getline(cin,t2);
               location[locationCnt].locationdescription=t2;
               cin.ignore();
               cin>>location[locationCnt].latitude;
               cin>>location[locationCnt].longitude;
               locationCnt=locationCnt+1;
           }
           else
           {
               ResizeArray(location,locationCnt,locArySize);
              
           }
       }
       else if(ch==2)
       {
           for(int kk=0;kk<locationCnt;kk++)
           {
               cout<<"Location"<<location[kk].locationName<<endl;
               cout<<"Description"<<location[kk].locationdescription<<endl;
               cout<<"Latitude"<<location[kk].latitude<<endl;
               cout<<"Longitude:"<<location[kk].longitude<<endl;
           }
       }
       else
           exit(1);
   }
   system("pause");
   return 0;
}