In C coding: 1) Create a program that will prompt the user for the number of kit
ID: 3812496 • Letter: I
Question
In C coding:
1) Create a program that will prompt the user for the number of kits they will be designing (for our purposes we expect this to be no more than 10, so your code should make sure the user enters a value between 1-10).
2) Prompt the user for the number of unique parts in each "kit" (for our purposes we are assuming that number will be less than 25, your code should handle the case where the user enters a value not in the range 1-25.)
3) Write a function that will prompt the user for the details about each part in a kit, populating a part struct with the details
4) After all the data is obtained from the user.
5) Write a function that will calculate the total cost of the kit write a function that will print the details of the kit in tabular form, including subcost for each item.
Your code should define and use the following structures :
Example of how the output should look like (A sample execution):-
Explanation / Answer
code:
#include <stdio.h>
struct part {
char description[20];
int qtyPerKit;
double costPerItem;
};
struct kit {
int numParts;
struct part components[25];
double kitCost;
}kits[10];
//Function prototypes
void kitDetails(struct kit [], int);
void printDetails(struct kit [], int);
int main(void)
{
int numKits,i;
while(1)
{
printf("Enter the number of kits you are creating(1-10): ");
scanf("%d",&numKits);
if(numKits < 1 || numKits >10) // Checking for legal value
printf("Kits should be between 1-10 ");
else
break;
}
for(i = 0; i <numKits; i++)
{
printf("Working on Kit %d ",i+1);
kitDetails(kits,i); // Details of kit
printDetails(kits,i); // printing details of kit
}
return 0;
}
void kitDetails(struct kit kits[], int num)
{
int numParts,i;
while(1)
{
printf("How many parts are in the kit?(1-25): ");
scanf("%d",&numParts);
if(numParts < 1 || numParts >25) //checking legal value
printf("Kits should be between 1-25 ");
else
break;
}
kits[num].numParts = numParts;
for(i = 0; i < numParts; i++)
{
printf("Enter the description of the part (<20 char, no spaces): ");
scanf("%s",&(kits[num].components[i].description));
printf("Enter number of %s in the kit: ",kits[num].components[i].description);
scanf("%d",&(kits[num].components[i].qtyPerKit));
printf("Enter cost of each %s: ",kits[num].components[i].description);
scanf("%lf",&(kits[num].components[i].costPerItem));
}
}
void printDetails(struct kit kits[], int num)
{
printf("Description Qty Cost Subtotal ");
int i;
double total = 0, subtotal;
for(i = 0; i < kits[num].numParts; i++)
{
subtotal = kits[num].components[i].costPerItem * kits[num].components[i].qtyPerKit;
total += subtotal;
printf("%s %d %.2lf $%.2lf ",kits[num].components[i].description, kits[num].components[i].qtyPerKit, kits[num].components[i].costPerItem, subtotal);
}
printf("Total cost for kit is $%.2lf ",total);
printf("-------------------------------------------------");
}