Computer Programming Lab 10

Lab 10: Function Overloading and Recursion

Exercise 1                                                                                                                                             
Write a C++ program for calculating grades of students.
            int main():
·         prompt user for the number of subjects for which he/she wants to calcuclate the grade 2 or 3.
·         prompt for marks with respect to their early selection
·         call calgrades() according to their selection.
o   For example calgrades(s1,s2) // for two subjects.
o   calgrades(s1,s2,s3) // for three subjects.
            Calgrades():
·         Function(s) must be overloaded accordingly.
·         These functions determine the grade of student on following criteria:
A grade : 87 - 100
B grade : 75 – 86
C grade : 65 – 74
D grade : 50 – 64
F grade : < 50
Source Code:
#include<iostream>

using namespace std;
char calgrades(int,int);
char calgrades(int,int,int);
int main()
{
int n;
cout<<"enter number of subjects:";
cin>>n;
if(n==3){
int a,b,c;
cout<<"enter marks respectively:";
cin>>a>>b>>c;
char r=calgrades(a,b,c);
cout<<"GRADE:"<<r;
}
if(n==2)
{
int a,b;
cout<<"enter marks respectively:";
cin>>a>>b;
char r=calgrades(a,b);
cout<<"GRADE:"<<r;
}

}

char calgrades(int x ,int y)
{
int temp=x+y;
int sum=temp/2;
if(sum>=50&&sum<=64)
return 'D';
if(sum>=65&&sum<=74)
return 'C';
if(sum>=75&&sum<=86)
return 'B';
if(sum>=87&&sum<=100)
return 'A';
else
return 'F';
}
char calgrades(int x,int y,int z)
{
int sum,temp;
temp=x+y+z;
sum=temp/3;
if(sum>=50&&sum<=64)
return 'D';
if(sum>=65&&sum<=74)
return 'C';
if(sum>=75&&sum<=86)
return 'B';
if(sum>=87&&sum<=100)
return 'A';
else
return 'F';
}



Exercise 2                                                                                                                                             
Write a C++ that contains following functions:
int main():
·         prompt user to enter numbers for comparison.
·         Minimum numbers user can enter are 2 and maximum upto 4.
·         call comparison() method with two, three and four parameters.
int comparison():
·         this function determine the smallest and largest number
·         print the smallest and largest number
·         this function(s) must be overloaded
Source Code: 
#include<iostream>
using namespace std;
void comp(int,int);
void comp(int,int,int);
void comp(int,int,int,int);
int main()
{
int n,a,b,c,d;
cout<<"enter choice :";
cin>>n;

if(n==2)
{
cout<<"enter 2 numbers respectively:"<<endl;
cin>>a>>b;

comp(a,b);
}
if(n==3){
cout<<"enter 3 numbers respectively:"<<endl;
cin>>a>>b>>c;
comp(a,b,c);
}

if(n==4)
{
cout<<"enter 4 numbers respectively:"<<endl;
cin>>a>>b>>c>>d;
comp(a,b,c,d);
}
}
void comp(int x,int y)
{
if(x>y)
cout<<x<<" is greater and "<<y<<" is smaller";
else
cout<<y<<" is greater and "<<x<<" is smaller";
}
void comp(int x,int y,int z){
if(x>y&&y>z)
cout<<x<<" is greater and "<<z<<" is smaller";
if(y>z&&z>x)
cout<<y<<" is greater and "<<x<<" is smaller";
if(z>x&&x>y)
cout<<z<<" is greater and "<<y<<" is smaller";
}
void comp(int x,int y,int z,int w){
if(x>y&&y>z&&z>w)
cout<<x<<" is greater and "<<w<<" is smaller";
if(y>z&&z>w&&w>x)
cout<<y<<" is greater and "<<x<<" is smaller";
if(z>w&&w>x&&x>y)
cout<<z<<" is greater and "<<y<<" is smaller";

if(w>x&&x>y&&y>z)
{
cout<<w<<" is greater and "<<z<<" is smaller";
}
}
RECURSION
Exercise 3                                                                                                                                             
Write a recursive function that prints the numbers between 1 to n in a reverse order.
Source Code:

#include <iostream>
using namespace std;
int recur(int x)
{
cout<<x<<" ";
if(x<=1)
return 1;
else
return recur(x-1);
}
int main()
{
int n;
cout<<"enter number to generate series in reverse order:";
cin>>n;
recur(n);
return 0;
}
Exercise 3                                                                                                                                             
Write a recursive function that prints the numbers between 1 to n in a reverse order.Exercise 4     
Write a C++ program that perform following task:
int main():
·         ask user to enter a positive number, store it in variable N.
·         You have to calculate 1+2+3+4+……+N with fuction int sum().
·         Print the result.
            int sum():
·         this function calculate the sum of series from 1 to N.
·         this fuction must be recursion function.
Source Code:
#include<iostream>
using namespace std;
int sum(int x)
{
  if(x==1)
return 1;
else
return x+sum(x-1);

}
int main()
{
int n,temp,sum1=0;
cout<<"enter number: 1 to ";
cin>>n;
temp=n;

int result=sum(temp);
cout<<"SUM="<<result;
}
Exercise 5                                                                                                                                             
Write a C++ program that perform following task:
int main():
·         Ask user to enter a positive number, store it in variable N.
·         You have to calculate  Fibonacci number with fuction int fab().
·         Print the result.
            int fab():
·         this function calculate the Fibonacci number.
o   0, 1, 1, 2, 3, 5, 8, 13, 21, 34,..
o   fab(0) = 0, fab(1) = 1
o   fab(n) = fab(n-1) + fab(n-2)  where n>1
Source Code:
#include<iostream>
using namespace std;
int fib(int x)
{
if(x==1||x==0)
{
return x;
}
else
return fib(x-1)+fib(x-2);
}
int main()
{
int n;
cout<<"enter n:";
cin>>n;
for(int i=0;i<=n;i++)
{
cout<<" "<<fib(i);
}
}



Comments

Post a Comment

Popular posts from this blog

Computer Programming Lab 5

Computer Programming lab 11

Computer Programming Lab 4