2D array - I want to find which collums have 1s in them in each row

My input looks like this :

``````15 5
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 1 0 1 0
0 0 1 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
1 0 0 0 1
0 0 1 0 0
0 0 0 1 0
``````

The first row contains the number of rows and collums of my array. And basically i want to find out where these 1s are in the array. So in the first 3 rows i want to get `3`, in the 7th `1`, and in the 8th, i want to get `2 3` etc..

My code looks like this so far

``````#include <stdio.h>

int main() {

int row, collumn;

FILE* input;
FILE* output;

input = fopen("input.txt", "r");

if (input == 0) {
printf("ERROR couldn't open input.txt");
return 1;
}

if (! fscanf(input, "%d %d", &row, &collumn)) {
printf("ERROR not recognised value");
fclose(input);
return 2;
}

output = fopen("output.txt", "w");

int meteor[row][collumn];

for (int i = 0; i < row; i++) {
for (int j = 0; j < collumn; j++) {
fscanf(input, "%d", &meteor[i][j]);
}
}
int sum;
int loc[row];
for (int i = 0; i < row; i++) {
sum = 0;
for (int j = 0; j < collumn; j++) {
sum += meteor[i][j];
if (meteor[i][j] == 1) {
loc[i] = (j + 1);
}
}
printf("%d %d\n", loc[i], sum);
}

fclose(input);
fclose(output);

return 0;
}
``````

My output is this :

``````3 1
3 1
3 1
0 0
-1 0
1 1
4 2
3 1
5 1
0 0
4214921 0
2 1
5 2
3 1
4 1
``````

The first collumn shows some of the locations, the second shows how many 1s are in the row, but it all fails when there are only `0s` in the row or there is more than one `1`. And also i would like to store these values.

You need to initialize `loc[]`.. If you look carefully at your code, you only populate it on the condition of `if (meteor[i][j] == 1)`... but you print it for every index of i. which would print uninitialized memory (i.e. unknown).
To answer the second part of your question, if you want to store the "sum". Simply make `loc[]` a 2d array just like `meteor`, but with 2 columns (row and sum). i.e. `int loc[row][2]`.. making sure to initialize both columns of course :)