Oop Lab 2
Oop Lab 2
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
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 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;
}
}
}
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
cout << firstvalue << "\t\t\t" << secondvalue << endl;// print firstvalue,
secondvalue
system("pause");