0% found this document useful (0 votes)
16 views

Worked Problems On Functions

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Worked Problems On Functions

Uploaded by

beshahashenafe20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Addis Ababa University

Department of Computer Science


Solved Problems on Functions
1. Give answers to the following questions
a) Write the prototype for the function called calculatePayCheck that takes two arguments
pay rate and total time worked and computes and prints the total pay.
b) Within the main function, read from the keyboard the total time worked and the pay rate
c) Within the main function, call the calculatePayCheck function on the inputs in part b
above.

Solution:
#include <iostream>
using namespace std;
// Function prototypes
void calculatePayCheck(float, int);

int main()
{
float payRate;
int hours;
cout << "Welcome to the Payroll Program." << endl;
cout << endl << "Please input the pay per hour." << endl;
cin >> payRate;
cout << endl << "Please input the number of hours worked."
<< endl;
cin >> hours;
cout << endl << endl;
calculatePayCheck(payRate, hours); // Call to the calPaycheck
//function

cout << "We hope you enjoyed this program." << endl;
return 0;
}

1
// calculatePayCheck
// Task: This function computes and outputs gross pay
// Data in: rate and time

void calculatePayCheck(float rate, int time)


{
float gross;
gross = rate * time;
cout << "The pay is " << gross << endl;
}
2. The following template program demonstrates the scope rules. Give answer to the questions
asked inside the template program.

#include <iostream>
#include <iomanip>
using namespace std;
const double PI = 3.14;
const double RATE = 0.25;

void findArea(float, float&);


void findCircumference(float, float&);

int main()
{

float radius = 12;

cout <<" Main function outer block" << endl;


cout <<" LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

Answer:
The identifier that are active at this point are:
o PI and Rate since they are global variables
o radius since it local variable

{
float area;
cout << "Main function first inner block"
<< endl;
cout << "LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

2
Answer:
The identifier that are active at this point are:
o PI and Rate since they are global variables
o radius since it local variable
o area

/// Fill in the code to call findArea here

Answer:
FindArea(radius,area)

cout << "The radius = " << radius << endl;


cout << "The area = " << area << endl << endl;
}

{
float radius = 10;
float circumference;

cout << "Main function second inner block" << endl;


cout << "LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

Answer:
The identifier that are active at this point are:
o PI and Rate since they are global variables
o radius since it local variable
o circumference

/// Fill in the code to call findCircumference here

cout << "The radius = " << radius << endl;


cout << "The circumference = "
<< circumference << endl << endl;
}

cout << "Main function after all the calls" << endl;
cout << "LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

return 0;

3
}

// findArea
//
// task: This function finds the area of a circle given its
//radius
// data in: radius of a circle
// data out: answer (which alters the corresponding actual
parameter)
//
//
***********************************************************
*********

void findArea(float rad, float& answer)


{
cout << "AREA FUNCTION" << endl << endl;
cout << "LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

/// Fill in the code, given that parameter rad


// contains the radius, that

Answer: ///
answer = PI * rad * rad; will
find
the area to be stored in answer

// findCircumference
//
// task: This function finds the circumference of a circle
//given its radius
// data in: radius of a circle
// data out: distance (which alters the corresponding
//actual parameter)

void findCircumference(float length, float& distance)


{
cout << "CIRCUMFERENCE FUNCTION" << endl << endl;
cout << "LIST THE IDENTIFIERS THAT are active here"
<< endl << endl;

4
// Fill in the code, given that parameter length
//contains the radius,
// that will find the circumference to be stored in
// distance

Answer:
distance = 2 * PI * length;

3. The following program demonstrates the use of default arguments. Find the output of
the program if it successfully compiles and runs.

#include <iostream>
using namespace std;

// function prototype with default arguments specified


void calNetPay(float& net, int hours=40, float rate=6.00);

int main()
{
int hoursWorked = 20;
float payRate = 5.00;
float pay;
// net pay calculated by the calNetPay function

calNetPay(pay);
// call to the function with only 1 parameter

cout << "The net pay is $" << pay << endl;
calNetPay(pay, hoursWorked);
// call to the function with 2 parameter
cout << "The net pay is $" << pay << endl;
calNetPay(pay, hoursWorked, payRate);
// call to the function with 3 parameter

cout << "The net pay is $" << pay << endl;

return 0;
}
// calNetPay
//
// task: This function takes rate and hours and multiples them to

5
// get net pay (no deductions in this pay check!!!). It has two
// default parameters. If the third argument is missing from the
// call, 6.00 will be passed as the rate to this function. If the
// second and third arguments are missing from the call, 40 will
//be passed as the hours and 6.00 will be passed as the rate.
//
// data in: pay rate and time in hours worked
// data out: net pay (alters the corresponding actual parameter)
void calNetPay(float& net, int hours, float rate)
{
net = hours * rate;
}
4. The following program demonstrates the prototype and implementation of the function
the takes an integer argument and returns its cube.

#include <iostream>
#include <math.h>
using namespace std;

// prototype for a user defined function


int cubeIt(int x);

// that returns the cube of the value passed to it.

int main()
{
int x = 2;
int cube;

cube = cubeIt(x);
// This is the call to the cubeIt function.

cout << "The cube of " << x << " is " << cube << endl;

return 0;
}

//*********************************************************
*********
// cubeIt
//
// task: This function takes a value and returns its cube
// data in: some value x
// data returned: the cube of x

6
//
//*********************************************************
*********

// Notice that the function type is int rather than void


int cubeIt(int x)
{
int num;

num = x * x * x;

return num;
}
4. The following program finds the square root of a number.

#include <iostream>

using namespace std;

int findSqrRoot(int x); // prototype for a user defined


function that

// returns the square root of the number passed to it


int main()
{
int number;

cout << "Input the number whose square root you want."
<< endl;
cout << "Input a -99 when you would like to quit."
<< endl;
cin >> number;

while (number != -99)


{
cout << "The square root of your number is "
<< findSqrRoot(number) << endl;
cout << "Input the number whose square root you want."
<< endl;
cout << "Input a -99 when you would like to quit."
<< endl;
cin >> number;
}

7
return 0;
}

int findSqrRoot(int x)
{
return sqrt(x);
return 0;
}

5.Write a function named "reverse" that takes as its arguments the following:
(1) an array of floating point values;
(2) an integer that tells how many floating point values are in the array.
The function must reverse the order of the values in the array. Thus, for example, if the array
that's passed to the function looks like this:
The function should not return any value.

Solution

#include <iostream>
using namespace std;
void display(int a[],int n){
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
void reverse(int a[],int n){
int m=n/2;
int temp;
int j;
for(int i=0;i<m;i++){
temp=a[i];
j=n-(i+1);
a[i]=a[j];
a[j]=temp;
}

}
int main(int argc, char** argv) {

8
int a[]={7,4,6,5,2,0,1};
int n=7;
cout<<"Before reversing the array:\n";
display(a,n);
reverse(a,n);
cout<<"After reversing the array:\n";
display(a,n);
return 0;
}

You might also like