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...
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...
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...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix...
declare a struct named matrix and typedef the struct to type name Matrix. A mathematical matrix is a two-dimensional, rectangular data structure. The matrix struct should have three fields (in this order): an unsigned variable named "rows", an unsigned variable named "columns", and a pointer to a pointer to double (i.e. double**) named "data". (Code #including matrix.h should be able to declare Matrix variables.) In matrix.c, implement the create_matrix function. The Matrix should be filled with 0.0 values. data[i] should...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT