In C coding: >Create a program that will prompt the user for the number of kits
ID: 3805303 • Letter: I
Question
In C coding:
>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).
>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.)
>Write a function that will prompt the user for the details about each part in a kit, populating a part struct with the details
>After all the data is obtained from the user.
>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 :
Explanation / Answer
#include<stdio.h>
struct part {
char description[20];
int qtyPerKit;
double costPerItem;
};
struct kit {
int numParts;
struct part components[25];
double kitCost;
};
void print_kit(struct kit k)//prints kits data
{
int i=0;
printf("Number of components in kit are : ",k.numParts);
while(i<k.numParts)
{
printf("Component %d Description : %s ",i+1,k.components[i].description);
printf("Component %d quantity per kit : %d ",i+1,k.components[i].qtyPerKit);
printf("Component %d cost per each item : %lf ",i+1,k.components[i].costPerItem);
i++;
}
printf("Total cost of the kit :%lf ",k.kitCost);
}
void calculate_totalcost(struct kit k)//calculating total_cost of each kit
{
int i=0;
double t=0;
while(i<k.numParts)
{
t=t+k.components[i].qtyPerKit * k.components[i].costPerItem;
i++;
}
k.kitCost = t;
}
struct kit kit_data(struct kit k)//taking user data...for each component in kit
{
int i=0;
while(i<k.numParts)
{
printf("Enter component %d (Description:max 20 chars):",i+1);
scanf("%s",&k.components[i].description[0]);
printf("Enter quantity of component %d per kit :",i+1);
scanf("%d",&k.components[i].qtyPerKit);
printf("Enter cost per item:");
scanf("%lf",&k.components[i].costPerItem);
i++;
}
return k;
}
int main()
{
int n;//varible declaration
while(1)//reading number of kits
{
printf("Enter number of kits[between 1 and 10]:");
scanf("%d",&n);
if(n>=1 && n<=10)
break;
else
printf("Invalid number Enter again ");
}
struct kit k[n];//declaring kit variable
int parts,i;
i=0;
while(i<n)
{
while(1)//reading number of unique parts
{
printf("Enter number of unique parts in kit %d [between 1 and 25]:",i+1);
scanf("%d",&parts);
if(parts>=1 && parts<=25)
break;
else
printf("Invalid number Enter again ");
}
k[i].numParts=parts;
i++;
}
i=0;
while(i<n)
{
printf("Enter kit %d ,component details..: ",i+1);
k[i]=kit_data(k[i]); //function to prompt each component data in kit...
i++;
}
//calculating each kit const
i=0;
while(i<n)
{
calculate_totalcost(k[i]);
i++;
}
//printing output
i=0;
while(i<n)
{
printf("kit %d ,details..: ",i+1);
print_kit(k[i]); //function to print kit details
i++;
}
return 0;
}
ouput:
Enter number of kits[between 1 and 10]:1
Enter number of unique parts in kit 1 [between 1 and 25]:1
Enter kit 1 ,component details..:
Enter component 1 (Description:max 20 chars):bat
Enter quantity of component 1 per kit :10
Enter cost per item:5
kit 1 ,details..:
Number of components in kit are :
Component 1 Description : bat
Component 1 quantity per kit : 10
Component 1 cost per each item : 5.000000
Total cost of the kit :0.000000
Process exited normally.
Press any key to continue . . .