Practice Exercises #8 Chapter 6 Functions Page 1
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #8 Functions
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
draw a flowchart to find the factorial of a positive integer,
declare a function,
call a function,
define a function,
write a program to declare, call and define a function to calculate the factorial of a positive
integer
test the written program in one of Computer Labs.
1. Draw a flowchart to calculate the factorial of a positive integer:
2. Write a program using a function. The program is to print the factorial for a given
integer number n. You should have a function to calculate the factorial of that introduced
number. The main program coordinates the process of inputting values and calling this
function as necessary.
Questions
stionsExercise
Practice Exercises #8 Chapter 6 Functions Page 2
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Flowchart
START
yes
no
Fact=1
Fact=1
Fact=Fact*I
I=I+1
N <I
no
END
N, Fact
N=0 yes
N
Practice Exercises #8 Chapter 6 Functions Page 3
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Program
// Function prototypes
#include <iostream>
using namespace std;
double factorial(int);
int main( )
{
int n;
double result;
cout << "Enter a value for n: ";
cin >> n;
result = factorial(n);
cout << "\n The factorial of n is " << result<< endl;
return(0);
}
double factorial(int m)
{
int i;
double fact;
fact=1;
if (m==0)
{
fact=1;
return(fact);
}
for (i=1;i<=m;i++)
fact=fact*i;
return(fact);
}
Practice Exercises #9 Chapter 6 Functions Page 4
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #9 Pass-by-Value and Pass-by-Reference
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
Apply passing arguments by value and by reference,
write programs calling functions and passing arguments by value and by reference,
test the written programs in one of Computer Labs.
1. Write a program using two functions. The program is to print the area of a rectangle for
a given input length and width. One of these function should be called from the main
program by pass-by-value arguments and the second by pass-by-reference arguments.
Try to use some mechanisms to detect the difference between the two types of calls.
The main program coordinates the process of inputting values, calling these functions
and printing outputs on the screen.
Questions
stionsExercise
Practice Exercises #9 Chapter 6 Functions Page 5
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
// Calculate the area of a rectangle
// Pass-by-Value and Pass-by-Reference
#include <iostream>
using namespace std;
// Function prototype
float area1(float,float);
float area2(float &,float &);
main()
{
float x,y,surf;
cout << "Enter values for length x and width y: ";
cin >> x >> y;
cout <<endl;
cout << "Value of x and y before functions calls"<< endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout <<endl;
surf = area1(x,y);
cout << "Pass By Value"<< endl;
cout << "Rectangle1 area is " << surf << endl;
cout << "Value of x and y after Pass-by-Value call"<< endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout <<endl;
surf = area2(x,y);
cout << "Pass By Reference"<< endl;
cout << "Rectangle2 area is " << surf << endl;
cout << "Value of x and y after Pass-by-Reference call"<< endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
cout <<endl;
return(0);
} // End of main
Practice Exercises #9 Chapter 6 Functions Page 6
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
// Function Definitions_ Pass By value
float area1(float x, float y)
{
float surf1; //local variable
x=x+1;
y=y+1;
surf1 = x * y;
return(surf1);
}
// Function Definitions_ Pass By Reference
float area2(float &x, float &y)
{
float surf2; //local variable
x=x+1;
y=y+1;
surf2 =x*y;
return(surf2);
}
Program Output
Enter values for length x and width y: 2 4
Value of x and y before functions calls
x=2
y=4
Pass By Value
Rectangle1 area is 15
Value of x and y after Pass-by-Value call
x=2
y=4
Pass By Reference
Rectangle2 area is 15
Value of x and y after Pass-by-Reference call
x=3
y=5
Press any key to continue
Practice Exercises #10 Chapter 6 Functions Page 7
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #10 Functions (Cont.)
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
call a function,
define a function,
write a program to declare, call and define a function,
test the written programs in one of Computer Labs
1. Write two functions to calculate the areas of a rectangle and a square respectively. The
main program coordinates the process of inputting values, calling these functions and
printing the corresponding outputs on the screen.
Questions
stionsExercise
Practice Exercises #10 Chapter 6 Functions Page 8
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Program
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
// Function prototypes
float RectangleArea(float,float);
float SquareArea(float);
main()
{
float length, width, side;
float areaRec=0.0;
float areaSq=0.0;
cout << "\n Enter the rectangle length: ";
cin>>length;
cout << "\n Enter the rectangle width: ";
cin>>width;
cout <<"\n Enter the squre side: ";
cin>>side;
areaRec=RectangleArea(length, width);
areaSq=SquareArea(side);
cout<<"\n";
cout<<"\nOutput Data\n";
cout << "Rectangle Length: "<<length<<endl;
cout << "Rectangle Width: "<<width<<endl;
cout << "Square side: "<<side<<endl;
cout<<"\n";
cout << "\nTRectangleArea = " << areaRec<< endl;
cout << "Square Area = " << areaSq<< endl;
cout<<"\n";
return(0);
} // End main
Practice Exercises #10 Chapter 6 Functions Page 9
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
// function to calculate the area of a rectangle
float RectangleArea(float length1, float width1)
{ float area; //local variable
area=length1*width1;
return(area);
} // function to calculate the area of a square
float SquareArea(float side1)
{ float area; //local variable
area= side1*side1;
return(area);
}
Practice Exercises #11 Chapter 6 Passing Arrays to Functions Page 10
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #11 Passing Arrays to Functions
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
call a function,
define a function,
pass an array to function
test the written program in one of Computer Labs
Write a main program passing an array to functions. These functions should accomplish the
following tasks:
Calculate the sum of even elements in a two-dimensional array.
Calculate the sum of odd elements in a two-dimensional array.
The main program coordinates the process of inputting values, calling these functions and printing
the corresponding outputs on the screen.
Questions
stionsExercise
Practice Exercises #11 Chapter 6 Passing Arrays to Functions Page 11
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Program
// Passing arrays and individual array elements to functions
#include <iostream>
using namespace std;
int evenArray(int [][2], int, int);
int oddArray(int [][2], int, int);
main()
{
const int row = 3;
const int col = 2;
int a[row][col];
int i,j;
cout << endl << endl;
// Input Array Elements
cout << "\n Iutput Data "<< "\n";
cout << endl;
for ( i=0; i<row; i++)
for ( j=0; j<col; j++)
{
cout << "matrix[" << i <<","<<j<< "]: ";
cin >> a[i][j];
}
cout << endl;
cout << endl << endl << endl;
cout << "\nOutput Data "<< "\n";
cout << endl;
cout << "\n Sum of even elements = " <<evenArray(a, row, col)<<endl;
cout << "\n Sum of odd elements = " <<oddArray(a, row, col)<<endl;
cout << endl << endl << endl;
return(0);
}
Practice Exercises #11 Chapter 6 Passing Arrays to Functions Page 12
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
// function to calculate the sum of even elements in a two-dimensional array
int evenArray(int b[][2], int row1, int col1)
{
int max=0;
int i,j;
int s1=0;
for ( i=0; i<row1; i++)
for ( j=0; j<col1; j++)
{
if (b[i][j]%2==0)
s1=s1+b[i][j];
}
return(s1);
}
// function to calculate the sum of odd elements in a two-dimensional array
int oddArray(int b[][2], int row1, int col1)
{
int s2=0;
int i,j;
for ( i=0; i<row1; i++)
for ( j=0; j<col1; j++)
if (b[i][j]%2==1)
s2=s2+b[i][j];
return(s2);
}
Practice Exercises #12 Chapter 6 Functions using menu Page 13
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #12 Functions Using Menu
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
call a function,
define a function,
write a menu and select items from the menu
write switch case structure
test the written program in one of Computer Labs
Write a main program to calculate the area of some geometric forms. The user should select from a
menu the geometric form and input the corresponding values:
Area Calculus 1. Trapeze Area ";
2. Circle Area ";
3. Square Area ";
Enter your choice (0 to end) ";
The input values should be transmitted to the corresponding function for conversion. The function
result should be printed by the main program.
Questions
stionsExercise
Practice Exercises #12 Chapter 6 Functions using menu Page 14
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
#include <iostream> #include <math.h> #include <iomanip> using namespace std; // Function prototypes float TrapezeArea(float,float, float); float CircleArea(float); float SquareArea(float); main() { float B, b, h, s; float r; int choice; do { cout << "\n Area Calculus"; cout<<"\n"; cout << "\n 1. Trapeze Area "; cout << "\n 2. Circle Area "; cout << "\n 3. Square Area "; cout<<"\n"; cout << "\n Enter your choice (0 to end) "; cin>>choice; cout << "\n"; switch(choice) { case 1: { cout << "\n Enter the trapeze big base: "; cin>>B; cout << "Enter the trapeze little base: "; cin>>b; cout << "Enter the trapeze height: "; cin>>h; cout << "\nTrapeze Area = " << TrapezeArea(B, b, h)<< endl; break; } case 2: { cout << "Enter the Circle Radius: "; cin>>r; cout << "Circle Area = " <<CircleArea(r)<< endl; break; } case 3: { cout << "Enter the square side: "; cin>>s; cout << "Square Area = " << SquareArea(s)<< endl; break; } case 0: { break; } default: cout << "\not in range"<< endl; break; } } while (choice>0); return(0); } // End main
// Function to calculate trapeze area
float TrapezeArea(float B1, float b1,
float h1)
{ float area; //local variable
area=((B1+b1)*h1)/2;
return area;
}
// Function to calculate circle area
float CircleArea(float r1)
{ float area1; //local variable
area1= 3.141*r1*r1;
return area1;
}
// Function to calculate square area
float SquareArea(float s1)
{ float area; //local variable
area= s1*s1;
return area;
}
Practice Exercises #13 Chapter 6 Functions to Convert Measures Page 15
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
Practice Exercises #13 Functions to Convert Measures
Related Chapter Chapter Title
6 |Functions
STUDENT LEARNING OUTCOMES:
Upon completion of this practical exercise, a successful student will be able to:
call a function,
define a function,
write a function to convert from one measure to another,
test the written program in one of Computer Labs
Write a main program which calls two functions to convert values from kilograms to grams
and from meters to centimeters respectively.
The main program asks the user for inputs. Thereafter, the main program calls the functions
and displays the result.
Questions
stionsExercise
Practice Exercises #13 Chapter 6 Functions to Convert Measures Page 16
Dr. Mohamed Aissa COMP151 Introduction to Algorithms and Programming
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
// Function prototypes
float convweight(float);
float convdist(float);
int main()
{
float w, d;
cout << "\n Enter the weight in kg: ";
cin>>w;
cout << "Enter the distance in m: ";
cin>>d;
cout<<"\n";
cout<<"\n Output Data \n";
cout << " Weight in kg: "<<w<<endl;
cout << " Distance in m: "<<d<<endl;
cout<<"\n";
cout << "\n Weight in g = " <<convweight(w)<< endl;
cout << "\n Distance in m = " <<convdist(d)<< endl;
cout<<"\n";
return(0);
} // End main
// Function to convert values from kg to gr.
int convweight(float w1)
{
int weight; //local variable
weight=w1*1000;
return(weight);
}
// Function to convert values from m to cm.
int convdist(float d1)
{
int dist; //local variable
dist=d1*100;
return(dist);
}