Objective:
- Pointers in C++
- Reference Operator
- Dynamic Memory Allocation
Activity 1:
Given two integers x and y, find and print their sum using pointers.
Solution:
#include<iostream>
using namespace std;
int main()
{
int x, y, sum = 0;
cout << "Enter First Integer: " << endl;
cin >> x;
cout << "Enter Second Integer: " << endl;
cin >> y;
int* p = &x;
int* q = &y;
sum = *p + *q;
cout << "Sum = " << sum << endl;
return 0;
}
Activity 2:
A C++ program where you create an integer array of size 10. Your program will add 3 to each element of the array. You have to add to the elements using pointer only. Array subscript notation cannot be used (neither in addition nor while printing resultant array).
Note: *(ptr+i) is same as ptr[i]
Solution:
#include<iostream>
using namespace std;
int main()
{
int arr[10];
cout << "Enter 10 values for Array: " << endl;
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
int* p = arr;
cout << "New Array after Addition of 3: " << endl;
for (int j = 0; j < 10; j++)
{
cout << (*(p + j)) + 3 << endl;
}
return 0;
}
Activity 3:
- Write a function int* InputArray(int& size) that asks user to enter size of required array allocates the memory on heap, takes input in array and returns its pointer.
- Write a program void OutputArray(int* myArray, const int& size) that takes a pointer to an integer array and prints its data.
- Implement a function int* Intersection(int* setA, int& size1, int* setB, int& size2, int& size3) that finds intersection (common elements) of two sets (stored using arrays).
Solution:
#include<iostream>
using namespace std;
int* InputArray(int& size)
{
cout << "Enter the Size of the Array:" << endl;
cin >> size;
int* array = new int[size];
cout << "Enter Values for the Array: " << endl;
for (int i = 0; i < size; i++)
{
cin >> array[i];
}
return array;
}
void OutputArray(int* array, const int& size)
{
cout << endl;
cout << " { ";
for (int i = 0; i < size; i++)
{
cout << array[i];
if (i < size - 1)
{
cout << ", ";
}
}
cout << " }" << endl;
}
int* Intersection(int* array1, int& size1, int* array2, int& size2, int& size3)
{
int* array = new int[size3];
int k = 0;
for (int i = 0; i < size1; i++)
{
for (int j = 0; j < size2; j++)
{
if (array1[i] == array2[j])
{
array[k] = array1[i];
k++;
}
}
}
size3 = k;
return array;
}
int main()
{
int size1;
int size2;
int* array1;
int* array2;
cout << "For Array 1: " << endl;
array1 = InputArray(size1);
cout << "For Array 2: " << endl;
array2 = InputArray(size2);
cout << "Array 1: " << endl;
OutputArray(array1, size1);
cout << "Array 2: " << endl;
OutputArray(array2, size2);
int* intersection;
int size3 = size1 + size2;
intersection = Intersection(array1, size1, array2, size2, size3);
cout << "Intersection: " << endl;
OutputArray(intersection, size3);
delete[]array1;
delete[]array2;
delete[]intersection;
return 0;
}
Activity 4:
a) Write a function int** AllocateMemory(int& rows, int& cols) that takes size of matrix (rows and columns) from user, allocates memory for the matrix and return its pointer.
b) Write a function void InputMatrix(int** matrix, const int rows, const int cols) which takes input the values in matrix from user(console).
c) Write a function void DisplayMatrix(int** matrix, const int& rows, const int& cols) that displays the matrix in proper format.
d) Write a function called maxRow that takes as parameters a pointer to a 2D array and its dimensions. It should return the largest element in each row of the array. Since there is more than one row in 2D array, you have to return a dynamic array that contains largest of each row.
For example, if the Sample Matrix is
1 4 8
9 1 6
5 7 2
Your function will return array containing maximum elements of all the columns i.e.
8, 9, 7
e) Write a function void DeallocateMemory(int** matrix, const int& rows) that deallocates all the memory.
f) (Concatinate tables ) It takes two 2D arrays and returns a new 2D array that is concation on y or x axis.
int ** concat_table (int **table1, int **table2, int row1, int col1, int row2, int col2, bool axis); //axis 0 mean x axis 1 means Y axis.
Array 1
1,2,3
4,4,5
3,4,6
Array 2
5,8,9
4,9,4
4,6,0
If axis = 0 (x-axis)
Output
1,2,3,5,8,9
4,4,5,4,9,4
3,4,6,4,6,0
If axis = 1 (y-axis)
Output
1,2,3
4,4,5
3,4,6
5,8,9
4,9,4
4,6,0
Solution:
#include<iostream>
using namespace std;
int** AllocateMemory(int& rows, int& columns)
{
cout << "Enter the Number of Rows: " << endl;
cin >> rows;
cout << "Enter the Number of Columns: " << endl;
cin >> columns;
int** array = new int* [rows];
for (int i = 0; i < rows; i++)
{
*(array + i) = new int[columns];
}
return array;
}
void InputMatrix(int**& Array2D, const int rows, const int columns)
{
cout << "Enter Values for Matrix: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << "[" << i << "]" << "[" << j << "] = ";
cin >> *(*(Array2D + i) + j);
cout << endl;
}
}
}
void DisplayMatrix(int** Array2D, const int& rows, const int& columns)
{
cout << "Given Matrix: " << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
cout << *(*(Array2D + i) + j) << " ";
}
cout << endl;
}
cout << endl;
}
int* maxRow(int** Array2D, const int& rows, const int& columns, int& size)
{
int largest;
size = columns;
int* array = new int[size];
for (int i = 0; i < columns; i++)
{
largest = Array2D[0][columns];
for (int j = 1; j < rows; j++)
{
if (largest < *(*(Array2D + i) + j))
{
largest = *(*(Array2D + i) + j);
}
}
array[i] = largest;
}
return array;
}
void DeallocateMemory(int** Array2D, const int& rows)
{
for (int i = 0; i < rows; i++)
{
delete[] * (Array2D + i);
}
delete[] Array2D;
}
void concat(int** A, int** B, int row1, int column1, int row2, int column2)
{
int choice;
cout << "Press 0: For x-axis" << endl;
cout << "Press 1: For y-axis" << endl;
cin >> choice;
if (choice == 0)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
cout << *(*(A + i) + j) << " ";
}
for (int j = 0; j < column2; j++)
{
cout << *(*(B + i) + j) << " ";
}
cout << endl;
}
}
if (choice == 1)
{
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < column1; j++)
{
cout << *(*(A + i) + j) << " ";
}
cout << endl;
}
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < column2; j++)
{
cout << *(*(B + i) + j) << " ";
}
cout << endl;
}
}
}
int main()
{
int rows, columns;
int** Array2D;
Array2D = AllocateMemory(rows, columns);
InputMatrix(Array2D, rows, columns);
DisplayMatrix(Array2D, rows, columns);
int size;
int* maxRowArray;
maxRowArray = maxRow(Array2D, rows, columns, size);
cout << "The Maximum Values in each Row are: " << endl;
for (int i = 0; i < size; i++)
{
cout << maxRowArray[i] << " ";
}
DeallocateMemory(Array2D, rows);
cout << endl;
cout << endl;
cout << "Concatenate Table: " << endl;
int** A;
int** B;
int row1, row2, column1, column2;
cout << "For A:" << endl;
A = AllocateMemory(row1, column1);
cout << "For B:" << endl;
B = AllocateMemory(row2, column2);
cout << "For A:" << endl;
InputMatrix(A, row1, column1);
DisplayMatrix(A, row1, column1);
cout << endl;
cout << "For B:" << endl;
InputMatrix(B, row2, column2);
DisplayMatrix(B, row2, column2);
concat(A, B, row1, column1, row2, column2);
DeallocateMemory(A, row1);
DeallocateMemory(B, row2);
return 0;
}