-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2darray.cpp
121 lines (96 loc) · 2.56 KB
/
2darray.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
using namespace std;
bool isPresent(int arr[][3], int target, int row, int col) {
for(int row=0; row<3; row++) {
for(int col=0; col<3; col++) {
if( arr[row][col] == target) {
return 1;
}
}
}
return 0;
}
//to print row wise sum
void printColSum(int arr[][3], int row, int col) {
cout << "Printing Sum -> " << endl;
for(int col=0; col<3; col++) {
int sum = 0;
for(int row=0; row<3; row++) {
sum += arr[row][col];
}
cout << sum << " ";
}
cout << endl;
}
//to print row wise sum
void printSum(int arr[][3], int row, int col) {
cout << "Printing Sum -> " << endl;
for(int row=0; row<3; row++) {
int sum = 0;
for(int col=0; col<3; col++) {
sum += arr[row][col];
}
cout << sum << " ";
}
cout << endl;
}
int largestRowSum(int arr[][3], int row, int col) {
int maxi = INT_MIN;
int rowIndex = -1;
for(int row=0; row<3; row++) {
int sum = 0;
for(int col=0; col<3; col++) {
sum += arr[row][col];
}
if(sum > maxi ) {
maxi = sum;
rowIndex = row;
}
}
cout << "the maximum sum is " << maxi << endl;
return rowIndex;
}
int main() {
//create 2 d array
int arr[3][3];
//int arr[3][4] = {1,2,3,4,5,6,7,8,9,10,14,16};
//int arr[3][4] = {{1,11,111,1111}, {2,22,222,2222}, {3,33,333,3333}};
cout << "Enter the elements " << endl;
//taking input -> row wise input
for(int row=0; row<3; row++) {
for(int col=0; col<3; col++) {
cin >> arr[row][col];
}
}
/*
//taking input -> col wise input
for(int col=0; col<4; col++) {
for(int row=0; row<3; row++) {
cin >> arr[row][col];
}
}
*/
cout << "Printing the array " << endl;
//print
for(int row=0; row<3; row++) {
for(int col=0; col<3; col++) {
cout << arr[row][col] << " ";
}
cout << endl;
}
/*
cout <<" Enter the element to search " << endl;
int target;
cin >> target;
if(isPresent(arr, target, 3, 3)) {
cout <<" Element found " << endl;
}
else{
cout <<" Not Found" << endl;
}
printColSum(arr, 3, 3 );
*/
int ansIndex = largestRowSum(arr,3,3);
cout << " Max row is at index " << ansIndex << endl;
return 0;
}