0% found this document useful (0 votes)
80 views14 pages

Lab 9 Exercise 1: - The Result As Follows

1) The document describes 4 programming exercises from a computer science lab class. 2) The exercises involve writing C++ programs to calculate powers of a base number, generate random numbers and calculate their average and statistics, perform basic math operations using functions, and calculate the total price of purchased fruits. 3) The programs demonstrate the use of functions, input/output, and basic math operations in C++.

Uploaded by

Muhammad Zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views14 pages

Lab 9 Exercise 1: - The Result As Follows

1) The document describes 4 programming exercises from a computer science lab class. 2) The exercises involve writing C++ programs to calculate powers of a base number, generate random numbers and calculate their average and statistics, perform basic math operations using functions, and calculate the total price of purchased fruits. 3) The programs demonstrate the use of functions, input/output, and basic math operations in C++.

Uploaded by

Muhammad Zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

CSL-113 : Computer Programming Lab

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


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

int main()
{
int base ,power ;
cout<<"enter base : ";
cin>>base;

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)

cout<<"\nenter power : ";


cin>>power;
cout<<"\nbase "<<base<<" to the power "<<power<<" is : "<<MY_POWER(base ,power);
}

Snapshot

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)

Exercise 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<cstdlib>
#include<iomanip>
#include<cmath>
#include<time.h>
using namespace std;

avrg( float a , float b , float c , float & avg )


{
avg = ( a + b + c )/3;
}

int main()
{
float a,b,c,d;
int a1[3],a2[2],a3[3],a4[3];
srand(time(0));

for( int i=0;i<=2;i++)


{
float f;
f=a1[i]= rand() % 100 ;

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)

if(i==0)
{
a=f;
}

if(i==1)
{
b=f;
}

if(i==2)
{
c=f;
}
}

cout<<"\nthree random generated numbers are : "<<setw(4)<<a<<setw(4)<<b<<setw(4)<<c;

avrg( a , b , c , d );
cout<<"\n\ntheir average is : "<<d<<endl;

cout<<"\n*********************standard devaition and varince********************\n";

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)

a3[i]= pow (((a-d/3)*(a-d/3))/3,2);


float b=a3[i];
a4[i]=sqrt(b);
float c=a4[i];
cout<<"\nstandard deviation of number "<<i+1<<" is : "<<b<<" and variance is : "<<c;
cout<<endl;
}

Snapshot

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)

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;

add(int a ,int b,int &plu)


{
plu=a+b;
}

sub(int a,int b,int &min)


{
min=a-b;
}

multiply(int a,int b,int &mult)


{
mult=a*b;
}

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)

divide(int a,int b,float &div)


{
float e=a,f=b;
div=e/f;
}

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

cout<<"\naddition : "<<plu<<"\n\nsubtraction : "<<min<<"\n\nmultiplication : "<<mult<<"\n\ndivision :


"<<div;
}

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

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)

Exercise 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():
#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
***************************************

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)

Source Code
#include<iostream>
using namespace std;

Cal_Price (int price,int how_many,int &total)


{

int menu ;

do
{

cout<<"\n\n\n************************* menu **************************\n\n";

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 :

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)

cout<<"\nhow many kg of apple would you like to buy : ";


cin>>how_many;
price=100;
total=price*how_many;

break;

case 2 :

cout<<"\nhow many dozen of banna would you like to buy : ";


cin>>how_many;
price=120;
total=price*how_many;
break;

case 3 :

cout<<"\nhow many kg of mango would you like to buy : ";


cin>>how_many;
price=130;
total=total*how_many;
break;

case 4 :

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)

cout<<"\nhow many kg of peach would you like to buy : ";


cin>>how_many;
price=140;
total=price*how_many;
break;

case 5 :

cout<<"\nhow many kg of grapes would you like to buy : ";


cin>>how_many;
price=150;
total=price*how_many;
break;

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)

int price=0 , how_many=0,total=0;

Cal_Price (price,how_many,total);

cout<<"\n\nyour total amount is : "<<total;


}

Snapshot

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)

Department of computer science Semester BSCS-1A


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

You might also like