Lab Description: The purpose of this assignment is to provide practice programmi
ID: 3683489 • 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
#include #include #include //structure Location struct Location{ char name[15]; char desc[50]; float latitude; float longitude; } **LocationArray; void printLocations(); // Function print locations void addLocation(); // Function for adding new location void ResizeArray(struct Location**); // Function used whenever the number of locations to be added to LocationArray would exceed the current bounds of the array int noOfLoc; // to store user choice number of locations int indx; // indx to keep track of no locations added int main() { int choice, srchIndx; char sname[15]; indx = 0; printf("How many locations would you like to be inside the array? "); int noOfLoc = 0; //variable for storing the size of the LocationArray scanf("%d", &noOfLoc); //gets the user input and stores in numerOfLoc LocationArray = (struct Location*)malloc(noOfLoc*sizeof(struct Location)); // allocating memory as per no of locations entered by user //Print the User menu do{ choice = 0; printf(" Location Information menu "); printf("1. Add additional locations. "); printf("2. Print the current list of locations. "); printf("3. Search for a Location. "); printf("4. Quit the program. "); printf("Enter your Choice : "); scanf("%d", &choice); if (choice == 1){ printf(" 1. Enter Location Information. "); addLocation(); // call the user defined function to enter one Loaction details }else if (choice == 2){ printf(" 2. List of locations "); printLocations(); // function call to print all locations }else if (choice == 3){ printf("Enter name of the Location : "); scanf("%s", sname); srchIndx = searchLoc(sname); // search function to find if location exists in the array if (srchIndx-- != 0) // if srchIndx is zero not found, otherwise function will return index of record (as 1 to n but not 0 to n) // if found print Location details printf("%s %s (%f,%f) ",LocationArray[srchIndx]->name,LocationArray[srchIndx]->desc,LocationArray[srchIndx]->latitude,LocationArray[srchIndx]->longitude); else printf("Loaction not found in LocationArray "); } else if (choice == 4){ printf("Thank you "); break; } else{ printf(" Please enter correct a menu value between 1 and 3 To continue press any key "); fflush(stdout); getch(); //system("cls");//clears the screen } }while(1); //Free the pointer free(LocationArray); return 0; } void ResizeArray(struct Location** locArray){ noOfLoc *=2; locArray = (struct Location*)malloc(noOfLoc*sizeof(struct Location)); //return locArray; }; // Function to add one Location details void addLocation(){ char name[15]; char desc[50]; float latitude; float longitude; // Promting user to enter Location details printf("Enter name of the Location : "); scanf("%s", name); printf("Enter Location Description : "); scanf("%s", desc); printf("Enter Latitude : "); scanf("%f", &latitude); printf("Enter Longitude : "); scanf("%f", &longitude ); //printf("Test : %s %s (%f,%f) ",name,desc,latitude,longitude); //getch(); if (indx >= noOfLoc) // if array is full ResizeArray(LocationArray); // Dynamic allocation of memory for storing Location details in the Array LocationArray[indx] = (struct Location*) malloc(sizeof(struct Location)); strcpy (LocationArray[indx]->name , name); strcpy (LocationArray[indx]->desc , desc); LocationArray[indx]->latitude = latitude; LocationArray[indx]->longitude = longitude; //printf( "%f %f ",LocationArray[indx].latitude,LocationArray[indx].longitude ); indx++; } // Function to print all Location details in the Location array void printLocations(){ int i; printf("List of Locations "); for (i=0; iname,LocationArray[i]->desc,LocationArray[i]->latitude,LocationArray[i]->longitude); } } //Function to search for Location name in the array, If location is found Index record is returned otherwilse zero is returned, // index of record (1,2,3,4,....n ,not 0,1,2,3......n) int searchLoc(char loc[15]){ int i; for (i=0; iname,loc)== 0){ return (i+1); // when loaction is found in array, returning Location posision in array break; } } return 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.