In: Computer Science
Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:
Write a void function called printFruit that takes a fruitType parameter and prints the data (as shown in the example below).
Declare a variable of type fruitType to store the following data:
then use your printFruit function to print the data in the variable just created.
Write a function called getFruit that prompts the user to enter all the components of a fruitType and then returns that information in a fruitType (you can assume that the string data will not contain any spaces).
Create a new fruitType variable and use your getFruit to prompt the user for the fruit datga, then call printFruit to display the results.
For Example:
Your fruit is...
Fruit: banana
Color: yellow
Fat: 1
Sugar: 15
Carbohydrate: 22
Fruit: Apple
Color: Red
Fat: 11
Sugar: 22
Carbohydrate: 33
Your fruit is...
Fruit: Apple
Color: Red
Fat: 11
Sugar: 22
Carbohydrate: 33
You will need the string.h package in your program!
#include<stdio.h>
#include<string.h>
//create a fruitType data type using typedef
typedef struct fruitType_struct
{
char name[50];
char color[10];
int fat;
int sugar;
int carbohydrate;
}fruitType;
//define the printFruit() method toprint the fruit details
void printFruit(fruitType f)
{
printf("\n Your fruit is....");
printf("\n Fruit : %s",f.name);
printf("\n Color : %s",f.color);
printf("\n Fat : %d",f.fat);
printf("\n Sugar : %d",f.sugar);
printf("\n Carbohydrate : %d",f.carbohydrate);
}
//define getFruit() method to input the fruit details
fruitType getFruit()
{
fruitType f;
printf("\n Enter the name of Fruit");
scanf("%s",f.name);
printf("\n Enter the color of Fruit");
scanf("%s",f.color);
printf("\n Enter the value of fat");
scanf("%d",&f.fat);
printf("\n Enter the value of sugar");
scanf("%d",&f.sugar);
printf("\n Enter the value of carbohydrate");
scanf("%d",&f.carbohydrate);
return f;
}
//driver program
int main()
{
//declare the fruitType variable
fruitType f;
//call to getFruit() to input the fruit details
f=getFruit();
//call to printFruit() method to display the details
printFruit(f);
}
output;