Вы находитесь на странице: 1из 6

REVISION

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.

Identify and correct the errors in the following program:


int function1(int n)
{
cout<< n;
}
function2(int n, m)
{
n += m;
function1(3.4);
}
int main()
{
int a = 2, b = 3;
function2(a, b);
return 1;
}

2.

What is the output of the following code:


void getSinCos(double dx, double &dSin, double &dCos)
{
dSin = sin(dx);
dCos = cos(dx);
}
int main()
{
doubledSin = 0.0;
doubledCos = 0.0;
getSinCos(3.0, dSin, dCos);
cout<< The sin is <<dSin<<endl;
cout<< The cos is <<dCos<<endl;
return 0;
}

3.

What is the output of the following code:


#include <iostream>
using namespace std;
int glob = 10;
void func(int&x, int y)
{
x = x - y;
y = y * 10;
cout<< x << " " << y <<endl;
}

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.

Write appropriate function prototype for each of the followings descriptions:


a)

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

Give the following C++ program.

#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;
}

a. Define a function getLengthAndHeight()to get the value of length and height.


b. Define a function rectangle(). The area of rectangle is given by multiplying the
width by the length.
c. Define a function getRadius() to get the value of radius.
d. Define a function circle ().The area of circle is (
) where r is the radius of the
circle.
e. Define a function cylinder(). The volume of cylinder is (
h) where r is the
radius and h is the height.
11.

What is the output of the following code:


void func(double &p)
{
p += 2;
}
void func2(double p)
{
p = 0;
}
double func3(double p)
{
return (p-2);
3

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;

<< x << endl;


<< x << endl;
<< x << endl;
<< x << endl;

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)

float convertTemp(float fahr);


void printTables(int n);
char convertGrade(float mark);
bool isEvenNum(intnum);

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

Вам также может понравиться