Professional Documents
Culture Documents
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061
LAB 09
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"
using namespace std;
int main()
{
int power;
int base;
cout<<"enter any power: ";
cin>>power;
cout<<my_power(power,base);
}
SNAPSHOT:
TASK 02:
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.
SOURCE CODE:
#include <cstdlib>
#include<cmath>
#include <ctime>
#include <iostream>
using namespace std;
x1 *= x1;
x2 *= x2;
x3 *= x3;
x = x1 + x2 + x3;
n = 3 - 1;
var = x / n;
return var;
int main()
int averg;
int var;
srand((unsigned)time(0));
cout<<endl;
TASK 03:
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:
o Get two numbers from user
o Call four user defined fuctions
Variable to contain result should be decleared in main function and passed as
reference to the user defined function.
o Calculation must be done in the user defined function according to their names, e.g.
plus() should perfom addition of the two variables. Variable to contain result shoud be
updated in user defined function.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
int result;
float dresult;
int x;
int y;
cout<<"please enter 2 numbers:" <<endl;
cout<<endl;
SNAPSHOT:
SOURCE CODE:
#include "iostream"
using namespace std;
{
cout<<"=================================" <<endl;
int apples;
cout<<"How many Apples did you buy : ";
cin>>apples;
cout<<endl;
int banana;
cout<<"How many Bananas did you buy : ";
cin>>banana;
cout<<endl;
int mango;
cout<<"How many Mangoes did you buy : ";
cin>>mango;
cout<<endl;
int peaches;
cout<<"How many Peaches did you buy : ";
cin>>peaches;
cout<<endl;
int grapes;
cout<<"How many Grapes did you buy : ";
cin>>grapes;
cout<<endl;
cout<<"=================================" <<endl;
int y;
int total;
cout<<total;
cout<<endl;
cout<<endl;
cout<<"**********************************";
cout<<endl;
int subtotal = cal_price(160, apples, total) + cal_price(160, banana, total) +
cal_price(160, mango, total) + cal_price(160, peaches, total) + cal_price(160, grapes, total);
cout<<"The total price of your purchase is : ";
cout<<subtotal;
}
SNAPSHOT: