In C++ WE2.1 https://maryash.github.io/135/worked_examples/ch2_WE1.pdf WE2.2 htt
ID: 3742701 • Letter: I
Question
In C++
WE2.1 https://maryash.github.io/135/worked_examples/ch2_WE1.pdf
WE2.2 https://maryash.github.io/135/worked_examples/ch2_WE2.pdf
First, work through the Worked Examples WE2.1 and WE2.2
Next, your task is to design a program that models inflating a spherical balloon. First the balloon is inflated to have a certain diameter (which is provided as an input). Then inflate the balloon so that the diameter grows by an inch, and display the amount the volume has grown. Repeat that step: grow the diameter by another inch and show the growth of the volume. Hint:
The volume of a sphere is r 3 4 3 Assuming that the input is 10, the two outputs, when rounded to the nearest integer will be 173 and 208
1. Rearrange the following lines of code to produce correct pseudocode for this task:
growth = volume2 - volume1
volume2 = × diameter × diameter × diameter / 6
Print growth
volume3 = × diameter × diameter × diameter / 6
volume1 = × diameter × diameter × diameter / 6
growth = volume3 - volume2
Prompt for diameter and read user input
Print growth
diameter++
diameter++
2. Follow instructions in the link below to complete this assignment
http://wiley.code-check.org/files?repo=wiley&problem=ebook-bc-3-ch02-sec04-cc-1
Explanation / Answer
1.
#include
#include
#define PI 3.14159
using namespace std;
double getVolume(double diamtr) {
return (1.0 PI diamtr diamtr diamtr) / 6;
}
int main() { // main start
double growth; //growth
double diamtr; //diameter
double vol1, vol2; // volumes
cout << "Enter the diameter : ";
cin >> diameter;
for(int i=0; i<2; i++) {
vol1 = getVolume(diameter);//function call
diameter ++;
vol2 = getVolume(diameter);//function call
growth = vol2 - vol1;
cout << " Growth = " << round(growth);
}
}