Functions Solution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Introduction to Programming

Objective Part
Q 1. Consider the following C++ function:

int mystery(int num)


{
int y = 1;
if (num == 0)
return 1;
else if (num < 0)
return -1;
else
for (int count = 1; count < num; count++)
y = y * (num - count);
return y;
}
What is the output of the following statements?

a. cout << mystery(6) << endl;

Output: The function will calculate the factorial of 6, which is 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720.


So, the output will be: 720.

b. cout << mystery(0) << endl;

Output: The function will return 1 immediately, since num is 0. So, the output will be: 1.

c. cout << mystery(-5) << endl;

Output: The function will return -1 immediately, since num is negative. So, the output will be: -
1.

d. cout << mystery(10) << endl;

Output: The function will calculate the factorial of 10, which is 10! = 10 × 9 × 8 × 7 × 6 × 5 × 4
× 3 × 2 × 1 = 3628800. So, the output will be: 3628800.

Q 2. What is the output of the following program?

#include <iostream>
using namespace std;
int x;
void summer(int&, int);
void fall(int, int&);
int main() {
Introduction to Programming
int intNum1 = 2;
int intNum2 = 5;
x = 6;
summer(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
fall(intNum1, intNum2);
cout << intNum1 << " " << intNum2 << " " << x << endl;
return 0;
}
void summer(int& a, int b) {
int intNum1;
intNum1 = b + 12;
a = 2 * b + 5;
b = intNum1 + 4;
}
void fall(int u, int& v) {
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v;
}

Output:
15 5 6
15 24 -9

Q 3. What is the output of the following program?

#include <iostream>
using namespace std;
void tryMe(int& v);
int main()
{
int x = 8;
for (int count = 1; count < 5; count++)
tryMe(x);
return 0;
}
void tryMe(int& v)
{
static int num = 2;
if (v % 2 == 0) {
num++;
v = v + 3;
}
Introduction to Programming
else {
num--;
v = v + 5;
}
cout << v << ", " << num << endl;
}

Output:
11, 3
16, 2
19, 3
24, 2

Q 4. Consider the following function prototype:


void funcDefaultParam(double x = 7.3, int y = 4, char z = '*');
Which of the following function calls is correct?
a. funcDefaultParam();
b. funcDefaultParam(2.8);
c. funcDefaultParam(3.2, 0, 'h');
d. funcDefaultParam(9.2, '*');
e. funcDefaultParam(7, 3);

Solution:
 funcDefaultParam(); is incorrect because the function expects at least one argument,
even though it has default values.
 funcDefaultParam(2.8); is correct because it provides a value for x, and the default
values for y and z will be used.
 funcDefaultParam(3.2, 0, 'h'); is correct because it provides values for all three
parameters.
 funcDefaultParam(9.2, '*'); is incorrect because the second argument is a char, but the
function expects an int for y.
 funcDefaultParam(7, 3); is correct because it provides values for x and y, and the
default value for z will be used.
Introduction to Programming
Q 5. Consider following function definition

void defaultParam(int num1, int num2 = 7, double z = 2.5) {


int num3;
num1 = num1 + static_cast<int>(z);
z = num2 + num1 * z;
num3 = num2 - num1;
cout << "num3 = " << num3 << endl;
}

What is the output of the above mentioned function calls by providing input as:
a. defaultParam(7);
b. defaultParam(8, 2);
c. defaultParam(0, 1, 7.5);
d. defaultParam(1, 2, 3.0);

Solution:
 defaultParam(7); * num1 is 7, num2 is default (7), and z is default (2.5).
* num1 becomes 7 + 2 = 9. * z becomes 7 + 9 * 2.5 = 31.5. * num3 is 7 - 9 = -2. *
Output: num3 = -2
 defaultParam(8, 2); * num1 is 8, num2 is 2, and z is default (2.5). * num1 becomes
8 + 2 = 10. * z becomes 2 + 10 * 2.5 = 27. * num3 is 2 - 10 = -8. * Output: num3 = -8
 defaultParam(0, 1, 7.5); * num1 is 0, num2 is 1, and z is 7.5. * num1 becomes 0 + 7
= 7. * z becomes 1 + 7 * 7.5 = 53.5. * num3 is 1 - 7 = -6. * Output: num3 = -6
 defaultParam(1, 2, 3.0); * num1 is 1, num2 is 2, and z is 3.0. * num1 becomes 1 + 3
= 4. * z becomes 2 + 4 * 3.0 = 14. * num3 is 2 - 4 = -2. * Output: num3 = -2

Q 6. Describe scope rule in given table for following mentioned program

#include <iostream>
using namespace std;
const double RATE = 10.50;
int z;
double t;
void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);
int main(){
int num, first;
double x, y, z;
char name, last;
.
Introduction to Programming
.
.
return 0;
}
void one(int x, char y){
:
:
}
int w;
void two(int a, int b, char x) {
int count;
:
}
void three(int one, double y, int z) {
char ch;
int a;
.
.
.
//Block four
{
int x;
char a;
.
.
}//end Block four
.
.
.
}

