You are on page 1of 6

Programming Fundamentals

Graded Lab
Lab 10

Lab Engineer: Ahmad Abduhu

Topic: Recursion and File Handling


Session: Fall 2018

School of Systems & Technology


UMT Lahore Pakistan
Lab4: Recursion & File Handling
Lab Objectives:
 Understand the concept of recursions.
 Understand the concept of File Handling.
 Implementing various recursive functions.
 Implementation of Input/output with files.

Instructions:
 Indent your code
 Comment your code
 Use meaningful variable and function names
 Plan your code carefully on a piece of paper before you implement it.
 Name of the program should be same as the task name. i.e. the first program should be
Task_4_01.cpp
 Upload a zip file on Moodle, zip file should contain all the tasks you have implemented.
 Name of zip file should be your UMT-ID e.g. 12003065123.zip
Sample Lab Tasks:
Sample Lab Task (Recursion) :

Write a recursive function which calculates two positive numbers sum.

Sample Output:

Enter first number: 2

Enter second number: 3

Sum of 2 & 3 is 5

Sample Code:
#include <iostream>
using namespace std;
int add(int x,int y){
if(y>=0 && x>=0){
if(y==0)
return x;
else
return 1+add(x,y-1);
}
else
return -1;
}
int main()
{
int x,y;
cout<<"Enter first number ";
cin>>x;
cout<<"Enter second number ";
cin>>y;
int ans=add(x,y);
if(ans==-1)
cout<<"you have entered a negative number ";
else
cout<<"Sum of "<<x<<" and "<<y<<" is "<<ans<<endl;
return 0;
}
Sample Lab Task (Fille Handling ) :

Writing operations on text files are performed in the same way as we operated with cout:

// writing on a text file


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

int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

Reading from a file can also be performed in the same way that we did with cin:

// reading a text file


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
}
Lab Tasks
Task 1:

Write a recursive function which computes sum of first ten numbers. (1 to 10)

i.e. 1+2+3+4+5+6+7+8+9+10=55

Task 2:

Write a recursive function which computes factorial of a number entered by the user.

Task 3:

Write a recursive function power (base, exponent) that when invoked returns base exponent. For example,
power (3, 4) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1.

Hint: The recursion step would use the relationship

base exponent = base * base exponent–1


and the terminating condition occurs when exponent is equal to 1 because

base1 = base
Sample Output:

Enter base number: 2


Enter power number: 3
base ^ exponent = 8

Task 4:

The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y.
Write a recursive function GCD that returns the greatest common divisor of x and y. The GCD of x and y
is defined recursively as follows: If y is equal to 0, then GCD(x, y) is x; otherwise GCD(x, y) is GCD(y, x % y)
where % is the remainder operator.

Sample Output:

Enter two numbers (Positive numbers): 12 18

GCD of 12 & 18 is: 6

Task 5:

Write a program to declare an array of user defined size, initialize it with random numbers and write this
array to a file myArray.txt. Also write code to read that array data from file.
#include <iostream>
#include <fstream>
using namespace std;

int main () {
const int size = 7;
double x[] = {1,2,3,4,5,6,7};

ofstream myfile ("example.txt");


if (myfile.is_open())
{

for(int count = 0; count < size; count ++){


myfile << x[count] << " " ;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

You might also like