Question
modify sample code from example two to calculate the surface area and volume of a sphere givin the radius

int main() Example 2 Introduction to structures and passing the structure variables as arguments into functions. 1. A structu
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screen Shot:

#include <iostream> #include <cmath> 2 using namespace std; 6 const double pi = M_PI; 8 // --- Adding three new functions for

// --- We are calling the functions as arguments as given in sample code messageSphere( 51.radius, sphere_surfaceArea(s1.radi

Program code to copy:

#include <iostream>
#include <cmath>

using namespace std;

const double pi = M_PI;

// --- Adding three new functions for calculating Surafce Area, Volume & one for showing message
// --- Function prototypes

double sphere_surfaceArea(double);
double spehre_volume(double);
void messageSphere(double, double, double);

// --- changing structure name to 'Sphere' & with three variables, for "radius", "surfaceArea" & "volume" respectively

struct Sphere{
  
   double radius;
   double surfaceArea;
   double volume;
};

int main(){
  
   // --- Creating an instance of Sphere
  
   Sphere s1 = {1, 0, 0};
  
   // --- Taking radius as input from user
  
   cout << "Please input a radius for your sphere: ";
   cin >> s1.radius;
  
   // --- We are calling the functions as arguments as given in sample code
  
   messageSphere(s1.radius, sphere_surfaceArea(s1.radius), spehre_volume(s1.radius));
  
   return 0;
}

// --- Implementing all functions

double sphere_surfaceArea(double r){
  
   return 4 * pi * r * r;   // calculating surface area
}

double spehre_volume(double r){
  
   return (4.0/3.0) * pi * r * r * r;   // calculating volume
}

void messageSphere(double radius, double surfaceArea, double volume){
  
   cout << endl << "Sphere characteristics using structures" << endl;
  
   cout << "Radius: " << radius << endl
   << "Surface Area: " << surfaceArea << endl
   << "Volume: " << volume << endl;
  
}

Sample output:

Please input a radius for your sphere: 2 Sphere characteristics using structures Radius: 2 Surface Area: 50.2655 Volume: 33.5

------------------------------------------------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERIES..............................................
HIT A THUMBS UP IF YOU DO LIKE IT!!!

Add a comment
Know the answer?
Add Answer to:
modify sample code from example two to calculate the surface area and volume of a sphere...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A) One of the problems that have been discussed in the class is to write a simple C++ program to ...

    C++ programming question will upvote A) One of the problems that have been discussed in the class is to write a simple C++ program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following int main //the radius of the...

  • A) One of the problems that have been discussed in the class is to write a...

    A) One of the problems that have been discussed in the class is to write a simple C++ program to determine the area and circumference of a circle of a given radius. In this lab you will be rewriting the program again but this time you will be writing some functions to take care of the user input and math. Specifically, your main function should look like the following: int main() { double radius; double area; double circumference; //the radius...

  • c++ Please help! i keep getting an error message. I think my main problem is not...

    c++ Please help! i keep getting an error message. I think my main problem is not understanding the circle calculations function and how both the area and the circumference is returned from the function. I know & needs to be used, but I am unsure how to use it. Copy the following code and run it. You should break it into the following 3 functions getValidinput - which asks the user to enter the radius and then make sure that...

  • C++ please Let's pretend that we can still go out to eat at a restaurant. Suppose...

    C++ please Let's pretend that we can still go out to eat at a restaurant. Suppose we have the job to make a program that accepts a dinner reservation. The information collected will be the name of the customer, the date for the reservation, the time for the reservation (good choice for a hierarchical struct), the number in the party, email address (for updates) and phone number. Define a hierarchical structure for a dinner reservation that will contain the above...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Please upload screenshots of the code. Thank you! P1 - Circle Area and Circumference functions.cpp, functions.h,...

    Please upload screenshots of the code. Thank you! P1 - Circle Area and Circumference functions.cpp, functions.h, P1.cpp The user enters 3 floating-pointer numbers which correspond to 3 radi. Store these numbers in an array. Then, define and prototype a function void area and cireffloat* area, float circumference, float array) which takes as input the array of circumferences of circles with the input radii. Think about why we need to pass pointers for both the area and the circumference variables. radii...

  • Help please: A Modular Function: You must rewrite the code to use a modular function that...

    Help please: A Modular Function: You must rewrite the code to use a modular function that can be used again by other modules. Your main program will call the function and pass the variables by value. Your function will take the appropriate action and return the required value . (Hint you will use additional variables in your revised code). The called function must be able to accept the data passed to it by the function doing the calling. Only after...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

  • Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume...

    Part 1: Use principles of inheritance to write code calculating the area, surface area, and volume of rectangular objects. First, write an abstract super class RectangularShape, then write a subclass Rectangle that inherits from the super class to compute areas of rectangles, including squares if there is only one data. Finally, write another subclass Cuboid that inherits from its super class Rectangle above to compute surface areas and volumes of cuboids, including 3 equal sided cube. Must apply code-reusability in...

  • In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk,...

    In C++ Amanda and Tyler opened a business that specializes in shipping liquids, such as milk, juice, and water, in cylindrical containers. The shipping charges depend on the amount of the liquid in the container. (For simplicity, you may assume that the container is filled to the top.) They also provide the option to paint the outside of the container for a reasonable amount. Write a program that does the following: Prompts the user to input the dimensions (in feet)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT