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

Oop Lab 2

The document discusses object oriented programming and pointers in C++. It contains 9 questions that demonstrate various pointer concepts like: 1. Declaring and initializing pointer variables and passing pointers to functions. 2. Using pointers to read from and write to arrays, pass arrays to functions, and search arrays. 3. Demonstrating how pointers point to memory locations and can be reassigned. 4. Traversing arrays using pointers, pointer arithmetic, and call by reference. The questions cover fundamental pointer operations and show how pointers can be used to reference and manipulate data in memory.

Uploaded by

Talha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Oop Lab 2

The document discusses object oriented programming and pointers in C++. It contains 9 questions that demonstrate various pointer concepts like: 1. Declaring and initializing pointer variables and passing pointers to functions. 2. Using pointers to read from and write to arrays, pass arrays to functions, and search arrays. 3. Demonstrating how pointers point to memory locations and can be reassigned. 4. Traversing arrays using pointers, pointer arithmetic, and call by reference. The questions cover fundamental pointer operations and show how pointers can be used to reference and manipulate data in memory.

Uploaded by

Talha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

OBJECT ORIENTED

PROGRAMING
20F-0114_BSC_2C
Question no.1
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<fstream>
using namespace std;
int arr[15], smallest_ind, num, lower, upper;
bool bub = true; //using global variable

void numArray() //funcation of assigning values to array


{
cout << "Enter the value of upper range :";
cin >> upper;
cout << endl;
cout << "Enter the value of lower range :";
cin >> lower;
cout << endl;
srand(time(0));

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


{
arr[i] = (rand() % (upper - lower + 1)) + lower; // range of number between
10 to 99
}
//cout << endl;
for (int i = 0; i < 15; i++)
{
cout << setw(3) << arr[i] << endl;
}
}

void bubble() //function of bubble sortning


{
cout << endl;
cout << "Elements after Bubble sorting\n";
while (bub)
{
bub = false;
for (int i = 0; i < 15; i++)
{
if (arr[i] > arr[i + 1])
{ //swaping the elements of array
int x = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = x;
bub = true;
}
}
}
for (int i = 0; i < 15; i++)
cout << setw(3) << arr[i] << endl;
}

void selection() //funcation of selection sortning


{
cout << "Array after selection sortning\n";

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


{
num = arr[i];
smallest_ind = i;

for (int j = i; j < 15; j++)


if (arr[j] < num)
{
num = arr[j];
smallest_ind = j;
}
int x = arr[i];
arr[i] = arr[smallest_ind]; //swaping
arr[smallest_ind] = x;
}
for (int i = 0; i < 15; i++)
cout << setw(3) << arr[i] << endl;
}

void file() //fuction of making file


{
ofstream file("num.txt");
if (!file.is_open())
{
cout << "ERROR\n";
}
file << "Following array is Bubble sorted :";
for (int i = 0; i < 15; i++)
file << setw(3) << arr[i] << endl;
file << "Following array is Selection sorted :";
for (int i = 0; i < 15; i++)
file << setw(3) << arr[i] << endl;
file.close();
}

void main() //main calling all functions


{

numArray();
bubble();
selection();
file();
system(" pause ");
}
Question no.2
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int readData[15], key, key1, x = 0, y = 15, mid;//global variables
//int item;

int linear(int readData[], int) //linear search


{
for (int i = 0; i < 15; i++)
{
if (readData[i] == key)
return i;
}
return -1;
}

int binary(int readData[], int)// binary search


{
while (x <= y)
{
mid = (x + y) / 2;
if (readData[mid] == key)
{
return mid;
}
else if (readData[mid] > key)
{
y = mid - 1;
}
else
{
x = mid + 1;
}
}
return -1; //
}

int main() //main holding file function


