Computer Programming Lab 9
Lab 09: Functions - II
Task 1:
Write a
C++ Program that calculates
the power of a base by a user defined function as follows:
- Take the power and the base from the user in the
main function.
- Calcuate the power of the base in a user defined
function “MY_POWER” by passing power and base from the main ( ) to the
MY_POWER function.
- Calculated value must return from the function to
the main and display there.
Source Code:
#include
<iostream>
#include
<iomanip>
using
namespace std;
//user
define function
int
my_power(int x,int y,int ans=1)
{
for(int i=0;i<y;i++)
{
ans*=x;
}
return ans;
}
//main
function
int
main()
{
int x,y;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter Number For
Base: ";
cin>>x;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter Number For
Power: ";
cin>>y;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<my_power(x,y)<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
return 0;
}
Result
Task 2:
Write a
C++ Program that perform
following task.
- Generate three random numbers ranged from 1 to 100.
- Calculate average of three numbers using a function avrg(int,
int, int, int&).
- Calculate standard deviation and variance of three
numbers.
- Print the results.
Source Code:
#include
<iostream>
#include
<iomanip>
#include
<cmath>
#include
<ctime>
using
namespace std;
#include
<ctime> /* for time*/
#include
<stdlib.h> /* for rand*/
int
average(int a,int b, int c,int&avg)
{
avg=(a+b+c)/3;
}
int
Sd(int x,int y,int z,int&sd)
{
int mean=(x+y+z)/3;
int v=
(pow(x-mean,2))+(pow(y-mean,2)) + (pow(z-mean,2));
sd=sqrt(v/3);
}
int
Var(int x,int y, int z,int&V)
{
int mean=(x+y+z)/3;
V= (pow(x-mean,2))+(pow(y-mean,2)) +
(pow(z-mean,2));
}
int
main()
{
int i,j,k,x,y,z,A,SD,f,g,h;
int n[3]={i,j,k};
srand(time(NULL));
i=(rand()%100)+1;
j=(rand()%100)+1;
k=(rand()%100)+1;
cout<<"Three Random Numbers Are:
"<<i<<" "<<j<<"
"<<k<<endl;
average(i,j,k,A);
cout<<"Average of Three Random
Number: "<<A<<endl;
Var(i,j,k,h);
cout<<"Variance
"<<h<<endl;
Sd(f,g,h,SD);
cout<<"Standard
Deviation: "<<SD;
}
Result:
Task 3:
Write a
C++ Program that contains
four user defined function(s) plus(int,
int, int&), minus(int, int, int&), multiply(int, int, int&),
divide(int, int, float&).
- In main() function:
- Get two numbers from user
- Call four user defined fuctions
- Variable to contain result should be decleared in
main function and passed as reference to the user defined function.
Source Code:
#include
<iostream>
#include
<iomanip>
using
namespace std;
//user
defined function
int
add(float&a,float&b,float&add)
{
add=a+b;
}
int
minuss(float&a,float&b,float&minus)
{
minus=a-b;
}
float
div(float &a,float &b,float &div)
{
div=a/b;
}
float
mul(float &a,float &b,float &mul)
{
mul=a*b;
}
//Main
Function
int
main()
{
float x,y;
float a;
char c;
float ans;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter First
Number: ";
cin>>x;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter Second
Number: ";
cin>>y;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<" Enter Choice "<<endl;
cout<<" '+' for additon
'-' for Subbtract"<<endl;
cout<<" '*' for
multiply '/'for
Division"<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cin>>c;
cout<<setw(35)<<setfill('=')<<"="<<endl;
if(c=='+')
{
add(x,y,a);
cout<<a;
}
else if(c=='-')
{
minuss(x,y,a);
cout<<a;
}
else if(c=='/')
{
div(x,y,ans);
cout<<ans;
}
else if(c=='*')
{
mul(x,y,ans);
cout<<ans;
}
else
{
cout<<"Invalid
operation!"<<endl;
}
cout<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
system("pause");
}
Result:
Task 4:
Write a C++
that calculate price of purchased fruits.
- A shopkeer supplies following fruits.
- Apple, Banana, Mango, Peach and Grapes
- Unit of each fruit per kg is:
- Apple = 160
- Banana = 120
- Mango = 110
- Peach = 100
- Grapes = 130
- Ask user to enter purchased quantity of each fruits.
Store values in variables.
- Write a function Cal_Pric
(int, int, int& total) that calculate the price for each fruit.
- For example Cal_Price(160,2,total)
saves 320 in variable total.
- Print the results from main():
Source Code:
include <iostream>
#include
<iomanip>
using
namespace std;
//userdefined
function
int
app_price(int x,int y,int &total)
{
total=x*y;
}
int
ban_price(int x,int y,int&total1)
{
total1=x*y;
}
int
mang_price(int x,int y,int&total2)
{
total2=x*y;
}
int
peac_price(int x,int y,int&total3)
{
total3=x*y;
}
int
grap_price(int x,int y,int&total4)
{
total4=x*y;
}
//main
function
int
main()
{
int
a,b,m,p,g,bill,ap=160,ba=120,ma=110,pe=100,gr=130;
int totalbill=0;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"How many Apples
did you buy : ";
cin>>a;
cout<<"How many Banana
did you buy : ";
cin>>b;
cout<<"How many Mangoes
did you buy : ";
cin>>m;
cout<<"How many Peaches
did you buy : ";
cin>>p;
cout<<"How many Grapes
did you buy : ";
cin>>g;
cout<<setw(35)<<setfill('=')<<"="<<endl;
app_price(a,ap,bill);
cout<<"Price for Apple:
"<<a<<"*"<<ap<<" =
"<<bill<<endl;
totalbill=totalbill+bill;
ban_price(b,ba,bill);
cout<<"Price for Banana:
"<<b<<"*"<<ba<<" =
"<<bill<<endl;
mang_price(m,ma,bill);
totalbill=totalbill+bill;
cout<<"Price for MAngoes:
"<<m<<"*"<<ma<<" =
"<<bill<<endl;
peac_price(p,pe,bill);
totalbill=totalbill+bill;
cout<<"Price for Peaches:
"<<p<<"*"<<pe<<" =
"<<bill<<endl;
grap_price(g,gr,bill);
totalbill=totalbill+bill;
cout<<"Price for Grapes:
"<<g<<"*"<<gr<<" =
"<<bill<<endl;
totalbill=totalbill+bill;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Total Price of your
purchase is: "<<totalbill<<endl;
cout<<setw(35)<<setfill('=')<<"="<<endl;
system("pause");
return 0;
}
Comments
Post a Comment