-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaggeredArray.cpp
53 lines (41 loc) · 1011 Bytes
/
jaggeredArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<vector>
using namespace std;
int main(){
// STATIC
int row1[] = {1,2,3,4};
int row2[] = {5,6};
int row3[] = {7,8,9};
int *jaggered[] = {row1, row2, row3};
int sizes[] = {4,2,3};
cout << "Elements in matrix form" << endl;
for(int i=0; i<3; i++){
int* row = jaggered[i];
for(int j=0; j<sizes[i]; j++){
cout<<*(row+j)<<" ";
}
cout<<endl;
}
//DYNAMIC
int row,col;
row=3;
int** arr = new int*[row];
int sizes2[] = {4,2,3};
for(int i=0; i<row; i++){
*(arr+i)=new int[sizes2[i]];
}
int num=1;
for(int i=0; i<row; i++){
for(int j=0; j<sizes2[i]; j++){
arr[i][j]=num++;
}
}
cout<<"Elements in matrix form"<<endl;
for(int i=0; i<row; i++){
for(int j=0; j<sizes2[i]; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
return 0;
}