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

Oop Assignmnet 1

The document contains C++ code examples demonstrating object oriented programming concepts like arrays, pointers, functions, and dynamic memory allocation. It includes code to generate random numbers and sort arrays using different sorting algorithms like insertion sort, bubble sort, and selection sort. It also shows functions to concatenate, copy, compare strings and find string lengths. The last example demonstrates a 2D dynamic array with random number generation and deletion of dynamically allocated 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)
19 views

Oop Assignmnet 1

The document contains C++ code examples demonstrating object oriented programming concepts like arrays, pointers, functions, and dynamic memory allocation. It includes code to generate random numbers and sort arrays using different sorting algorithms like insertion sort, bubble sort, and selection sort. It also shows functions to concatenate, copy, compare strings and find string lengths. The last example demonstrates a 2D dynamic array with random number generation and deletion of dynamically allocated 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/ 12

[OBJECT ORIENTED

PROGRAMING]
20F-0114_BCS_2C
Question no.1
#include<iostream>
#include<iomanip>
#include<ctime>
#include<cstdlib>
using namespace std;
void main()
{
int List[10], sum = 0, average;
int* ptr;
ptr = List;
srand(time(0));
cout << "Elements of array are :";
cout << endl;
for (int i = 0; i < 10; i++)
{
*ptr = rand() % 10; //random number
cout <<setw(2)<< *ptr;
sum = sum + *ptr; //sum
ptr++; // Address of next element
}
cout << endl;
cout << "sum of array: " << sum;
average = sum / 10; //Average
cout << endl;
cout <<setprecision(3)<< "Average of array: " <<average;
cout << endl;
system("pause");
}
Question no.2

Insertion sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

void display(int* array, int size) // funcation for printing


{
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}

void insertionSort(int* array, int size)// insertion algo


{
int x, i;
for (int j = 1; j < size; j++) {
x = array[j];
i = j;
while (i > 0 && array[i - 1] > x)
{
array[i] = array[i - 1];
i--;
}
array[i] = x; //shifting right
}
}

void main()
{
int n, arr[100];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: " ;
for (int i = 0; i < n; i++)
{
arr[i]=rand()%10; //Genrating random numbers
cout << arr[i] << " ";
}
cout << endl;
cout << "Array after Sorting: ";
insertionSort(arr, n); //Calling the insertion function
display(arr, n);//Calling the displayfunction
system("pause");
}

Bubble sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

void bubble(int *array,int size) //function of bubble sortning


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

void main()
{
int n, arr[20];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: ";
for (int i = 0; i <n; i++)
{
arr[i] = rand() %10; //Genrating random numbers
cout << arr[i] << " ";
}
bubble(arr,n); //Calling the bubble funcation function
cout << endl;
system("pause");
}

Selection sorting
#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<ctime>
using namespace std;

void selection(int* array, int size)// selection algo


{
int num, smallest_ind;
cout << "Array after selection sortning\n";

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


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

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


if (array[j] < num)
{
num = array[j];
smallest_ind = j;
}
int x = array[i];
array[i] = array[smallest_ind]; //swaping
array[smallest_ind] = x;
}
for (int i = 0; i < size; i++)
cout << setw(2) << array[i];
}

void main()
{
int n, arr[100];
srand(time(0));
cout << "Enter size of array :";
cin >> n; //Size integer
cout << "Elements of array: \n";
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10; //Genrating random numbers
cout << arr[i] << " ";
}
cout << endl;
selection(arr, n); //Calling the section function
cout << endl;
system("pause");
}
Question no.3
#include <iostream>
#include <iomanip>
using namespace std;

char* strcat(char* s1, char* s2) //function of concatination


