Lab 9 Exercise 1: - The Result As Follows
Lab 9 Exercise 1: - The Result As Follows
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)
LAB 9
Exercise 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. Print
the result as follows:
Source Code
#include<iostream>
using namespace std;
int main()
{
int base ,power ;
cout<<"enter base : ";
cin>>base;
Snapshot
Exercise 2
int main()
{
float a,b,c,d;
int a1[3],a2[2],a3[3],a4[3];
srand(time(0));
if(i==0)
{
a=f;
}
if(i==1)
{
b=f;
}
if(i==2)
{
c=f;
}
}
avrg( a , b , c , d );
cout<<"\n\ntheir average is : "<<d<<endl;
for(int i=0;i<=2;i++)
{
cout<<"\nenter number "<<i+1<<" : ";
cin>>a2[i];
float a = a2[i];
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 09 : Functions 2
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)
Snapshot
Exercise 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:
o Get two numbers from user o Call four user defined fuctions o 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.
o Print the result from main()
Source Code
#include<iostream>
using namespace std;
int main()
{
int a,b,plu,min,mult;
float div;
cout<<"\nenter first number : ";
cin>>a;
cout<<"\nenter second number : ";
cin>>b;
add( a ,b, plu);
sub( a, b,min);
multiply(a,b, mult);
divide( a, b,div);
Snapshot
Exercise 4
• 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():
#Sample Output
==================================
How many Apples did you buy : 2
How many Banana did you buy : 1
How many Mango did you buy : 3
How many Peach did you buy : 4
How many Grapes did you buy : 2
===================================
Price for Apple: 2 * 160 = 320
Price for Banana 1 * 120 = 120
Price for Mango: 3 * 110 = 330
Price for Peach: 4 * 100 = 400
Price for Grapes: 2 * 130 = 260
***************************************
Total Price of your purchase is: 1430
***************************************
Source Code
#include<iostream>
using namespace std;
int menu ;
do
{
cout<<"enter -1 to exit\n"
<<"enter 1 for apple ; price per kg 100 "
<<"\nenter 2 for banana ; price per dozen 120 "
<<"\nenter 3 for ; price per kg 130 "
<<"\nenter 4 for peach ; price per kg 140 "
<<"\nenter 5 for grapes ; price per kg 150\n\n";
cin>>menu;
switch(menu)
{
case 1 :
break;
case 2 :
case 3 :
case 4 :
case 5 :
default :
cout<<"\nthanku ";
}
}while(menu!=-1);
int main()
{
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 09 : Functions 2
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)
Cal_Price (price,how_many,total);
Snapshot