Lab 02
Lab 02
Note:
● Maintain discipline during the lab.
● Listen and follow the instructions as they are given.
● Just raise your hand if you have any problem.
● Completing all tasks of each lab is compulsory.
● Get your lab checked at the end of the session.
Lab Title: Learn to implement a Dynamic Safe Array with one/two dimensional pointers.
Objectives: Get the knowledge of how dynamic memory is used to implement 1D and 2D
arrays which are more powerful as compared to the default array mechanism of the programming
language C/C++ supports.
Tool: Dev C++ (You can also use any editor of your choice)
1D & 2D Array:
A one-dimensional array (or single dimension array) is a type of linear array. Accessing its
elements involves a single subscript which can either represent a row or column index.
Syntax:
char name[5];
int mark[5] = {5,11,14,65,85};
int mark[] = {5,11,14,65,85};
Like a 1D array, a 2D array is a collection of data cells, all of the same type, which can be given
a single name. However, a 2D array is organized as a matrix with a number of rows and columns.
Syntax:
float x[3][4];
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[][3] = {{1, 3, 0}, {-1, 5, 9}};
int c[2][3] = {1, 3, 0, -1, 5, 9};
Task # 1
Write a C++ program to read elements in a matrix and check whether the matrix is an Identity
matrix or not.
Weaknesses:
1. Slow worst-case appends. Usually, adding a new element at the end of the dynamic array takes
O (1) time. But if the dynamic array doesn't have any room for the new item, it'll need to expand,
which takes O(n) time.
2. Costly inserts and deletes. Just like arrays, elements are stored adjacent to each other. So adding
or removing an item in the middle of the array requires "scooting over" other elements, which
takes O(n) time.
Factors impacting performance of Dynamic Arrays:
The array's initial size and its growth factor determine its performance. Note the following points:
1. If an array has a small size and a small growth factor, it will keep on reallocating memory
more often. This will reduce the performance of the array.
2. If an array has a large size and a large growth factor, it will have a huge chunk of unused
memory. Due to this, resize operations may take longer. This will reduce the performance of the
array.
The new Keyword:
In C++, we can create a dynamic array using the new keyword. The number of items to be
allocated is specified within a pair of square brackets. The type name should precede this. The
requested number of items will be allocated.
Syntax:
int *ptr1 = new int;
int *ptr1 = new int[5];
int *array { new int[10]{}};
int *array { new int[10]{1,2,3,4,5,6,7,8,9,10}};
Resizing Arrays:
The length of a dynamic array is set during the allocation time. However, C++ doesn't have a
built-in mechanism of resizing an array once it has been allocated. You can, however, overcome
this challenge by allocating a new array dynamically, copying over the elements, then erasing the
old array.
Example:
Single Dimensional Array:
#include <iostream>
using namespace std;
main(){
int* darray = new int[3] {1,2,3}; //Initializing a dynamic array
cout << *darray+1 << endl;
cout << darray[2];
delete[] darray; //Deleting the dynamic array to save memory space
//cout <<darray[2] << endl; If we try to print the array we would get random values
}
Two Dimensional Array Using Array of Pointers:
We can dynamically create an array of pointers of size M and then dynamically allocate memory
of size N for each row as shown below.
Example:
// Dynamically Allocate Memory for 2D Array in C++
int main(){
int** A = new int*[M]; // dynamically create array of pointers of size M
srand (time(NULL)); /* initialize random seed: */
for (int i = 0; i < M; i++) // dynamically allocate memory of size N for each row
A[i] = new int[N]; // assign values to allocated memory
cout << A[i]; // print the 2D array
0 * * *
1 * * *
2 *
3 * *
4 * * *
Write a method to check whether two people have a common friend. For example, in the
example above, 0 and 4 are both friends with 3 (so they have a common friend), whereas 1 and
2 have no common friends.
Jagged Array:
Jagged array is similar to an array but the difference is that its an array of arrays in which the
member arrays can be in different sizes.
Example:
int *arr = new int*[3];
int Size[3];
int i,j,k;
for(i=0;i<3;i++){
cout<<"Row "<<i+1<< " size: ";
cin>>Size[i];
arr[i] =new int[Size[i]];
}
for(i=0;i<3;i++){
for(j=0;j<Size[i];j++){
cout<<"Enter row " <<i+1<<" elements: ";
cin>>*(*(arr + i) + j);
}
}
// print the array elements using loops
// deallocate memory using delete[] operator as mentioned in the previous example
Task # 5
Write a program to calculate the GPA of students of all subjects of a single semester. Assume all
the courses have the same credit hour (let’s assume 3 credit hours). Do this task with Jagged
Array?