0% found this document useful (0 votes)
4 views

Object Oriented Programming 1

Uploaded by

f2024408246
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)
4 views

Object Oriented Programming 1

Uploaded by

f2024408246
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/ 15

Object Oriented Programming

Lab Manuals

School of Systems and Technology


UMT Lahore Pakistan

M Jawad Akhtar
LAB. NO 1
Revision of Programming Fundamentals and Structured
Programming Concepts

Objective:
Revision of concepts that you’ve already discussed and implemented in Programming Fundamentals and
Structured Programming.

1D array
Sample Lab Task 1:

Write a program in C++ which implements the bubble sort algorithm. It should sort the input array in ascending
order.
Sample Code:

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10], i,j,temp;
cout << "Enter the elements of array: " << endl;
for (i = 0; i<10; i++){
cin >> arr[i];
}
cout << "The elements of array before sorting are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
for (i = 0; i < 10; i++){
for (j = 0; j < 9; j++){
if (arr[j + 1] < arr[j]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "\nThe elements of array after sorting in ascending order are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
cout << endl;
}

2D array
Sample Lab Task 2:

Write a program in C++ which declares a 2D array of size 3 by 5, take input from the user in 2D array and show the
output in matrix form.

Sample Code:

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;


void main()
{
int disp[3][5];
int i, j;
cout<<"Enter elements of array: "<<endl;
for(i=0; i<3; i++)
{
for(j=0;j<5;j++)
{
disp3[i][j]=disp1[i][j] + disp2 [i][j] ;
}
}
cout<<"The elements of array are: "<<endl<<endl;
for(i=0;i<3;i++){
for(j=0;j<5;j++){
cout<<setw(4)<<disp[i][j];
}
cout<<endl;
}
cout<<endl;
}

char array
Sample Lab Task 3:
Write a C++ program to take user name as input and print his/her name in reverse order. For example, reverse of
Ahmed Ali is ilA demhA.

Sample Code:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void main(){
char name[30];
cout << "Please enter your name: ";
gets_s(name);
cout << "My name before reverse order is : ";
puts(name);
cout << "\nMy name after reverse order is : ";
for (int i = strlen(name) - 1; i >= 0; i--){
cout << name[i];
}
cout << endl;
}

Functions (Pass By Value)


Sample Lab Task 4:

Write a function "check_prime()" to check whether a number is prime or not in the given intervals.

Sample Code:

int check_prime(int num);


int main(){
int n1, n2, i, flag;
cout << "Enter two numbers(intervals): ";
cin >> n1 >> n2;
cout << "Prime numbers between " << n1 << " and " << n2<<" are ";
for (i = n1 + 1; i<n2; i++)
{
flag = check_prime(i); //Passing a variable to function
if (flag == 0)
cout << i<<" ";
}
cout << endl;
return 0;
}

int check_prime(int num) /* User-defined function to check prime number*/


{
int j, flag = 0;
for (j = 2; j <= num / 2; j++){
if (num%j == 0){
flag = 1;
break;
}
}
return flag;
}

Sample Lab Task 5:

Write a function which takes an octal number from user and give its output in decimal.

int oct2dec(int oct) //Function Prototype

Sample Code:

#include "stdafx.h"
#include <iostream>
#include<math.h>
using namespace std;
int oct2dec(int oct)
{
int r, i = 0, s = 0;
while (oct != 0)
{
r = oct % 10;
s = s + r*(int)pow(8, i);
oct = oct / 10;
i++;
}
return s;
}
void main()
{
int n, num = 0;

cout << "Please Enter octal number: ";


cin >> n;
num = oct2dec(n);
cout << "The equivalent number in decimal is : " << num;
cout << endl;
}

Functions (Pass By Reference)


Sample Lab Task 6:

Write a function that calculates Sin and Cos values of an integer.

Sample Code:

#include <iostream>
#include <math.h> // for sin() and cos()

void GetSinCos(double dX, double &dSin, double &dCos)


{
dSin = sin(dX);
dCos = cos(dX);
}

int main()
{
double dSin = 0.0;
double dCos = 0.0;

// GetSinCos will return the sin and cos in dSin and dCos
GetSinCos(30.0, dSin, dCos);

std::cout << "The sin is " << dSin << std::endl;


std::cout << "The cos is " << dCos << std::endl;
return 0;
}

Passing Arrays to Functions by Reference

Sample Lab Task 7:

Write a function "bubblesort" which takes two arguments array itself & size and sort the array using bubble sort
algorithm in descending order.

Sample Code:

#include "stdafx.h"
#include <iostream>
using namespace std;
void bubblesort(int arr[], int size){
int i, j, temp;
for (i = 0; i<size; i++){
for (j = 0; j<size - 1; j++){
if (arr[j + 1]>arr[j]){
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10], i,j,temp;
cout << "Enter the elements of array: " << endl;
for (i = 0; i<10; i++){
cin >> arr[i];
}
cout << "The elements of array before sorting are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
bubblesort(arr, 10); //Passing an array to function
cout << "\nThe elements of array after sorting in descending order are: " << endl;
for (i = 0; i<10; i++){
cout << arr[i] << " ";
}
cout << endl;
}

Pointers (Allocation and De-allocation)


Sample Lab Task 8:

Write a program to dynamically allocate memory that takes the size of array entered by the user. Take input from the
user in array and output the number of elements entered by the user. Also de-allocate the used memory.

Sample Code:

#include <iostream>
using namespace std;
int main()
{
int i, n;
int * p;
cout << "Enter the size of array ? ";
cin >> i;
p = new int[i]; // memory allocation
if (p == NULL)
cout << "Error: memory could not be allocated";
else
{
for (n = 0; n<i; n++)
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n = 0; n<i; n++)
cout << p[n] << ", ";
delete[] p; // memory deallocation
}
cout << endl;
return 0;
}
Sample Lab Task 9:

Write a function void find_max_min(int *p,int n ,int* max, int* min); that will find max and min value from an
array of size n.

Sample Code:

#include "stdafx.h"
#include <iostream>
using namespace std;
void find_max_min(int *p, int n, int *max, int *min){
int i;
for (i = 0; i < n; i++)
{
if (*max <= *p)
*max = *p;
if (*min >= *p)
*min = *p;
p++;
}
}
void main()
{
int n, i, max,min;
int *p;
cout << "Enter the size of array: ";
cin >> n;
p = new int[n]; // memory allocation
cout << "Enter " << n << " elements in the array: \n";
for (i = 0; i < n; i++)
cin >> p[i];
cout << "Elements in the array are:\n";
for (i = 0; i < n; i++){
cout << p[i] << " ";
}
cout << endl;
max = p[0];
min = p[0];
find_max_min(p, n, &max, &min);
cout << "\nMinimum element in the array is: " << min;
cout << "\nMaximum element in the array is: " << max;
delete[] p; // memory deallocation
cout << endl<<endl;
}

Structures

Sample Code:

Creating a struct
struct my_struct
{
int x;
double y;
char z;
};
Assigning values to struct's members in main
#include<iostream>
using namespace std;
struct my_struct
{
int x;
double y;
char z;
};
int main()
{
my_struct a;
cout<<"Enter an integer"<<endl;
cin>>a.x;
cout<<"Enter a Double"<<endl;
cin>>a.y;
cout<<"Enter a Char"<<endl;
cin>>a.z;
cout<<"You entered these values"<<endl;
cout<<"Integer="<<a.x<<endl;
cout<<"Double="<<a.y<<endl;
cout<<"Char="<<a.z<<endl;
return 0;
}

Assigning values to struct's members in a function (pass by value)


#include<iostream>
using namespace std;
struct my_struct
{
int x;
double y;
char z;
};
my_struct input(my_struct b)
{
b.x=6;
b.y=78.89;
b.z='q';
return b;//returing struct
}
int main()
{
my_struct a,c;
a.x=5;
a.y=26.78;
a.z='w';

cout<<"Printing values in main before input function"<<endl;


cout<<"Integer="<<a.x<<endl;
cout<<"Double="<<a.y<<endl;
cout<<"Char="<<a.z<<endl;

c=input(a);//passing struct by value

cout<<"Printing values in main after input function"<<endl;


cout<<"Integer="<<c.x<<endl;
cout<<"Double="<<c.y<<endl;
cout<<"Char="<<c.z<<endl;

return 0;
}
Assigning values to struct's members in a function (pass by reference)
#include<iostream>
using namespace std;
struct my_struct
{
int x;
double y;
char z;
};
void input(my_struct &b)
{
b.x=6;
b.y=78.89;
b.z='q';
//it was passed by reference so, there is no need to return it
}
int main()
{
my_struct a;
a.x=5;
a.y=26.78;
a.z='w';

cout<<"Printing values in main before input function"<<endl;


cout<<"Integer="<<a.x<<endl;
cout<<"Double="<<a.y<<endl;
cout<<"Char="<<a.z<<endl;

input(a);//passing struct by reference. Notice the & operator in input functions arguments

cout<<"Printing values in main after input function"<<endl;


cout<<"Integer="<<a.x<<endl;
cout<<"Double="<<a.y<<endl;
cout<<"Char="<<a.z<<endl;

return 0;
}

Declaring array of struct and passing it to a function and returning from that
function

#include<iostream>
#include <conio.h>
using namespace std;
struct my_struct
{
int x;
double y;
char z;
};
my_struct* input(my_struct b[])
{
b[0].x=9;
b[0].y=2.78;
b[0].z='h';

b[1].x=45;
b[1].y=6.7;
b[1].z='m';

b[2].x=35;
b[2].y=2.8;
b[2].z='n';

return b;
}
void print(my_struct *b)
{
for(int i=0;i<3;i++)
{
cout<<"Integer="<<b[i].x<<endl;
cout<<"Double="<<b[i].y<<endl;
cout<<"Char="<<b[i].z<<endl;
}
}
void main()
{
my_struct a[3];
a[0].x=5;
a[0].y=26.78;
a[0].z='w';

a[1].x=4;
a[1].y=6.7;
a[1].z='r';

a[2].x=5;
a[2].y=27.8;
a[2].z='y';

cout<<"Printing values in print function, before calling input function"<<endl;


print(a);
my_struct *ptr=input(a);//array is always passed as reference
cout<<"Printing values in print function, after calling input function"<<endl;
print(ptr);
getch();
}
Lab Tasks

(Arrays)
Task 1:
Write a C++ program which computes sum of two square matrices using two dimensional array in C++.

SAMPLE OUTPUT:

Enter matrix 1
1 2 3
4 5 6
1 2 3

Enter matrix 2
3 4 5
8 7 6
9 10 11

Sum of matrices is
4 6 8
12 11 12
10 12 14

Task 2:
Write a C++ program that takes a string from user and all remove characters from a string except alphabets.

Sample Output:

Enter a string: p2'r"o@gram74s


Output String is: programs

(Functions)
Task 3:

Write a C++ function power () which takes two integers x and y as parameters and returns the
value of xy. The prototype of your function must be int power (int, int). Write appropriate main function to
test the power function.

Task 4:

Write a C++ function power () which takes addresses of two integers x and y as parameters. The
prototype of your function must be void power (int* x, int* y). Write appropriate main function to test the
power function.

(Passing Arrays to Functions)


Task 5:
Implement the insertion sort algorithm. The function prototype is:

void insertion Sort(int list[ ], int size)

Before implementing this function, try it out on a small array of size 5. Plan it properly on a piece of paper
and then continue.
It should sort the input array in ascending order.

(Pointers)
Task 6:
Complete the program by filling in the code. (Areas in bold) This problem requires that you study very
carefully the code already written to prepare you to complete the program.
// This program demonstrates the use of dynamic variables
#include <iostream>
using namespace std;

const int MAXNAME = 10;

int main()
{
int pos;
char * name;
int * one;
int * two;
int * three;
int result;

// Fill in code to allocate the integer variable one here

// Fill in code to allocate the integer variable two here

// Fill in code to allocate the integer variable three here

// Fill in code to allocate the character array pointed to by name

cout<< "Enter your last name with exactly 10 characters." <<endl;


cout<< "If your name has <10 characters, repeat last letter."
<< "Blanks at the end do not count." <<endl;

for (pos = 0; pos < MAXNAME; pos++)

cin>> // Fill in code to read a character into the name array


//without using a bracketed subscript
cout<< "Hi ";

for (pos = 0; pos < MAXNAME; pos++)

cout<<// Fill in code to a print a character from the name array


// without using a bracketed subscript

cout<<endl<<"Enter three integer numbers separated by blanks" <<endl;


// Fill in code to input three numbers and store them in the
// dynamic variables pointed to by pointers one, two, and three.
// You are working only with pointer variables

//echo print
cout<< "The three numbers are " <<endl;

// Fill in code to output those numbers


result = // Fill in code to calculate the sum of the three numbers
cout<< "The sum of the three values is " << result <<endl;
// Fill in code to deallocate one, two, three and name
return 0;
}

Structs
Task 7:

Create a struct teacher which has employee id, name, and designation pass it to function by value and
assign value to it now print it in main. Pass it to function by reference and assign value to it and print it in
main.

Task 8:

Create a struct student which has student id, name and cgpa as members. Create a static array of size 05 of
type struct student. Write a function Input_Record for taking record of 05 students. Write a function display
to print contents of array on output screen.
Task 9:

Create a struct employee which has employee_id, name and name, age as members. Create a dynamic array
of size 05 of type struct employee. Write a function Input_Record for taking record of 05 employees. Write
a function display to print contents of array on output screen.

Hint: syntax for creating dynamic array ptr=(cast-type*)malloc(byte-size)

____________________________________________________________The
End__________________________________________________________________

You might also like