Identifier Visibility in Visibility in Visibility in Visibility in block Visibility in


function one function two function three four main
RATE (before main) global global global global global
z (before main) global global global global global
t (before main) global global global global global
main - - - - local
local variables of main - - - - local
one (function name) - - - global
x (one’s formal local - - - -
parameter)
y (one’s formal local - - - -
parameter)
w (before function two) global global global global
Introduction to Programming
two (function name) - - - - global
a (two’s formal - local - - -
parameter)
b (two’s formal - local - - -
parameter)
x (two’s formal - local - - -
parameter)
local variables of two - local - - -
three (function name) - - - - global
one (three’s formal - - local - -
parameter)
y (three’s formal - - local - -
parameter)
z (three’s formal - - local - -
parameter)
ch (three’s local variable) - - local - -
a (three’s local variable) - - local - -
x (block four’s local - - - local -
variable)
a (block four’s local - - - local -
variable)

Q 7. Name various parts of the function in given boxes.

Solution:
Void = data type
{} = block
Length, width, area, perimeter = variables (parameters of a function)
areaAndPerimeter = function name.

Q 8. Select right answer


a. A variable for which memory is allocated at block entry and deallocated at block exit is called:
Automatic variable.
i. Static variable
Introduction to Programming
ii. Automatic variable
b. A variable for which memory remains allocated as long as the program executes is called a
Static Variable.
i. Static Variable
ii. Automatic Variable
c. Global variables are Static variables, and by default, variables declared within a block are
Automatic variables.
i. Static
ii. Automatic
iii. By reference
Q 9. Mark the following statements as true or false:
a. Value-returning function returns only one value. TRUE
i. True
ii. False
b. Parameters allow you to use different values each time the function is called. TRUE
i. True
ii. False
c. When a return statement executes in a user-defined function, the function immediately exits.
TRUE
i. True
ii. False
d. If a C++ function does not use parameters, parentheses around the empty parameter list are
still required. TRUE
i. True
ii. False
e. In C++, function definitions can be nested; that is, the definition of one function can be
enclosed in the body of another function. FASLE
i. True
ii. False
Q 10. Consider the following function:

int secret(int m, int n)


{
int temp = 0;
for (int i = 1; i < abs(n); i++)
temp = temp + i * m;
return temp;
}

What is the output if function is provided with following input?


i. cout << secret(3, 6) << endl;
Here, m = 3 and n = 6. The loop will run from i = 1 to i = 5 (since abs(n) = 6). The sum will
be: temp = 0 + 1*3 + 2*3 + 3*3 + 4*3 + 5*3 = 0 + 3 + 6 + 9 + 12 + 15 = 45
So, the output will be: 45.
Introduction to Programming
ii. cout << secret(5, -4) << endl;
Here, m = 5 and n = -4. The loop will run from i = 1 to i = 3 (since abs(n) = 4). The sum will
be: temp = 0 + 1*5 + 2*5 + 3*5 = 0 + 5 + 10 + 15 = 30
So, the output will be: 30.

Subjective Part
Q 1. Write the definition of a void function that takes as input two decimal numbers. If the first
number is nonzero, it outputs the second number divided by the first number; otherwise, it outputs a
message indicating that the second number cannot be divided by the first number because the first
number is 0.

Solution:

#include <iostream>
using namespace std;

float divideNUmber(float num1, float num2);

void divideNumber(float num1, float num2) {


float result;
if (num1 != 0) {
result = num2 / num1;
}
else {
cout << "the second number cannot be divided by the first number
because the first number is 0.";
}
cout << "Desire Result: " << result;
}

void main() {
float num1, num2 = 0;
cout << "Enter The value of Two Numbers: \n";
cin >> num1;
cin >> num2;
divideNumber(num1, num2);
}

Q 2. Write a program that uses the function isPalindrome. The function return true if value is
Palindrome, false otherwise.
a. Mention proper function declaration/prototype, and function call from main body.
b. Implement isPalindrome(char[] value, int size) function and specify the return type.

Output:
#include < iostream>
using namespace std;

bool isPalindrome(char[], int);


