Professional Documents
Culture Documents
#include <iostream>
int main()
{
int n;
cout << "Enter the size of the arrays: ";
cin >> n;
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>
int main()
{
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> 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 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;
}
Question #3:
#include <iostream>
int main()
{
int rows, cols;
cout << "Enter the number of rows and columns: ";
cin >> rows >> 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 upper half of the 2D array is: " << endl;
upper_half(arr, rows, cols);
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 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);
delete[] arr;
return 0;
}