{
int s1time = 0, s2time = 0; int x = 0;
while (*(s1 + x) != '\0')
{
++s1time;
++x;
}
x = 0;
while (*(s2 + x) != '\0')
{
++s2time;
++x;
}
int result = s1time + s2time;
char* copy = new char[s1time + s2time];
for (int i = 0; i < s1time; i++)
{
copy[i] = s1[i];
}
for (int i = s1time, j = 0; i < result; i++, ++j)
{
copy[i] = s2[j];
}
return copy;
}
char* strcpy(char* s1, char* s2)//function of copy
{
int x = 0, s2time = 0;
while (*(s2 + x) != '\0')
{
++s2time;
++x;
}
for (int i = 0; i < s2time; i++)
{
s1[i] = s2[i];
}
return s1;
}

int strlen(char* s1)//function for checking length of string


{
int s1time = 0; int x = 0;
while (*(s1 + x) != '\0')
{
if (*(s1 + x) != ' ')
{
++s1time;
}
++x;
}
return s1time;
}
int strcmp(char* s1, char* s2) //function of camprision
{
int i = 0;
int check1 = 0;
while (s1[i] != '\0' || s2[i] != '\0')
{
if (s1[i] != s2[i])
{
check1 = 1;
break;
}
i++;
}
if (s1 > s2)
{
cout << "String 1> String 2 " << endl;
}
else if (s1 < s2)
{
cout << "String 2< String 1 " << endl;
}
else if (check1 == 0)
cout << "String 2 = String 1 " << endl;
return 0;
}
int main()
{
char* ptr1 = new char[20]; int i = 0, s1time = 0, s2time = 0;
char* ptr2 = new char[20];
cout << "Insert data in string 1" << endl;
cin.getline(ptr1, 20); //inserting the data
cout << "Insert data in string 2" << endl;
cin.getline(ptr2, 20); //inserting the data
while (*(ptr1 + i) != '\0')
{
++s1time;
++i;
}
i = 0;
while (*(ptr2 + i) != '\0')
{
++s2time;
++i;
}

char* cat = new char[s1time + s2time];


cat = strcat(ptr1, ptr2); // concatenation of two string .
cout << endl;
cout << "Concatenation of both strings" << endl;
for (int i = 0; i < s1time + s2time; i++)
{
cout << cat[i];
}
cout << endl;
cout << endl;
cout << "Comparison of strings\n " << endl;
int out = strcmp(ptr1, ptr2); // comparison of strings
if (ptr1 == ptr2)
{
cout << setw(1) << "Strings are equal \n" << endl;
}
else if (ptr1 > ptr2)
{
cout << setw(1) << "String 1 is greater than String 2\n" << endl;
}
else if (ptr1 < ptr2)
{
cout << setw(1) << "String 2 is greater than String 1\n" << endl;
}
for (int i = 0; i <= 0; i++)
{
cout << " " << endl;
}
char* a = new char[s1time + s2time];
a = strcpy(a, ptr2); //coping one string to the second
cout << setw(1) << "String after copying stirng 2 to first string" << endl;

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


{
cout << setw(1) << a[i];
}
cout << endl; //calling function of length of string
cout << endl;
cout << "Length of string 1 : " << setw(1) << strlen(ptr1) << endl;
cout << "Length of string 2 : " << setw(1) << strlen(ptr2) << endl;
cout << endl;
system("pause");
}

Question no.4
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int** main_arr, row, * col;
cout << "Insert Number of Rows of Array :";
cin >> row;
cout << endl;
main_arr = new int* [row];
col = new int[row];
for (int i = 0; i < row; i++)
{
cout << "Insert size of column " << i + 1<<" :"; //Inserting number of
columns
cin >> col[i];
cout << endl;
main_arr[i] = new int[col[i]]; //Daynamic array
}

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


{
for (int j = 0; j < col[i]; j++)
{
*(*(main_arr + i) + j) = rand() % 10; //Genrating randome numbers
cout << *(*(main_arr + i) + j) << " ";
}
cout << endl;
}
for (int i = 0; i < row; i++)
{
delete[] main_arr[i]; //deleting array rows
}
delete[] main_arr; //deleting array pointer
main_arr = NULL; // removing from memory
system("pause");
}

You might also like