Question

Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov......

Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov...
C Programming Problem:
Pointer and Struct
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.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Item
{
char name[25];
size_t quantity;
double price;
};

void resizeArray(struct Item** SC, int* n)
{
   //printf("Resizing array\n");
   struct Item *temp = NULL;
   int i;
  
   temp = (struct Item *) malloc(*n * 2 * sizeof(struct Item));
   memcpy(temp, *SC, sizeof **SC * *n);
   struct Item** temp1=&temp;

   *n *= 2;
  
   free(*SC);
   printf("no mem");
   SC = temp1;
}

void add(struct Item* SC, int* n, int* i)
{
   int x=*i;
  
   if(x>*n)
   {
       printf("Resizing array\n");
       resizeArray(&SC,n);
       printf("Resized array\n");
   }
   printf("%d %d\n", x,*n);
   if(x<=*n)
   {
           x--;
       printf("Enter name of item\n");
       scanf("%s",SC[x].name);
   printf("Enter quantity of item\n");
       scanf("%d",&SC[x].quantity);
       printf("Enter price of item\n");
       scanf("%lf",&SC[x].price);
   }
  
  
  
   return;
}

void list(struct Item* SC, int* n, int* i)
{
   int j=0;
   while(j<*i)
   {
       printf("Item %d\n",j+1);
       printf("Name: %s\n",SC[j].name);
       printf("Quantity: %d\n",SC[j].quantity);
       printf("Price: %lf\n",SC[j].price);
       j++;
      
   }
}

int main()
{
int n;
printf("Enter the number of items you want to add in the shopping cart : \n");
scanf("%d",&n);
struct Item *shoppingCart=(struct Item*)malloc(n*sizeof(struct Item));
int op=0;
int count=0;
while(1)
   {
printf("MENU\n");
printf("1. Add an item to ShoppingCart\n");
printf("2. Print the current list of items\n");
printf("3. Quit the program\n");
scanf("%d",&op);
  
switch(op)
{
   case 1: count++;
           add(shoppingCart, &n, &count);
   break;
   case 2: list(shoppingCart, &n, &count);
   break;
   case 3: exit(0);
   break;
   default: printf("Enter valid input\n");
       }
}
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov......
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C programming. 1.Create a program that does the following - Creates three pointers, a character pointer...

    C programming. 1.Create a program that does the following - Creates three pointers, a character pointer professor, and two integer pointers student_ids, grades - Using dynamic memory, use calloc to allocate 256 characters for the professor pointer - Prompts the professor for their name, and the number of students to mark. - Stores the professor’s name using the professor pointer and in an integer the number of students to mark. - Using dynamic memory, use malloc to allocate memory for...

  • IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer...

    IN C PROGRAMMING, NOT C++ Rewrite the program you submitted for A8 to use pointers, pointer notation and arithmetic, etc. as follows: If you did not complete A8, you will need to start with those instructions, and then, change your code based on the instructions here The main() function should: Create a fixed or dynamic array, e.g., double grade[SIZE] or double *grade = malloc(...) or calloc(...) Concerning the function call for getData(), pass the address of the array and its...

  • In this exercise, you will make a struct that contains x and y coordinates: struct point...

    In this exercise, you will make a struct that contains x and y coordinates: struct point {int x; int y;}; The object of this exercise is to make a singly linked list of these point structures. Add one more member called "next" to your point struct. It is a pointer to the next member of the linked list. The next member of the last point struct should point to NULL, which indicates the end of the list. Your program will...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • C programming The program will require the following structure: struct _data { char *name; long number;...

    C programming The program will require the following structure: struct _data { char *name; long number; }; The program will require command line arguments: int main(int argv, char **argc) { Where argv is the number of arguments and argc is an array holding the arguments (each is a string). Your program must catch any case where no command line arguement was provided and print a warning message (see below). You MUST include/use the following functions, defined as follows: int SCAN(FILE...

  • Please need help, programming in C - Part A You will need to create a struct...

    Please need help, programming in C - Part A You will need to create a struct called Team that contains a string buffer for the team name. After you've defined 8 teams, you will place pointers to all 8 into an array called leaguel], defined the following way: Team leaguel8]. This must be an aray of pointers to teams, not an array of Teams Write a function called game) that takes pointers to two teams, then randomly and numerically determines...

  • C++: Array of contact info Design a contact struct, that takes your phone struct and address...

    C++: Array of contact info Design a contact struct, that takes your phone struct and address struct along with a c string for a name to create a record of contact information. ( C++: Design a Struct Design two structs address and phone Address has the following attributes: street address, city name, state code, zip code. phone has 3 numbers: area code, prefix and suffix test these by writing a program that creates one of each and fills them with...

  • ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

    ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store. Demonstrate the ability to store an array of Struct values on both the stack and the heap. Program Specifications: 1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements...

  • * C PROGRAMMING* Outcomes: Demonstrate the ability to create and use linked lists in dynamic memory...

    * C PROGRAMMING* Outcomes: Demonstrate the ability to create and use linked lists in dynamic memory Demonstrate the ability to add nodes and remove nodes from a linked list of structs Program Specifications: Write a program that creates the following struct (you can give it whatever name you want): char name[100]; int age; float weight; Create the following menu system: Add a Record Display All Records Quit When the user selects (1) you will prompt them for a name, age,...

  • Pointer Comparisons Assignment Write a small program called PointerComparisions LastName.cpp that • Declares an array of...

    Pointer Comparisons Assignment Write a small program called PointerComparisions LastName.cpp that • Declares an array of floats Name your array floatArray o Implicitly initialize your array with 4 numbers • Demonstrates memory address of arrays increase as the subscript increases o Use a for loop to compare each of the adjacent elements from first to last o Watch out for the bounds! • Demonstrates memory address of arrays decrease as the subscript decreases o Use a for loop to compare...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT