-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsum.cpp
More file actions
38 lines (33 loc) · 780 Bytes
/
sum.cpp
File metadata and controls
38 lines (33 loc) · 780 Bytes
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
#include <iostream>
using namespace std;
int totalSum(int input[][501], int n);
int main() {
int n;
cin >> n;
int arr[10][501];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
int sum = totalSum(arr, n);
cout << sum << endl;
return 0;
}
int totalSum(int input[][501], int n)
{
int sum = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
int ele = input[row][col];
if (row == col || row == n - col - 1) {
sum += ele;
continue;
}
if (row == 0 || row == n-1 || col == 0 || col == n-1) {
sum += ele;
}
}
}
return sum;
}