Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this C Programming Assignment Background Preparation: Review Blackboard slide on Pointers (under Course Content | General C Slides and Information), and

I need help with this C Programming Assignment

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago