Question

In: Computer Science

Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:...

Create a typedef fruitType using the struct fruitType_struct to store the following data about a fruit:

  • name (string, up to 50 characters long)
  • color (string, up to 10 characters long)
  • fat (integer)
  • sugar (integer)
  • carbohydrate (integer)

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:

  • name: banana
  • color: yellow
  • fat: 1
  • sugar: 15
  • carbohydrate: 22

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!

Solutions

Expert Solution

#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;


Related Solutions

Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
Consider the following declaration: typedef struct{                                  &nbsp
Consider the following declaration: typedef struct{                                          int A;                                          char B[10];                                          float C;                                          char D;                                  }rectype;                                   typedef rectype matrix[121][4][5];                                   matrix A1; Compute the address of element A1[120][3][3] given the base address at 2000.
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
To store the information about a city and its weather forecast, create a struct that holds...
To store the information about a city and its weather forecast, create a struct that holds the city's name (string), population (int), and a forecast array storing 7 string elements representing the city's weather in the next 7 days. The program will then accept user input to enter the name and population of the city, then it accepts 7 string inputs to store into the forecast array. Then the program will output the city's name, population, and forecast data. Ex:...
Define a struct computerType to store the following data about a computer: Manufacturer (50 character string),...
Define a struct computerType to store the following data about a computer: Manufacturer (50 character string), model type (50 character string), processor type (10 character string), ram (int) in GB, hard drive size (int) in GB, year when the computer was built (int), and the price (double). Write a program that declares a variable of type computerType, prompts the user to the input data for the struct, and then outputs all the computer's data from the struct. For Example: Enter...
Create a struct for the following groups of data: (a) Product Data with the following members...
Create a struct for the following groups of data: (a) Product Data with the following members • ID • Name • Price • Quantity (b) Customer Data with the following members • ID • Name • Address • E-mail (c) Sales Data with the following members • Customer ID • Product IDs • Sale amount • Choose the types that you think will work best for the above descriptions. • Names should have a maximum size of 50. • Physical...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT