1+ #include < iostream>
2+ using namespace std ;
3+
4+ /* basic funtion to search a element in 2d array */
5+ bool search (int arr[][3 ], int target, int row, int col) {
6+ for (int i = 0 ; i < row; i++) {
7+ for (int j = 0 ; j < col; j++) {
8+ if (arr[i][j] == target) {
9+ return true ;
10+ }
11+ }
12+ }
13+ return false ;
14+ }
15+
16+ void rowSum (int arr[][3 ], int row, int col) {
17+ int sum = 0 ;
18+ for (int i = 0 ; i < row; i++) {
19+ int sum = 0 ;
20+ for (int j = 0 ; j < col; j++) {
21+ sum += arr[i][j];
22+ }
23+ cout << " Sum: " << sum << endl;
24+ }
25+ }
26+
27+ void largestRowSum (int arr[][3 ], int row, int col) {
28+ int sum = 0 ;
29+ int max = 0 ;
30+ int rowIndex = 0 ;
31+ for (int i = 0 ; i < row; i++) {
32+ int sum = 0 ;
33+ for (int j = 0 ; j < col; j++) {
34+ sum += arr[i][j];
35+ }
36+ if (sum > max) {
37+ max = sum;
38+ rowIndex = i;
39+ }
40+ }
41+ cout << " Max sum " << max << " at index: " << rowIndex;
42+ }
43+
44+ int main () {
45+ /* create a 2d array */
46+ // int arr[3][3];
47+ // int arr[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
48+ int arr[3 ][3 ] = {{1 , 11 , 111 }, {2 , 22 , 222 }, {3 , 33 , 333 }};
49+
50+ /* input 2d array */
51+ /* for (int i = 0; i < 3; i++) {
52+ for (int j = 0; j < 3; j++) {
53+ cout << "Enter element at (" << i << ", " << j << "): ";
54+ cin >> arr[i][j];
55+ }
56+ } */
57+
58+ /* output 2d array */
59+ cout << " 2D Array:- \n " ;
60+ for (int i = 0 ; i < 3 ; i++) {
61+ for (int j = 0 ; j < 3 ; j++) {
62+ cout << arr[i][j] << " \t " ;
63+ }
64+ cout << endl;
65+ }
66+
67+ /* searching */
68+ if (search (arr, 111 , 3 , 3 )) {
69+ cout << " Found" << endl;
70+ } else {
71+ cout << " Not found" << endl;
72+ }
73+
74+ /* sum of rows */
75+ cout << " Sum of rows: " << endl;
76+ rowSum (arr, 3 , 3 );
77+
78+ /* row with max sum */
79+ cout << " Finding row with max sum: \n " ;
80+ largestRowSum (arr, 3 , 3 );
81+ return 0 ;
82+ }
0 commit comments