Functional decomposition - radius, height and volume for cylinder

How is functional decomposition different than doing it "normally"? Anyways, I don't know how to start.

Question 1
Use functional decomposition, write C++ program that will prompt user to enter
the radius and height for a cylinder and then calculate the volume for the
cylinder and then display the height, radius and volume.
Use π = 3.14 and the formula for volume is : volume = π radius2
*height
You must use pass-by-reference mechanism to get the value for radius and height
using a single function, say getRadiusHeight().
Remember to add documentation for your function; what it does, preconditions and
postconditions.
I think that by "normally" your teacher means write everything in the main function -bad idea but works-
Using functional decomposition you can easily change the behavior of your program, because you have modules, where you can implement changes, instead of a fat main function.
This approach is based on dividing a problem in smaller subproblems.

for example:


Eyenrique-MacBook-Pro:Study Eyenrique$ ./example2
Enter a number: 4
4 is even
  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//example.cpp
//no functions.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main(){

int number;

cout<<"Enter a number: ";
cin>>number;

if(number%2==0)
	cout<<number<<" is even"<<endl;
else
	cout<<number<<" is odd"<<endl;

return 0; //indicates success
}//end main 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//example2.cpp
//divide and conquer.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

bool isEven(int number); //function prototypes
int setNumber();

int main(){

int number;

number=setNumber();

if(!isEven(number)) //if is not even
cout<<number<<" is odd"<<endl;
else
	cout<<number <<" is even"<<endl;


return 0; //indicates success
}//end main

int setNumber(){
	//if you want to change the way that a value is initialized 
	// -for example: numbers less than 100- you can change setNumber function behavior
	//instead of add conditions in main function.	
int number;
	cout<<"Enter a number: ";
	cin>>number;
return number;
}//end function setNumber

bool isEven(int number){ //if you want to verify if some number is even or odd
                        //twice or more
			//you only have to call isEven function
			//instead of repeat the code needed.
	if(number%2==0)
		return true;
	else
		return false;
}//end function isEven 
Last edited on
I've seen this a lot:

using std::cout;
using std::cin;
using std::endl;

but I don't think it is used in any of the class examples. What are those?
I believe you were probably taught

#include<iostream>
using namespace std;

it's the equivalent.
Topic archived. No new replies allowed.