You are on page 1of 5

Question # 1:

#include <iostream>

using namespace std;

void reverseArray(int *ptr1, int *ptr2, int n)


{
for (int i = 0; i < n; i++)
{
*(ptr2 + i) = *(ptr1 + n - i - 1);
}
}

bool isPalindrome(int *ptr2, int n)


{
for (int i = 0; i < n / 2; i++)
{
if (*(ptr2 + i) != *(ptr2 + n - i - 1))
{
return false;
}
}
return true;
}

int main()
{
int n;
cout << "Enter the size of the arrays: ";
cin >> n;

int *ptr1 = new int[n];


int *ptr2 = new int[n];

cout << "Enter the elements of the first array: ";


for (int i = 0; i < n; i++)
{
cin >> *(ptr1 + i);
}

reverseArray(ptr1, ptr2, n);

cout << "The first array is: ";


for (int i = 0; i < n; i++)
{
cout << *(ptr1 + i) << " ";
}
cout << endl;

cout << "The reversed array is: ";


for (int i = 0; i < n; i++)
{
cout << *(ptr2 + i) << " ";
}
cout << endl;

if (isPalindrome(ptr2, n))
{
cout << "The reversed array is a palindrome." << endl;
}
else
{
cout << "The reversed array is not a palindrome." << endl;
}
delete[] ptr1;
delete[] ptr2;
return 0;
}

Question # 2:
#include <iostream>

using namespace std;

bool isSymmetric(int **arr, int rows, int cols)


{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
if (arr[i][j] != arr[j][i])
{
return false;
}
}
}
return true;
}

int main()
{
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;

int **arr = new int*[rows];


for (int i = 0; i < rows; i++)
{
arr[i] = new int[cols];
}

cout << "Enter the elements of the 2D array: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}

cout << "The original 2D array is: " << endl;


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

cout << "The transpose of the 2D array is: " << endl;
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
{
cout << arr[j][i] << " ";
}
cout << endl;
}

if (isSymmetric(arr, rows, cols))


{
cout << "The 2D array is symmetric." << endl;
}
else
{
cout << "The 2D array is not symmetric." << endl;
}

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


{
delete[] arr[i];
}
delete[] arr;
return 0;
}

Question #3:
#include <iostream>

using namespace std;

void upper_half(int **arr, int rows, int cols)


{
for (int i = 0; i < rows / 2; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> cols;

int **arr = new int*[rows];


for (int i = 0; i < rows; i++)
{
arr[i] = new int[cols];
}

cout << "Enter the elements of the 2D array: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}

cout << "The original 2D array is: " << endl;


for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}

cout << "The upper half of the 2D array is: " << endl;
upper_half(arr, rows, cols);

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


{
delete[] arr[i];
}
delete[] arr;
return 0;
}

Question #4:
Shallow copy is a copy of an object that includes only a reference to its internal data, not a new copy
of the data itself. This means that the new object and the original object share the same memory
space. Any change to the data in one object will affect the other object as well.

On the other hand, deep copy is a copy of an object that creates a new copy of its internal data, so
that the original object and the new object have separate memory spaces. Any change to the data in
one object will not affect the other object

Question #5:
#include <iostream>
#include <cstdlib>

int *inputArray(int &size)


{
std::cout << "Enter the size of the array: ";
std::cin >> size;

int *arr = new int[size];

int count = 0;
int number;
std::cout << "Enter the elements of the array: ";
while (std::cin >> number)
{
if (count == size)
{
int *newArr = new int[size * 2];
for (int i = 0; i < size; i++)
{
newArr[i] = arr[i];
}
delete[] arr;
arr = newArr;
size *= 2;
}
arr[count++] = number;
}

return arr;
}

int main()
{
int size;
int *arr = inputArray(size);

std::cout << "Array after extending the size: ";


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

delete[] arr;

return 0;
}

You might also like