POINTER & DYNAMIC VARIABLES
1. Theory Part
▪ What is the pointer?
▪ What advantages do pointers give us?
▪ What operator allocates memory dynamically? (Give the name, example, and
return value)
▪ Why is it important to subsequently deallocate that memory?
▪ What operator deallocates memory? (Give the name, example, and the
working progress)
▪ What is the disadvantage to a dynamically allocated array?
▪ Write the code to declare an array of 10 pointers to array of 100 characters
▪ What is pointer arithmetic, and how does it differ from normal arithmetic?
(give an example)
▪ What is a segmentation fault, and how can it be caused by pointers?
▪ What happens if you try to access memory that has been deallocated?
▪ What is the significance of pointer types in pointer arithmetic? (Explain how
the type of a pointer influences arithmetic operations performed with that
pointer.)
2. Pratical Part
▪ Write a C++ function to count the number of vowels and consonants in a
string using a pointer
void countVC(char* str, int& vowels, int& consonants);
▪ Write a function that takes in an integer n and a pointer to an array of n
integers and returns a new array that contains the elements of the original
array in reverse order
int* reverseArray(int* arr, int n);
▪ Write a C++ function to concatenate two strings using pointer.
void StringConcate(char* s1, char* s2, char*& des_str);
▪ A polynomial 𝑓(𝑥) = 𝑎0 𝑥 0 + 𝑎1 𝑥1 + ⋯ + 𝑎𝑛 𝑥 𝑛 , where 𝑎𝑖 is a coefficient, is
represented by the following structure:
struct Poly {
int n; //May be changed during computation
int* a; //Should be allocated dynamically
};
Implement function:
o addPoly to perform ℎ = 𝑓 + 𝑔 :
int addPoly(Poly f, Poly g, Poly & h);
o MulPoly to perform ℎ = 𝑓 ∗ 𝑔
int mulPoly(Poly f, Poly g, Poly & h);
▪ Given a 2D array (matrix) of integers with dimensions 𝑛 × 𝑚 students are
required to write a program and supporting functions to extract a submatrix
of size 𝑘 × ℎ where 𝑘 ≤ 𝑛 and ℎ ≤ 𝑚. The extracted submatrix should
have the largest sum compared to all possible submatrices of the same size
extracted from the original matrix.
The expected result:
Input dim: 4 3 [Enter]
Input Arr: 1 2 3 4 5 6 7 8 9 10 11 12 [Enter]
Input subArr dim: 2 2
Output:
8 9
11 12
Note: Students must create a new matrix for the extracted submatrix.
Merely printing the submatrix to the console following a pattern is not
allowed.
▪ Write a function to display 3-d matrix in C++ with the below prototype:
void print3DMatrix(int*** matrix, int depth, int rows, int cols)
▪ Write a function to extract all 𝑝 × 𝑞 matrices submatrices containing
exactly 𝑘 prime numbers from a given 2D matrix (A, rows, cols) and
returns a 3D array containing these submatrices. The function prototype is:
int*** findSubmatrices(int** A, int rows, int cols, int p, int q, int k, int& depth)