[go: up one dir, main page]

0% found this document useful (0 votes)
15 views13 pages

Lab Report 02

Uploaded by

bM malik
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)
15 views13 pages

Lab Report 02

Uploaded by

bM malik
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/ 13

Lab Report No __02

Object Oriented Programming

Submitted By:
MOHAMMAD AWAIS MALIK 19-EE-057
SH. TALHA 19-EE-093
WASIF ALI 19-EE-157
ALI ASHFAQ-19-EE-179
[Section :C]
Submitted to:
Sir Jawwad Qammar
Dated:
Week 02

Department of Electrical Engineering,


HITEC University, Taxila
Lab Task No 01:

Solution:
Brief description (3-5 lines)
Write a C++ Program to Calculate Average of Numbers Using Arrays. This program takes n
number of element from user (where, n is specified by user), stores data in an array and
calculates the average of those numbers.

The code
#include <iostream>
using namespace std;

int main()
{
int n, i;
float num[100], sum=0.0, average;

cout << "Enter the numbers of data: ";


cin >> n;

while (n > 100 || n <= 0)


{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}

for(i = 0; i < n; ++i)


{
cout << i + 1 << ". Enter number: ";
cin >> num[i];
sum += num[i];
}

average = sum / n;
cout << "Average = " << average;

return 0;
}

Page 1 of 13
The results (Screenshot)

Lab Task No 02:

Solution:
Brief description (3-5 lines)
Write C++ Program to Calculate Standard Deviation. This program calculates the standard
deviation of a individual series using arrays.

The code
#include <iostream>
#include <cmath>
using namespace std;

float calculateSD(float data[]);

int main()
{
int i;
float data[10];

cout << "Enter 10 elements: ";


for(i = 0; i < 10; ++i)
cin >> data[i];

cout << endl << "Standard Deviation = " << calculateSD(data);

return 0;
}

float calculateSD(float data[])


Page 2 of 13
{
float sum = 0.0, mean, standardDeviation = 0.0;

int i;

for(i = 0; i < 10; ++i)


{
sum += data[i];
}

mean = sum/10;

for(i = 0; i < 10; ++i)


standardDeviation += pow(data[i] - mean, 2);

return sqrt(standardDeviation / 10);


}

The results (Screenshot)

Page 3 of 13
Lab Task No 03:

Solution:
Brief description (3-5 lines)
Write a C++ Program to Add Two Matrix Using Multi-dimensional Arrays. In this program,
user is asked to enter the number of rows r and columns c. The value of r and c should be less
than 100 in this program. The user is asked to enter elements of two matrices (of order r*c).
Then, the program adds these two matrices, saves it in another matrix (two-dimensional
array) and displays it on the screen.

The code

Page 4 of 13
#include <iostream>
using namespace std;

int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;

cout << "Enter number of rows (between 1 and 100): ";


cin >> r;

cout << "Enter number of columns (between 1 and 100): ";


cin >> c;

cout << endl << "Enter elements of 1st matrix: " << endl;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

// Adding Two matrices


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)

Page 5 of 13
cout << endl;
}

return 0;
}

The results (Screenshot)

Lab Task No 04:

Solution:
Brief description (3-5 lines)
Write a C++ program to check Whether a Number can be Expressed as a Sum of Two
Prime Numbers or not using Functions.

The code
#include <iostream>
using namespace std;

Page 6 of 13
bool checkPrime(int n);

int main()
{
int n, i;
bool flag = false;

cout << "Enter a positive integer: ";


cin >> n;

for(i = 2; i <= n/2; ++i)


{
if (checkPrime(i))
{
if (checkPrime(n - i))
{
cout << n << " = " << i << " + " << n-i << endl;
flag = true;
}
}
}

if (!flag)
cout << n << " can't be expressed as sum of two prime numbers.";

return 0;
}

// Check prime number


bool checkPrime(int n)
{
int i;
bool isPrime = true;

for(i = 2; i <= n/2; ++i)


{
if(n % i == 0)
{
isPrime = false;
break;
}
}

Page 7 of 13
return isPrime;
}

The results (Screenshot)

Lab Task No 05:

Solution:
Brief description (3-5 lines)
Write a C++ program to generate the multiplication table of a number (entered by the user)
using Functions.

The code
#include<iostream>
using namespace std;

int table(int n);

int main()
{

Page 8 of 13
int n=0;

cout<<"Enter Number: ";


cin>>n;

table(n);

int table(int n)
{
int t=1;

cout<<"Table of " <<n << " is: " <<endl;

for(int i=1; i<=10; i++)


{
t=n*i;

cout<<n <<" * " <<i <<" = " <<t <<endl;

return 0;
}

The results (Screenshot)

Page 9 of 13
Lab Task 06:
Brief description (3-5 lines)
Write a C++ program that will display the calculator menu.
The program will prompt the user to choose the operation choice (from 1 to 5). Then it asks
the user to input two integer vales for the calculation.

The code
#include <cstdlib>
#include <iostream>
#include<iomanip>

using namespace std;

void displaymenu(){
cout<<" MENU "<<"\n";
cout<<" 1.Add"<<"\n";
cout<<" 2.Subtract"<<"\n";
cout<<" 3.Multiply"<<"\n";
cout<<" 4.Divide"<<"\n";
cout<<" 5.Modulus"<<"\n";
}

Page 10 of 13
int Add(int a,int b){
return(a+b);
}

int Substract(int a, int b){


return(a-b);
}

int Multiply(int a, int b){


return(a*b);
}
float Divide(int a,int b){
return(a/b);
}
int Modulus(int a, int b){
return(a%b);
}

int main(int argc, char *argv[])


{
//show menu
displaymenu();
int yourchoice;
int a;
int b;
char confirm;
do
{
cout<<"Enter your choice(1-5):";
cin>>yourchoice;
cout<<"Enter your two integer numbers:";
cin>>a>>b;
cout<<"\n";
switch(yourchoice){
case 1:cout<<"Result:"<<Add(a,b);break;
case 2:cout<<"Result:"<<Substract(a,b);break;
case 3:cout<<"Result:"<<Multiply(a,b);break;
case 4:cout<<"Result:"<<Divide(a,b);break;
case 5:cout<<"Result:"<<Modulus(a,b);break;
default:cout<<"invalid";
}

cout<<"\nPress y or Y to continue:";
cin>>confirm;

Page 11 of 13
}while(confirm=='y'||confirm=='Y');
system("PAUSE");
return EXIT_SUCCESS;
}

The results (Screenshot)

THE END

Page 12 of 13

You might also like