Object Oriented Programming Lab Manual (Lab 01)
Object Oriented Programming Lab Manual (Lab 01)
Revision of concepts that you’ve already discussed and implemented in Programming Fundamentals and
Structured Programming.
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;
}
Declaration and Implementation of 2D array
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++)
{
cin>>disp[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;
}
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;
}
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:
Write a function "check_prime()" to check whether a number is prime or not in the given intervals.
Sample Code:
Write a function which takes an octal number from user and give its output in decimal.
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;
Sample Code:
#include <iostream>
#include <math.h> // for sin() and cos()
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);
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 by Reference
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;
}
Lab Task (Passing Arrays to Functions)
Task 5:
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.
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;
}
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;
int main()
{
int pos;
char * name;
int * one;
int * two;
int * three;
int result;
//echo print
cout<< "The three numbers are " <<endl;
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;
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';
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';
input(a);//passing struct by reference. Notice the & operator in input functions arguments
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';
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.