Question: Urgent! Need Help ASAP with this C Programming Assignment. I need to turn this in tomorrow night Background Preparation: Review Blackboard slide on Pointers (under
Urgent! Need Help ASAP with this C Programming Assignment. I need to turn this in tomorrow night
Background Preparation:
Review Blackboard slide on Pointers (under Course Content | General C Slides and Information), and your class notes on structs. Also, use the example program malloc_in_function.c as a reference (found on Blackboard in: Course Content | Prof. Otten's Course Folder | Sample Code | Pointers). Review the manpages for malloc(), memcpy(), and sizeof().
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. First, you will define a structure to describe a item to be bought. 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 ShoppingCart. Once this is done, your program will allow the user to enter data for as many items as s/he wishes. If the user wishes to add more items 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 items. See the instructions below for specific details. Specifications: Define a struct (Item struct) appropriate for holding item information. This should include an item name (a cstring containing 25 characters), quantity (a size_t) and price (an double). You may typedef this struct to a new name if you wish. At the start of the program, ask the user for the number of items, and use this number as the initial size of your ShoppingCart. (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 ShoppingCart) of Item structs, large enough to hold the number of items entered by the user. Provide a menu that allows the user to choose among the following options:
Add an item to ShoppingCart - This will prompt the user to enter information about the item (Name, quantity, and price), and save this in the next uninitialized element in ShoppingCart.
Print the current list of items - This will print all items in the shopping cart
Quit the program
Create a function called "ResizeArray" to be used whenever the number of items to be added to ShoppingCart would exceed the current bounds of the array. This function must return void. The user should not be aware that the size of the array is changing. Rather, s/he should simply be allowed to keep adding items until s/he is done, and ResizeArray should be called (transparently to the user) whenever the number of items to be added would exceed the bounds of the array so that the user may add as many items as s/he likes. Each call to ResizeArray should double the size of the existing ShoppingCart. 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 items 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.
Use valgrind to check the validity of your code.
Testing using Valgrind:
It is time to get into the practice of testing your programs on zeus using valgrind. Valgrind is a suite of programs that you can use to improve your programs. For this assignment, you will be using a tool called Memcheck. Read a quick-start tutorial at this link: http://valgrind.org/docs/manual/quick-start.html Also read the Overview (section 4.1) for Memcheck at this link: http://valgrind.org/docs/manual/mc-manual.html and skim sections 4.2 and 4.3. Once you have your program working, use valgrind with the --leak-check=yes option to check for and correct any issues that are flagged. Don't forget to use the -g and -O0 (that's oh-zero) options. Your Makefile should contain these options so that it will compile with them automatically Make sure that the Heap, Leak and Error Summaries using valgrind show that there are no errors or issues. Then, to show what might happen if you make an error (such as creating a memory leak), comment out the "free(p);" statement in one or more of your source files and recompile. Then run valgrind again. Once you understand how valgrind catches errors and makes you aware of them, uncomment the lines of code, recompile and check again so that there are no errors.
Please help me quickly
This is what I have so far
#include #include typedef struct Item { char name[25]; size_t quanitiy; double price; struct Item *next; }Item; void printListOfItems(Item *head) { Item *current = head; while(current != NULL) { printf("%s Quantity: %zu $%.2f ",current->name, current->quanitiy, current->price); current = current -> next; } } void insert(Item *head, char *n, size_t q, double p) { while(head -> next != NULL) { head = head -> next; } //head -> next = (Item *)malloc(sizeof(Item)); head = head -> next; for(int i = 0; i < sizeof(n); i++) head -> name[i] = n[i]; head -> quanitiy = q; head -> price = p; head -> next = NULL; } void resizeArray() { Item *array = (Item*)malloc(2*sizeof(Item)); if(array != NULL) { return; } } int main() { char buf[100]; char answer = '\0'; int numOfItems = 0; int countNumOfItems = 0; Item *start,*temp; start = (Item *)malloc(sizeof(Item)); temp = start; temp -> next = NULL; printf("How many items would you like to enter?: "); fgets(buf,20,stdin); sscanf(buf,"%d",&numOfItems); while(1) { char name[25]; size_t quantity; double price; printf("What would you like to do?: Press 'A' to add an item to the Shopping cart Press 'P' to print the list Press 'Q' to quit the program "); fgets(buf,100,stdin); sscanf(buf,"%c",&answer); if(answer == 'A') { //printf("sizeof(item) = %lu ",sizeof(temp)); if(numOfItems <= countNumOfItems) { printf("Resize Array "); resizeArray(); } printf("Enter item information: "); fgets(buf,100,stdin); sscanf(buf,"%s",name); fgets(buf,100,stdin); sscanf(buf,"%zu",&quantity); fgets(buf,100,stdin); sscanf(buf,"%lf",&price); insert(start,name,quantity,price); printf("") } else if(answer == 'P') { printf("Here is the Item list: "); printListOfItems(start->next); printf(" "); } else if(answer == 'Q') { printf("Exiting Program "); exit(0); } else { printf("Invalid Choice. Try again "); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
