(Solved) : Use C Language Description Purpose Assignment Provide Practice Programming Using Structs P Q42745645 . . .

use c language

The purpose of this assignment is to provide practice programmingusing structs and pointers. Your program will accept user input andstore it in a dynamic array of structs. First, you will define astructure to describe a item to be bought. Your program will promptthe user for the initial size of the array. It will then allocatememory from the heap for an array called ShoppingCart. Once this isdone, your program will allow the user to enter data for as manyitems as s/he wishes. If the user wishes to add more items than theinitial size of the array will allow, you will use calls tomalloc() and memcpy() to increase the size of the array so that theuser can continue to add more items. See the instructions below forspecific details.


Define a struct (Item struct) appropriate for holding iteminformation. This should include an item name (a cstring containing25 characters), quantity (a size_t) and price (an double). You maytypedef 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 orthree.)

Allocate an appropriate amount of memory from the heap to create adynamic array (named ShoppingCart) of Item structs, large enough tohold the number of items entered by the user.

Provide a menu that allows the user to choose among the followingoptions:

  • Add an item to ShoppingCart – This will prompt the user toenter information about the item (Name, quantity, and price), andsave this in the next uninitialized element in ShoppingCart.
  • Print the current list of items – This will print all items inthe shopping cart
  • Quit the program

Create a function called “ResizeArray” to be used whenever thenumber of items to be added to ShoppingCart would exceed thecurrent bounds of the array. This function must returnvoid. The user should not be aware that the size of thearray is changing. Rather, s/he should simply be allowed to keepadding items until s/he is done, and ResizeArray should be called(transparently to the user) whenever the number of items to beadded would exceed the bounds of the array so that the user may addas many items as s/he likes. Each call to ResizeArray should doublethe size of the existing ShoppingCart. If by any chance all heapmemory is exhausted, an appropriate error message should be issuedto the user. Make sure you test your function by adding more itemsthan originally requested at the start of your program.

Be sure to include comments within your code that explain inhigh-level terms what the various parts of your code aredoing.

Other Specifications:

  • You may NOT use realloc() for this assignment.
  • With the exception of those specifically disallowed, usewhatever functions, parameters and return statements you wish toorganize your code and ensure that it works correctly.
  • Use valgrind to check the validity of your code.

Expert Answer

Answer to use c language Description: The purpose of this assignment is to provide practice programming using structs and pointers…

Leave a Comment


We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?