I need to write a code, with instruction:
Write a program that finds the row with the minimum amount in the matrix. If there are several such lines, find the first such line.
My code:
#include <iostream>
using namespace std;
int main() {
int n, m, h;
int arr[100][100];
cin >> n >> m;
const int q=m;
int ar[q];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i<n; i++){
int sum = 0;
for (int j = 0; j<m; j++){
sum += arr[i][j];
}
for (int y=0; y<q; y++){
ar[y] = sum;
}
}
int sammin = ar[0];
for (int i = 0; i<q; i++){
if (ar[i] < sammin){
h = i;
}
}
for (int j = 0; j<m;j++){
cout << arr[h][j] << " ";
}
return 0;
}
With input data:
4 5
1 3 2 54 234
75 12 3 46 9
13 26 56 9 12
14 90 897 6 34
The code writes out the first line when it should output a line with 2 index