Introduction to Programming
int main() {
char value[] = "radar";
int size = sizeof(value) / sizeof(value[0]);
if (isPalindrome(value, size)) {
cout << "The string is a palindrome." << endl;
}
else {
cout << "The string is not a palindrome." << endl;
}
return 0;
}
bool isPalindrome(char value[], int size) {
int start = 0;
int end = size - 1;
while (start < end) {
if (value[start] != value[end]) {
return false;
}
start++;
end--;
}
return true;

Q 3. Write a program that implements isVowel that returns the value true if a given character is a
vowel and otherwise returns false. Mention proper function declaration/prototype, and function call
from main body.

#include <iostream>
using namespace std;

bool isVowel(char c);

bool isVowel(char c) {
c = tolower(c);
return(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

void main() {
char alphabat;
cout << "Enter Alphabat Here: \n";
cin >> alphabat;
if (isVowel(alphabat)) {
cout << "The Alphabat Is Vowel";
}
else {
cout << "The Alphabat Is not Vowel";
}
}
Q 4. Write a function, reverseDigit that takes an integer as a parameter and returns the number with
its digits reversed. For example, the value of reverseDigit(12345) is 54321; the value of
Introduction to Programming
reverseDigit(5600) is 65; the value of reverseDigit(7008) is 8007; and the value of reverseDigit(-
532) is -235.

#include <iostream>
using namespace std;

int reverseDigit(int num) {


int reversedNum = 0;
int sign = (num < 0) ? -1 : 1;
num = abs(num);
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
return sign * reversedNum;
}

int main() {
int num;
cout << "Enter The Number: \n";
cin >> num;
cout << "Reversed digit of Number is: " << reverseDigit(num) << endl;
return 0;
}
Q 5. During winter when it is very cold, typically, everyone would like to know the windchill factor,
especially, before going out. Meteorologists use the following formula to compute the windchill
factor, W:
W = 35.74 + 0.6215 * T - 35.75 * V 0.16 + 0.4275 * T * V0.16

Where V is the wind speed in miles per hour and T is the temperature in degrees Fahrenheit. Write a
program that prompts the user to input the wind speed, in miles per hour, and the temperature in degrees
Fahrenheit. The program then outputs the windchill factor. Your program must contain at least two
functions: one to get the user input and the other to determine the windchill factor.

#include <iostream>
#include <cmath>
using namespace std;

float getWindSpeed(float speed);


float getTemperature(float temp);
float calculateWindChill(float windspeed, float temperature);

float getWindSpeed(float speed) {


float windspeed;
cout << "Enter The Speed Of Wind IN Km/h: ";
cin >> windspeed;
return windspeed;
}

float getTemperature(float temp) {


float temperature;
cout << "Enter Temperature In Farenhiet: ";
Introduction to Programming
cin >> temperature;
return temperature;
}

float calculateWindChill(float windspeed, float temperature) {


float W;
W = 35.74 + 0.6215 * temperature - 35.75 * pow(windspeed, 0.16) + 0.4275 *
temperature * pow(windspeed, 0.16);
return W;
}

int main() {
float speed = 0;
float temp = 0;
float windspeed = getWindSpeed(speed);
float temperature = getTemperature(temp);
float WindChill = calculateWindChill(windspeed, temperature);
cout << "The WindChill Is: " << WindChill;
return 0;
}

Q 6. Write a function that takes as a parameter an integer (as a long value) and returns the number of
odd, even, and zero digits. Also write a program to test your function.

#include <iostream>
using namespace std;

void countDigits(long num, int& odd, int& even, int& zero) {


odd = 0;
even = 0;
zero = 0;
while (num > 0) {
int digit = num % 10;
if (digit == 0) {
zero++;
}
else if (digit % 2 == 0) {
even++;
}
else {
odd++;
}
num /= 10;
}
}

int main() {
long num;
cout << "Enter a long number: ";
cin >> num;
cout << endl;

int odd, even, zero;


countDigits(num, odd, even, zero);
Introduction to Programming
cout << "Number of odd digits: " << odd << endl;
cout << "Number of even digits: " << even <<endl;
cout << "Number of zero digits: " << zero <<endl;

return 0;
}
Q 7. Write a program that defines the named constant p, i.e. const double p = 3.1419 which stores the
value of p.

The program shall contain following functions:

I. surfaceArea which takes radius:r as input and return the value of 4pr2 , which is the surface
area of the sphere.
II. volume which takes radius:r as input and return the value of (4/3)pr3, which is the volume of the
sphere.

#include <iostream>
using namespace std;

const float p = 3.1419;

int surfaceArea(float r);


int volume(float r);

int surfaceArea(float r) {
return 4.0 * r * r * p;
}

int volume(float r) {
return (4.0 / 3.0) * p * r * r * r;
}

int main() {
float radius = 0;
cout << "Enter The Value OF Radius: \n";
cin >> radius;
float SurfaceareaValue = surfaceArea(radius);
float VolumeValue = volume(radius);

cout << "Value Of Surface Area Is: " << surfaceArea(radius) << endl;
cout << "Value Of Volume Is: " << volume(radius) << endl;
return 0;
}

You might also like