#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;
}
Question: C Programming Problem: Pointer and Struct Description: The purpose of this assignment is to prov......
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 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 {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...
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; }; 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 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 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 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 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 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...