Question

In: Computer Science

The following is a structure template: struct box { char maker[40]; float height; float width; float...

The following is a structure template:

struct box

{

char maker[40];

float height;

float width;

float length;

float volume;

};

a. Write a function that has a reference to a box structure as its formal argument

and displays the value of each member.

b. Write a function that has a reference to a box structure as its formal argument

and sets the volume member to the product of the other three dimensions.

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>

struct box {
    char maker[40];
    float height;
    float width;
    float length;
    float volume;
};

// a. Write a function that has a reference to a box structure as its formal argument
//and displays the value of each member.
void print(struct box *b) {
    printf("Maker: %s\n", b->maker);
    printf("Height: %f\n", b->height);
    printf("Width: %f\n", b->width);
    printf("Length: %f\n", b->length);
    printf("Volume: %f\n", b->volume);
}

//b. Write a function that has a reference to a box structure as its formal argument
//and sets the volume member to the product of the other three dimensions.
void set_volume(struct box *b) {
    b->volume = b->height*b->width*b->length;
}

int main() {
    struct box b;
    strcpy(b.maker, "Maker");
    b.length = 2;
    b.width = 7;
    b.height = 5;
    set_volume(&b);
    print(&b);
    return 0;
}


Related Solutions

Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20];...
Create a structure in C called BarcelonaPlayer with the following members. struct BarcelonaPlayer { char name[20]; int age; char country[20]; char Position[20]; double Salary; double Rating; }; First, create an array of BarcelonaPlayer structures. Now, write a function that takes an array of BarcelonaPlayer structures as input and find out the highest paid player among all the players. void highestPaidPlayer(struct BarcelonaPlayer *pl, int size); Create another function that finds all the players from Argentina. void findPlayers(struct BarcelonaPlayer *pl, int size);
The length, width, and height of a box are measured as 5 ft, 4 ft, and...
The length, width, and height of a box are measured as 5 ft, 4 ft, and 8 ft, respectively, with an error in measurement of at most 0.3 ft in each. Use differentials to estimate the maximum error (in feet) in the calculated volume of the box.
The length ℓ, width w, and height h of a box change with time. At a...
The length ℓ, width w, and height h of a box change with time. At a certain instant the dimensions are ℓ = 4 m and w = h = 1 m, and ℓ and w are increasing at a rate of 8 m/s while h is decreasing at a rate of 2 m/s. At that instant find the rates at which the following quantities are changing. (a) The volume. (b) The surface area. (c) The length of a diagonal....
The length l, width w, and height h of a box change with time. At a...
The length l, width w, and height h of a box change with time. At a certain instant the dimensions are l = 2 m and w = h = 3 m, and l and w are increasing at a rate of 7 m/s while h is decreasing at a rate of 5 m/s. At that instant find the rates at which the following quantities are changing. (a) The volume. m3/s (b) The surface area. m2/s (c) The length of...
A wood burning stove is box shaped with a height of 30.7”, width 26.25” and depth...
A wood burning stove is box shaped with a height of 30.7”, width 26.25” and depth 25.5”. The front surface is glass (Ԑ= 0.9) and the other surfaces are cast iron (Ԑ= 0.7). The fire keeps the stove surface at a constant 202 °C. a.Calculate the rate at which the stove’s one glass surface (30.7” by 26.25”) radiates heat into the surrounding space. b.Calculate the rate at which the cast iron sides radiate heat into the surrounding space. Add this...
Implement the following function: a. float temp(float t, char ch) – this function takes temperature ‘t’...
Implement the following function: a. float temp(float t, char ch) – this function takes temperature ‘t’ and unit of temperature ‘ch’ as parameter. The variable ‘ch’ will be either f or c depending on whether the temperature in variable ‘t’ is in Fahrenheit or Celsius. The function returns the converted temperature to the main method. Call the above function in the main method. Initialize temperature and unit of temperature from the user and pass them as parameter to the above...
Program #include<iostream> using namespace std; struct bookRecord //structure definition {             //structure members definition char title[30];...
Program #include<iostream> using namespace std; struct bookRecord //structure definition {             //structure members definition char title[30];             char main_author[30];             int year_publish;             float price; }; int main() {             struct bookRecord book1; // declare structure variable;             //Get data from user             cout<<"Enter book title: ";             cin>>book1.title;             cout<<"Enter main author name: ";             cin>>book1.main_author;                     cout<<"Enter year publish: ";             cin>>book1.year_publish;             cout<<"Enter book price RM: ";             cin>>book1.price;             //Print the information the screen output            ...
Design a rectangular milk carton box of width w, length ll, and height h which holds...
Design a rectangular milk carton box of width w, length ll, and height h which holds 540 cm^3 of milk. The sides of the box cost 1 cent/cm^2 and the top and bottom cost 3 cent/cm^2. Find the dimensions of the box that minimize the total cost of materials used.
A rectangular box has a length of 1 in, width of 2 in, and height of 3 in. Find the cosine of the angle between the diagonal of the box and the diagonal of its base.
A rectangular box has a length of 1 in, width of 2 in, and height of 3 in. Find the cosine of the angle between the diagonal of the box and the diagonal of its base. what is the volume of the parallelepiped by <-5; 2; 1>, <1; -1; -3> and<-1; -1; -4>? (a) 9 (b) 10 (c) 11 (d) 12 (e) None of the above
Write  the following functions using the following structure for a point. struct Point { double x, y;...
Write  the following functions using the following structure for a point. struct Point { double x, y; }; (a) A function that passes a point and returns an integer (1, 2, 3, or 4) to indicate in which quadrant the point is located int findQuadrant(struct Point testpoint) { } (b) A function that passes two points and returns the distance between them. float distance(struct Point point1, struct Point point2) { } (c) A function that passes two points and generate the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT