Академический Документы
Профессиональный Документы
Культура Документы
Functions - 01
TTTK1114
Note: Unless otherwise stated, assume every code segment provided in every question is
contained within the main { } function, complete with the necessary directives and the
statements using namespace std; and return 0;
1.
2.
3.
REVISION
Functions - 01
TTTK1114
int main()
{
int glob = 7;
func(::glob, glob);
cout<< glob << " , " << ::glob <<endl;
func(glob, ::glob);
cout<< glob << " , " << ::glob <<endl;
cout<<endl;
return 0;
}
4.
5.
Function convertTemp() that takes as input the temperature in Fahrenheit and returns
the value of temperature in Celsius.
b)
Function printTables() that takes as input the value n and prints the multiplication
table for n.
c)
Function convertGrade() that takes as input the mark and returns the grade (e.g 'A'
for 80-100, 'B' for 65 - 79, 'C' for 50 -64, 'D' for 40 - 49 or 'E' for 0 - 39)
d)
Function isEvenNum() that test whether a number is even and returning true if it is.
Write the definition of the following function calculateArea() that takes as input the
value width and height of a rectangle and returns the area of the rectangle.
float calculateArea(float width, float height)
6.
Write the definition of the following computeCircle() function that return the area a and
the circumference c of a circle with given radius r:
void computeCircle(float& a, float& c, float r)
7.
Write the definition of the following zakatCalculator() function that takes as input the
total income and total expenses and return the amount of zakat (which is based on 2.5% of the
total net income):
double zakatCalculator(doubletotal_profit, double total_loss)
8.
Write the function that computes the sum of the digits in an integer (max is 3 digits). Use the
following function header:
int sumDigits(intn)
For example, sumDigits(234) returns 9 (because 2+3+4=9).
9.
Rewrite the following reference parameter function using return value function.
void funcA (int& n)
{
n *= 2;
}
REVISION
Functions - 01
10.
TTTK1114
#include <iostream>
using namespace std;
const double PI = 3.14;
int main()
{
double radius = 0;
double length = 0,
double height = 0;
cout << This program can calculate the area of a rectangle,;
cout << the area of a circle, or a volume of a cylinder.;
cout << endl;
cout << To find the area of rectangle. << endl;
getLengthAndHeight(length, height);
cout << Area of rectangle = << rectangle(length, height);
cout << endl;
cout << To find the area of a circle. << endl;
getRadius(radius);
cout << Area of circle = << circle(radius);
cout << endl;
cout << To find the volume of a cylinder. << endl;
cout << Volume of cylinder = cylinder(radius, height);
cout << endl;
return 0;
}
REVISION
Functions - 01
TTTK1114
}
int main()
{
double x = 10;
cout << x is
func(x);
cout << x is
func2(x);
cout << x is
x = func3(x);
cout << x is
return 0;
REVISION
Functions - 01
TTTK1114
Answer:
1.
void function1(double n)
{
cout<< n;
}
void function2(int n, int m)
{
n += m;
function1(3.4);
}
int main()
{
int a = 2, b = 3;
function2(a, b);
return 1;
}
2.
The sin is 0.14112
The cos is 0.989992
3.
3
7
4
4
70
, 3
30
, 3
4.
a)
b)
c)
d)
5.
float calculateArea(float width, float height) {
return width * height;
}
6.
void computeCircle(float &a, float &c, float r) {
double PI = 3.14;
c = PI* 2 * r;
a = PI * r * r;
}
7.
double zakatCalculator(double total_profit, double total_loss)
{
return (0.025 * (total_profit total_loss));
}
8.
int sumDigits(int n)
{
int x, total=0;
while( n > 0)
{
if (n >= 10)
{
x = n%10;
n = n/10;
5
REVISION
Functions - 01
TTTK1114
total +=x;
}
else
{
total +=n;
n -= n;
}
}
return total;
}
int main()
{
int digit;
cout<< "enter max 3 digits of integer: " <<endl;
cin>> digit;
cout<< "sum of the digits is " <<sumDigits(digit);
cout<<endl;
return 0;
}
9.
int funcA (int n)
{
return n *= 2;
}
10.
void getLengthAndHeight (double &l, double &h)
{
cin >> l >> h;
}
void getRadius (double &r)
{
cin >> r;
}
double rectangle(double l, double h)
{
return (l*h);
}
double circle(double r)
{
return (PI * r * r);
}
double cylinder (double r, double h)
{
return (PI* r * r * h);
}
11.
X
X
X
X
is
is
is
is
10
12
12
10