{
ifstream file("num.txt");
if (!file.is_open())
{
cout << "Error\n";
}
while (!file.eof())
{
{
if (!file.eof())
{
for (int i = 0; i < 15; i++)
{
file >> readData[i]; //reading data from file
//cout << readData[i] << " " << endl;

}
}
}
int arr[15];
for (int i = 0; i < 15; i++)
{
arr[i] = readData[i];// readed data giving to another array
cout << arr[i] << endl;

}
cout << "Enter the element to find its index in binary serach :";
cin >> key;
cout << binary(arr, key) << endl; // key/key1 is loaction element to
find
cout << "Enter the element to find its index in linear serach :";
cin >> key1;
cout << linear(arr, key1) << endl;
system("pause");
}
Question no.3
#include<iostream>
using namespace std;
void main()
{
int firstvalue = 5, secondvalue = 15;
int* p1;
int* p2;
//Step 1
p1 = &firstvalue;
//cout << p1<<endl;
//Step 2
p2 = &secondvalue;
//cout << p2 << endl;
//Step 3
*p1 = 10;
//cout << *p1 + 5 << endl;
//step 4
*p2 = *p1;
//cout << *p2 <<endl;
//step 5
p1 = p2;
//cout << p1 << endl;
//step 6
*p1 = 20;
//cout << *p1 << endl;
//step 7
cout << firstvalue << " " << secondvalue << endl;
system("pause");
}

Question no.4
#include<iostream>
#include<iomanip>//library for setwidth
using namespace std;
void main()
{
double balance[5] = { 1000.0, 2.01, 3.4, 17.0, 50.40 };//array elements
double* p;// pointer of type double
p = balance;
for (int i = 0; i < 5; i++)
{
cout << setw(3) << *p << endl;// printing contents of pointer
p++;
}
system("pause");
}
Question no .5
#include<iostream>
#include<iomanip>
using namespace std;
char arr[10];
char* ptr; //pointer of type char //global variable
int main()
{
ptr = arr;
cout << "Enter name" << endl;

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


{
cin >> *(ptr + i);// input the data
}
for (int i = 9; i >= 0; i--)
{
cout << *(ptr + i);
}
cout << endl;
system("pause");
}
Question no.6
#include<iostream>
#include<iomanip>

using namespace std;


int arr[5] = { 3,9,1,15,3 }, a = 0, x; //global variable
int* ptr; //pointer of int type
int main()
{
ptr = arr;
for (int i = 0; i < 5; i++)
{
cout << *ptr << setw(2) << " " << ptr << endl;
ptr++;
} // values in unorder way
cout << endl;
for (int i = 0; i < 5; i++)
{
ptr--;
cout << *ptr << setw(2) << " " << ptr << endl; // values in order way
}
for (int i = 0; i < 5; ++i)
{
for (int j = 1; j < 5; ++j)
{
if (i == j)
{
continue; // nested loop
}

if (arr[i] == arr[j])// check for equivlant element of arrays


{
if (a == arr[j])
{
continue;
}
a = arr[j];
cout << arr[j];

}
}
}
system("pause");
}

Question no.7
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ //part 1
int const size = 10;
double number[size] = { 0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9 };
cout << endl;

//part 2
double* nptr;
nptr = number;

//part 3
for (int i = 0; i < 10; i++)
{
cout << setprecision(1) << fixed << number[i] << "\t";
}
cout << endl;
cout << endl;
//part 4
double* ptr1, * ptr2;
ptr1 = number;
ptr2 = &number[0];
cout << ptr1 << "\t" << ptr2 << endl;
cout << endl;

//part 5
for (int i = 0; i < 10; i++)
{
cout << *nptr << "\t";
nptr++;
}
cout << endl;
cout << endl;
//part 6
for (int i = 0; i < 10; i++)
{
cout << *ptr1 << "\t";
ptr1++;
}
cout << endl;
cout << endl;
//part 7
for (int i = 0; i < 5; ++i)
cout << setprecision(1) << fixed << *(number+i) << "\t";

//part 8
cout << endl;
cout << endl;
nptr = &number[8];
cout << nptr<<"\t"<<*(number+8);

//part 9
cout << endl;
cout << endl;
nptr = &number[5];
nptr -= 4;
cout << nptr<<"\t"<<*nptr;
cout << endl;

system("pause");

}
Question no.8
#include <iostream>
#include <iomanip>
using namespace std;
int number, x,* ptr;// global variable
int prime() // function checking the prime number
{
int count=0;
cout << "Please enter the number to check if it's prime or not : " << endl;
cin >> number;
ptr = &number;
if (number == 0)
{
cout << "\n" << number << " This number is not prime";//0 is not a prime
number
}
else {
for (x = 2; x < *ptr; x++)
if (*ptr % x == 0)
count++;// logic of prime number
}
if (count > 1)
cout << "This number is not prime." << *ptr << "\n" <<" false";
else
cout << "This is prime number." << *ptr<<"\n"<<"true";
return true;
}

int main()
{// main fuction
prime();
cout << endl;
system("pause");
return 0;
}

Question no.9
#include <iostream>
#include <iomanip>
using namespace std;
int* p1, * p2, **p3, **p4;
int firstvalue = 5, secondvalue = 15;
void main()
{
p1 = &firstvalue;// p1 = address of firstvalue

p2 = &secondvalue;// p2 = address of secondvalue

p3 = &p1;// p3 = address of firstpointrer

p4 = &p2; // p4 = address of secondpointer

*p1 = 10;// value pointed by p1 = 10

*p2 = *p1;// value pointed by p2 = value pointed by p1

p1 = p2;// p1 = p2 (address of pointer is copied or not)

cout << p1 << "\t""\t" << p2 << endl;


p3 = p4;// p3 =p4 (check address of pointer is copied or not)

cout << p3 << "\t""\t" << p4 << endl;

*p1 = 20;// value pointed by p1 = 20

cout << firstvalue << "\t\t\t" << secondvalue << endl;// print firstvalue,
secondvalue
system("pause");

You might also like