You are on page 1of 12

CSL-113: Computer Programming Lab

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 my_power(int power,int base )


{
int s=1;
for(int i=0; i<power; i++)
s=(s*base);
return s;
}

int main()

{
int power;
int base;
cout<<"enter any power: ";
cin>>power;

Department of Computer Sciences 1/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

cout<<"enter base: ";


cin>>base;

cout<<my_power(power,base);
}

SNAPSHOT:

Department of Computer Sciences 2/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

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;

int avrg(int random_integer1, int random_integer2, int random_integer3, int& averg)


{
int sum;
sum= random_integer1 + random_integer2 + random_integer3;
averg = sum/3;
return averg;
}

int variance(int random_integer1, int random_integer2, int random_integer3, int averg)


{
int x1, x2, x3, x, var, n;
x1 = random_integer1 - averg;
x2 = random_integer2 - averg;
x3 = random_integer3 - averg;

Department of Computer Sciences 3/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

x1 *= x1;
x2 *= x2;
x3 *= x3;
x = x1 + x2 + x3;
n = 3 - 1;
var = x / n;
return var;

float sd(int random_integer1, int random_integer2, int random_integer3, int var)


{
float sdv;
sdv = sqrt(var);
return sdv;
}

int main()

int averg;
int var;

srand((unsigned)time(0));

int random_integer1 = (rand()%100)+1;


cout << "Random number 1: " <<random_integer1 <<endl;
cout<<endl;

int random_integer2 = (rand()%100)+1 ;

Department of Computer Sciences 4/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

cout << "Random number 2: " <<random_integer2 <<endl ;


cout<<endl;

int random_integer3 = (rand()%100)+1 ;


cout << "Random number 3: " <<random_integer3 <<endl;
cout<<endl;

cout<<"The average of 3 numbers is: " <<avrg(random_integer1, random_integer2,


random_integer3, averg);

cout<<endl;

cout << "The Variance is :" << variance(random_integer1, random_integer2,


random_integer3, averg) << endl;

var=variance(random_integer1, random_integer2, random_integer3, averg);

cout << "Standard Deviation is : " << sd(random_integer1, random_integer2,


random_integer3, var);
}
SNAPSHOT:

Department of Computer Sciences 5/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

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.

Department of Computer Sciences 6/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

SOURCE CODE:
#include <iostream>
using namespace std;

int sum(int x, int y, int& result)


{
result= x + y;
return result;
}

int subt(int x, int y, int& result)


{
result = x - y;
return result;
}

int mult(int x, int y, int& result)


{
result = x * y;
return result;
}

float div(int x, int y, float& result)


{
float a = (float)x;
float b = (float)y;
result = a/b;
return result;
}

int main()
{
int result;
float dresult;

Department of Computer Sciences 7/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

int x;
int y;
cout<<"please enter 2 numbers:" <<endl;

cout<<endl;

cout<<"1st number: ";


cin>>x;

cout<<"2nd number: ";


cin>>y;

cout<<"sum of 2 numbers is: ";


sum(x, y, result);
cout<<result;
cout<<endl;

cout<<"difference of 2 numbers is: ";


subt(x, y, result);
cout<<result;
cout<<endl;

cout<<"product of 2 numbers is: ";


mult(x, y, result);
cout<<result;
cout<<endl;

cout<<"division of 2 numbers is: ";


div(x, y, dresult);
cout<<dresult;
cout<<endl;
}

Department of Computer Sciences 8/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

SNAPSHOT:

TASK 04(take home assignment)


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.

Department of Computer Sciences 9/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

SOURCE CODE:
#include "iostream"
using namespace std;

int cal_price(int x, int y, int& total)


{
total = x * y;
return total;
}
int main()

{
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;

Department of Computer Sciences 10/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

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<<"Price for Apple : " <<apples <<" * 160 = ";


total = cal_price(160, apples, total);
cout<<total;
cout<<endl;

cout<<"Price for Banana : " <<banana <<" * 120 = ";


total = cal_price(160, banana, total);
cout<<total;
cout<<endl;

cout<<"Price for Mango : " <<mango <<" * 110 = ";


total = cal_price(160, mango, total);
cout<<total;
cout<<endl;

cout<<"Price for Peaches : " <<peaches <<" * 100 = ";


total = cal_price(160, peaches, total);

Department of Computer Sciences 11/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II
CSL-113: Computer Programming Lab
Semester BS CS – 01
MUHAMMAD SAAD UZ ZAMAN KHAN
02-134191-061

cout<<total;
cout<<endl;

cout<<"Price for Grapes : " <<grapes <<" * 130 = ";


total = cal_price(160, grapes, 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:

Department of Computer Sciences 12/12 Semester BSCS 01


CSL-113: Computer Programming Lab Lab 09: Functions II

You might also like