In: Computer Science
C program:
1, Make two parallel arrays, one to keep track of food names and
another to keep track of the food prices. There will be a total of
10 items, with a total of about 100 characters for each food name
title (i think we can use malloc for the strings and allocate it
accordingly)
Basically it will be something like a menu:
1, we will enter the food information
2, it will list all the food names and prices
3,then it will show a total value of the food inventory
Thanks!
To clarify, this is basically an inventory tracking. The user is the one inputting the food names, like for example the user inputs Donut and the price for Donut $3.00 that would be tracked, then the user enters 9 more items+ their prices(this is where the parallel array comes in). giving us 10 food items in the inventory.
****This requires some effort so please drop a like if you are satisfied with the solution****
Please go through comments for better understanding
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
//dynamic allocation of 2D char array to store food names as
strings
char **foodnames=(char**)malloc(10*sizeof(char*));
int i;
for(i=0;i<10;i++)
foodnames[i]=(char*)malloc(100*sizeof(char));
//dynamic allocation of double 1D array to store the prices of
fooditems
float *prices=(float*)malloc(10*sizeof(float*));
for(i=0;i<10;i++){
char * s = (char*)malloc(sizeof(char)*101);
char c;
printf("Enter Fooditem name: ");
float price;
scanf("%[^\n]%*c",s);
strcpy(foodnames[i], s);
printf("Enter Fooditem price: ");
scanf("%f",&price);
scanf("%c",&c);
prices[i]=price;
}
printf("\nInvertory:\n");
float sum;
for(i = 0; i < 10; i++){
printf("%10s $%.2f\n", foodnames[i],prices[i]);
sum += prices[i];
}
printf("Total Inventory Value: $%.2f",sum);
return 0;
}