-
Notifications
You must be signed in to change notification settings - Fork 0
/
23_Largest_RowsumIN_2DArray.cpp
79 lines (58 loc) · 1.39 KB
/
23_Largest_RowsumIN_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
#include <iostream>
using namespace std;
void rowSum(int matrix[][3], int rows, int cols){
// int sum=0; when we want 1 st sum also.
for (int i = 0; i < 3; i++)
{
int sum=0;
for (int j= 0; j < 3; j++)
{
// sum=sum+matrix[i][j];
sum += matrix[i][j];
}
cout<<sum<<endl;
}
}
int largestRowSum(int matrix[][3],int rows, int cols){
int maxi=INT32_MIN;
int rowIndex=-1;
for (int i = 0; i < 3; i++)
{
int sum=0;
for (int j = 0; j < 3; j++)
{
sum+=matrix[i][j];
}
if(sum>maxi){
maxi=sum;
rowIndex=i;
}
}
cout<<"The maximum sum of the row is:"<<maxi<<endl;
return rowIndex;
}
int main(){
int arr[3][3];
cout<<"Enter the elements:"<<endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin>>arr[i][j];
}
}
cout<<"Printing 2D-Array"<<endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
cout<<"the row-wise sum of the 2D-Array is:"<<endl;
rowSum(arr,3,3);
int ans=largestRowSum(arr, 3,3);
cout<<ans;
return 0;
}