Question
The purpose of this assignment is to provide practice programming using structs and pointers in C programming language. Your program will accept user input and
The purpose of this assignment is to provide practice programming using structs and pointers in C programming language. Your program will accept user input and store it in a dynamic array of structs. First, you will define a structure to describe a location. Your program will prompt the user for the initial size of the array. It will then allocate memory from the heap for an array called LocationArray. Once this is done, your program will allow the user to enter data for as many locations as s/he wishes. If the user wishes to add more locations than the initial size of the array will allow, you will use calls to malloc() and memcpy() to increase the size of the array so that the user can continue to add more locations. See the instructions below for specific details. Specifications: Define a struct appropriate for holding location information. At minimum, this should include a Location Number (an integer), ID (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). 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.) Allocate an appropriate amount of memory from the heap to create a dynamic array (named LocationArray) of Location structs, large enough to hold the number of locations entered by the user. Provide a menu that allows the user to choose among the following options:
Add a location to LocationArray - This will prompt the user to enter information about the location (ID, Description, Lat/Lon), and save this in the next uninitialized element in LocationArray.
Print the current list of locations - This will print all elements of each initialized Location struct)
Quit the program
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. Be sure to include comments within your code that explain in high-level terms what the various parts of your code are doing. Other Specifications:
You may NOT use realloc() for this assignment.
With the exception of those specifically disallowed, use whatever functions, parameters and return statements you wish to organize your code and ensure that it works correctly.
This is what I have done so far:
#include
#include
#include
// struct for location information
typedef struct
{
int locationNum;
char id[15];
char description[50];
float latitude;
float longitude;
} localInfo;
void printMenu();
void getUserInput(char input);
void resizeArray();
int main()
{
int numOfLocation;
char buffer[25]; // to hold input from user
printf("Enter the number of locations ");
fgets(buffer, 25, stdin);
sscanf(buffer, "%d", &numOfLocation);
printf("The number you have enter for the location is ->%d ", numOfLocation); // testing, remove later
localInfo *locationArray; // declaring locationArray as a point to struct localInfo
locationArray = (localInfo *) malloc((numOfLocation + 5) * sizeof(localInfo));
printMenu();
char userInput;
fgets(buffer, 25, stdin);
sscanf(buffer, "%c", &userInput);
getUserInput(userInput);
}
void printMenu()
{
printf("Add a location to LocationArray ------- A ");
printf("Print the current list of locations --- P ");
printf("Quit the program ---------------------- Q ");
}
// this will get the user input for the menu displayed
void getUserInput(char input)
{
switch(input)
{
case 'A':
case 'a':
printf("CASE A");
break;
case 'P':
case 'p':
printf("CASE P");
break;
case 'Q':
case 'q':
printf("Good Bye! ");
exit(0);
break;
default:
printf("Invalid Choice, Please Try Again ");
main();
}
}
void resizeArray()
{
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started