-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrays.cpp
More file actions
83 lines (74 loc) · 1.29 KB
/
Copy patharrays.cpp
File metadata and controls
83 lines (74 loc) · 1.29 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
using namespace std;
void PrintHourGlass(int arr[3][3]) {
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i == 1) {
if (i == j) {
cout << arr[i][j] << " ";
}
else {
cout << " ";
}
}
else {
cout << arr[i][j] << " ";
}
}
cout << endl;
}
}
int HourGlassSum(int arr[3][3]) {
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i == 1) {
if (i == j) {
sum += arr[i][j];
}
}
else {
sum += arr[i][j];
}
}
}
return sum;
}
void TwoDArrayBasic() {
// Initialization
const int rows = 3, cols = 3;
int arr[rows][cols] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
/*for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}*/
PrintHourGlass(arr);
int sum = HourGlassSum(arr);
cout << "Sum: " << sum;
}
void AllHourGlasses() {
const int rows = 4, cols = 4;
int arr[rows][cols] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
// create a 2d array of 3x3 from above array and pass it to function
int subArr[3][3];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
subArr[i][j] = arr[i][j];
}
}
PrintHourGlass(subArr);
}
int main() {
TwoDArrayBasic();
return 0;
}