Ds Lab 2
Ds Lab 2
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.
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:
float mark[5];
int mark[5] = {19, 10, 8, 17, 9};
int mark[] = {19, 10, 8, 17, 9};
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
Diagonal matrix or not.
NOTE: A diagonal matrix is a matrix that is both upper triangular and lower triangular. i.e., all the
elements above and below the principal diagonal are zeros.
Dynamic Memory Allocation for arrays:
Memory in your C++ program is divided into two parts
1. The stack − All variables declared inside the function will take up memory from the stack.
2. The heap − this is unused memory of the program and can be used to allocate the memory
dynamically when the program runs.
A dynamic array is an array with a big improvement: automatic resizing.
One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements
your array will hold ahead of time.
A dynamic array expands as you add more elements. So, you don't need to determine the size ahead of
time.
Strengths:
1. Fast lookups. Just like arrays, retrieving the element at a given index takes O(1) time.
2. Variable size. You can add as many items as you want, and the dynamic array will expand to
hold them.
3. Cache-friendly. Just like arrays, dynamic arrays place items right next to each other in memory,
making efficient use of caches.
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.
Example:
Single Dimensional Array:
#define N 10
// Dynamically Allocate Memory for 2D Array in C++
int main(){
// dynamically allocate memory of size N
// assign values to allocated memory
// print the 1D array
// deallocate memory
}
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(){
// dynamically create array of pointers of size M
int** A = new int*[M];
/* initialize random seed: */
srand (time(NULL));
// dynamically allocate memory of size N for each row
for (int i = 0; i < M; i++)
A[i] = new int[N];
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 nothing, but it is 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
// deallocate memory using delete[] operator
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).