Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SPECIFICATIONS: Given the following symbolic constants and structure declarations: #define PI 3.141593 #define NUM 8 struct Shape { char name [32] ; int length, width,

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
SPECIFICATIONS: Given the following symbolic constants and structure declarations: #define PI 3.141593 #define NUM 8 struct Shape { char name [32] ; int length, width, height, base, radius; double area, volume; Shape( ); // default constructor Shape (char type) ; // parameter overloaded constructor double computeAreaOrVolume( ); int displayShape( ); Q1. For the Shape class, write the code for the default constructor (a member function): Shape( ) that initializes all integer and double data members to e and sets the name to the string "unknown". Q2. For the Shape class, write the code for the parameter constructor (a member function): Shape (char type) that initializes all the data members to O and sets the name char array based on the value of the char parameter 'type' as follows: char : name : 'T sets name to "Triangle" sets name to "Sphere" "R' sets name to "Rectangle" "C sets name to "Cone" sets name to "unknown" for any other character other than 'T', 'S", 'R', or C'.Q3. For the Shape class, write the code for the member function: double computeAreaOrVolume( ) This function computes the area ONLY if the Shape's name is either "Triangle" or "Rectangle", otherwise the volume is computed using the correct formula based on whether the shape is a "Cone" or a "Sphere" (see below). If the name is "unknown" then this function does nothing and simply returns 0. 9ede. NOTE: You may use the stromp( ) function (prototyped in the string. h library) to perform string comparisons. The volume or area computed must be stored in the correct variable (area or volume) in the structure and must also be returned before the function ends. The formulas for computing area and volume are: Triangle: area = 1/2 x base x height Rectangle: area = length x width Cone : volume = 1/3 x PI x R^2 x H (* means exponent) Sphere: volume = 4/3 x PI x R*3 (* means exponent ) This function returns a double with the computed value of either the volume or the area. NOTE: DO NOT use the * symbol in your equation! It does not work as an exponent, but will not generate an error if used! ALSO: You MUST use the value of PI as represented by the symbolic constant PI already defined in the supplied main( ) function (ive. 3.141593) in your calculations. ALSO: Be careful with integer division! Recall what 1/2 evaluates to in C/C++. Q4. For the Shape class, write the code for the member function: int displayShape( ) This function displays the following information in EXACTLY the order listed below. value: name Followed by a newline (displays the string only). length followed by a newline (but only if the length is NOT @) (displays the integer only) width followed by a newline (but only if the width is NOT @) (displays the integer only) height followed by a newline (but only if the height is NOT @) (displays the integer only) base Followed by a newline (but only if the base is NOT @) (displays the integer only) radius followed by a newline (but only if the radius is NOT @) (displays the integer only) area Followed by a newline (but only if the name is either "Triangle" or "Rectangle") (displays the double only) volume followed by a newline (but only if the name is either "Sphere" or "Cone") (displays the double only) NOTE: The area or volume MUST be diplayed to EXACTLY 4 decimal places. This function returns the total number of length, width, height, base, and radius data members combined that ARE NOT EQUAL TO 8.MAIN PROGRAM: // Your solution may ONLY use functions from the following // included C/C++ library header files. #include #include #include #define PI 3.141593 #define NUM 8 struct Shape { char name [ 32]; int length, width, height, base, radius; double area, volume; // member functions: Shape ( ); // default constructor Shape (char T); // parameter overloaded constructor double computeAreaOrVolume ( ); int displayShape( ); struct Data { int length, width, height, base, radius; // global utility function void assignValues (struct Shape *, Data); int main( ) { (0 value sides) char types [NUM] = { 'C' li char names [NUM] [32] = { ' sphere "Rectangle", "unknown", "Triangle", "Sphere", "Cone", "Cone" }; int lengths [NUM] = { 2, 9, 12, 3 77, 18, 11 }, widths [NUM] = { 17, 6, 22, 9, 102, 16, 3, 27 ); int heights[NUM] = { 16, 61, 4, 19, 3, 27, 103, 47 ], bases [NUM] = { 90, 33, 12, 307, 85, 51, 9, 78 ]; int radii [NUM] = { 12, 8, 16, 31, 7, 15, 5, 45 ), i, j, notZero; int rvs[NUM] = { @ ), sides[NUM] = { 3, 4, 3, 3, 3, 4, 3, 3 ], okToGo = 1, sideTemp; double areas [NUM] = { 0.0900, 0.0ooo, 264.0909, 0.0ooo, 127. 50ed, 0.0ooo, 0.eone, 0.0one }; double volumes [NUM] = { 2412.7434, 2144.6608, 0.0one, e.0one, 0. 0one, 14137.1685, 2696.5340, 99657.0379 }; struct Shape *myShapes[NUM] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ]; struct Data myData [NUM] ; for (i=0; iname) != 0) { printf ("Error when storing the Shape's name; LOOP index Xd. . . program will stop here!'", i); ok ToGo = 0; 1 = NUM;if (okToGo) { rvs[i] = myShapes[i]->computeAreaOrVolume( ); if (types [i] => 'C' | | types[i] => 'S') { // checking volume if((int) rvs[i] != (int) volumes[i]) { printf( "Error when computing volume; LOOP index Xd. . . program will stop here!\\", i); i = NUM; ok ToGo = 0; else { // checking area instead if((int) rvs[i] != (int) areas[i]) { printf("Error when computing area; LOOP index *d. . . program will stop here!\\", i); printf( "your area:"d correct area:*d\\", (int)rvs[i], (int)areas[i]); i = NUM; ok ToGo = 0; if (okToGo) { printf(">>>>>>>23>>23>> >> >LAST TEST DISPLAY RESULT5>>>>>23723>>23>>> > >>\\0"); for (j=0; jdisplayShape( ); printf("index:"d number of non-zero integers: *d\\", j, notZero); 7 for (j=0; j length = d. length; p->width = d.width; p->height = d. height; p->base = d. base; p->radius = d. radius;/* Correct Program OUTPUT should be: 3232330230>303> > > > > >LAST TEST DISPLAY RESULTS>>>>32732737>323> > >> Cone 2 17 16 98 12 2412.7434 index:8 number of non-zero integers: 5 Sphere 9 6 61 33 2144.6608 index:1 number of non-zero integers: 5 Rectangle 12 22 12 16 264. 8089 index:2 number of non-zero integers: 5 unknown index:3 number of non-zero integers: 0 Triangle B 182 3 85 127. 5089 index:4 number of non-zero integers: 5 Sphere 17 16 27 51 15 14137. 1685 index:5 number of non-zero integers: 5 Cone 18 3 FAT 9 5 2696.5348 index:6 number of non-zero integers: 5 Cone 11 27 47 78 45 99667. 8379 index:7 number of non-zero integers: 5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

Why are Edge AI systems necessary for deep learning applications?

Answered: 1 week ago