OOP Lab # 3
CL-217 Object Oriented Programming
Lab # 3
Objective
Pointers
Dynamic Memory Allocation
Functions and Pointers
Shallow and Deep Copy
1. Deep Copy: In this task, you need to pass the array to the function to gets its deep
copy, that returns a new array of same values. Perform the following operations;
Declare a pointer variable arr of type int and a size variable of type int.
Get the size of the array from the user.
Create a dynamic array arr of length size.
Create a function initializeArr that takes two parameters, pointer to array of type
int and size of type int. This function initializes the array with the values i × 3
where 0 ≥ i < size.
Call the initializeArr function and initialize the arr.
Create a function printArr, that takes the same parameters as the initializeArr.
Print the array arr.
Create a function deepCopy that takes the same arguments as the initializeArr
and returns a pointer of type int. This function creates a new dynamic array of
length size and copy each value of argument arr to new array. And returns the new
array pointer.
Call the function deepCopy and save the returned array in a new pointer variable.
Display the new copied array with the help of printArr.
Now delete the first array and verify that the copied array still exists safe.
Sample run is shown as:
2. Students marks .txt file is given to you. You have to find the grades of each student.
Each lab carries equal weightage. From the file read the number of students and number
of labs, then create a dynamic array to store all lab marks of each student. There will
be three arrays, one for the names of the students and the other 2D array for the marks
of the students. The third array for the grades of the students. Perform the following
operations:
Page 1
OOP Lab # 3
Read the number of students in noOfStudents variable.
Read the number of labs in noOfLabs variable.
Create a dynamic array to store the names of each students in namesOfStudents.
Create a dynamic 2D array studentMarks to store the lab marks. studentMarks
should have no. of rows equal to noOfStudents and no. of columns equal to
noOfLabs + 1. In last column of each student, store the Grand Total obtained
marks.
The third dynamic array studentGrades to store the grades of each student.
Assign grades according to following
Marks Grade
80 to 100 A
70 to 89 B
50 to 79 C
33 to 49 D
0 to 32 F
Write functions to perform the specific tasks. Pass the pointer array to the function
arguments. The output should be in grades.txt file in a tabular form. The output
should have student name, lab marks, grand total, and the grades in a tabular form.
Also calculate the min, max, and average of the class.
3. Write a function named getString that has a local 2D char array of 100 elements. The
function should ask the user to enter a sentence, and store the sentence in the array.
Then the function should dynamically allocate a char array just large enough to hold
the sentence, plus the null terminator. It should copy the sentence to the dynamically
allocated array, and then return a pointer to the array. Demonstrate the function in a
complete program.
char *copySen(char *ptr,int size)
4. Write a C++ program to add 2 matrices A and B. Matrices should have user defined
size. Use double pointers and dynamic memory allocation to create matrices. If both
have equal size, then result of A+B should be saved in C matrix otherwise terminate the
program. Write the following functions
void Input(int** p, int row, int col) // this function will input the p matrix
void Display(int** p, int row, int col) // this function will output the p
matrix to console
int** Sum(int** p, int row, int col, int ** q , int row2, int col2) //
this function will take sum of two matrices and return the resultant matrix.
Print the resultant matrix in main using Display() function.
Display A , B and C in matrix on console.
5. Write a C++ program to build a matrix that have different number of elements in each
row (different number of column in each row) using two-dimensional dynamic array. Your
program must contain two functions. One for filling the elements into your two dimensional
array and other for printing that array or matrix. Sample run is shown as:
Page 2
OOP Lab # 3
Best of luck
You are done with your exercise, submit on classroom at given time.
Page 3