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
//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
//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)
returntrue;
elsereturnfalse;
}//end function isEven