Worked Problems On Functions
Worked Problems On Functions
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
#include <iostream>
#include <iomanip>
using namespace std;
const double PI = 3.14;
const double RATE = 0.25;
int main()
{
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
Answer:
FindArea(radius,area)
{
float radius = 10;
float circumference;
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
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)
//
//
***********************************************************
*********
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)
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;
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;
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
//
//*********************************************************
*********
num = x * x * x;
return num;
}
4. The following program finds the square root of a number.
#include <iostream>
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;
}