-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_2D_array_to_1D.cpp
More file actions
53 lines (45 loc) · 1.02 KB
/
flat_2D_array_to_1D.cpp
File metadata and controls
53 lines (45 loc) · 1.02 KB
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
//
// main.cpp
// flat_2D_array_to_1D
//
// Created by Muhammad Yusry on 14/03/2023.
//
#include <iostream>
#define endl "\n"<<endl
using namespace std;
const int rows = 4, cols = 4;
const int flatSize = rows*cols;
void flat_2D(int arr[rows][cols], int flatArr[]) {
for (int r=0; r<rows; ++r) {
for (int c=0; c<cols; ++c) {
int indx = (r*cols)+c;
flatArr[indx] = arr[r][c];
}
}
for (int i=0; i<flatSize; ++i) {
cout<<flatArr[i]<<" ";
}
cout<<endl;
}
void flatArr_to_2D(int flatArr[], int arr[rows][cols]) {
for (int i=0; i<flatSize; ++i) {
int r = i/cols, c = i%cols;
arr[r][c] = flatArr[i];
}
for (int r=0; r<rows; ++r) {
for (int c=0; c<cols; ++c)
cout<<arr[r][c]<<" ";
cout<<endl;
}
}
int main() {
int flatArr[flatSize], arr[rows][cols] = {
{1,2,3,5},
{4,5,6,5},
{7,8,9,5},
{0,1,2,4}
};
flat_2D(arr, flatArr);
flatArr_to_2D(flatArr, arr);
return 0;
}