Sei sulla pagina 1di 3

National University of Computer and Emerging Sciences

Laboratory Manual # 2
for
Object Oriented Programming

Course Instructor Mr. Bismillah Jan


Lab Instructor(s) Mr. Muhammad Saddam
Mr. Abdul Wahab
Date 09/04/2019
Semester Fall 2019

Department of Computer Science


FAST-NU, Lahore, Pakistan
Introduction to Debugging and Pointers Revision
Objectives:
By practicing this lab students will be able to:
 Understand 1D Dynamic arrays.
 Understand 2D Dynamic arrays(char, int)

Problem#1
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.
Write a function void InitializeMatrix(int** matrix, const int& rows, const int& cols) that
initializes the matrix elements to 0.
Write a function void DisplayMatrix(int** matrix, const int& rows, const int& cols) that
displays the matrix in proper format.
Write a function void DeallocateMemory(int** matrix, const int& rows) that deallocates all the
memory.
Test your program. An example run is given below.
Enter total rows:4
Enter total columns:3 void main(){
The array is: int rows=4, cols=3;
000 int ** matrix = AllocateMemory(rows,cols);
000 InitializeMatrix(matrix, rows, cols);
DisplayMatrix(matrix, rows, cols);
000 DeallocateMemory(matrix, rows);
000
}

b. Write a function void InputMatrix(int** matrix, const int rows, const int cols) which takes input
the values in matrix from user(console).
Write a function called maxCol that takes as parameters a pointer to a 2D array and its dimensions.
It should return the largest element in each column of the array. Since there is more than one column
in 2D array, you have to return an array that contains largest of each column.
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.
9, 7, 8
A Sample main function is given below for your help.

void main(){
int rows, cols;

//take input from user for rows and cols

int ** matrix = AllocateMemory(rows,cols);


InitializeMatrix(matrix, rows, cols);
DisplayMatrix(matrix, rows, cols);
InputMatrix(matrix, rows, cols);
int * maxColValues = maxCol(matrix,rows, cols);
for(int i=0; i<cols; i++)
cout<< *(maxColValues + i) <<”,”;
cout<<endl;

//deallocate matrix
//delete maxColValues

Problem#2
Implement the C++ Functions,

int myTokenizer (char *data, char**& list_tokens, char delim)


void printTokens (const char** list_tokens, int size)

Don’t use any built-in function of tokenization.

The function “myTokenizer” should store the tokens in the list_tokens, every row of list_tokens contains a token
and split the data on the basis of the delimiter (delim). The function returns the number of possible tokens. Call
the function in main and print the list_tokens.

Sample Run
Input: my,name,is,,Mr.,Ali Input: _I_Go_FAST_Nu_
Delim : , Delim: _
Tokens are : Tokens are:
my I
name Go
is FAST
Mr. Nu
Ali

Potrebbero piacerti anche