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










1















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 %dn", 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.










share|improve this question

















  • 1





    if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

    – Kamil Cuk
    Nov 12 '18 at 21:24







  • 1





    You never initialize loc.

    – Johnny Mopp
    Nov 12 '18 at 21:24















1















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 %dn", 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.










share|improve this question

















  • 1





    if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

    – Kamil Cuk
    Nov 12 '18 at 21:24







  • 1





    You never initialize loc.

    – Johnny Mopp
    Nov 12 '18 at 21:24













1












1








1








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 %dn", 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.










share|improve this question














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 %dn", 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.







c arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 21:17









Stan MarshStan Marsh

216




216







  • 1





    if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

    – Kamil Cuk
    Nov 12 '18 at 21:24







  • 1





    You never initialize loc.

    – Johnny Mopp
    Nov 12 '18 at 21:24












  • 1





    if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

    – Kamil Cuk
    Nov 12 '18 at 21:24







  • 1





    You never initialize loc.

    – Johnny Mopp
    Nov 12 '18 at 21:24







1




1





if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

– Kamil Cuk
Nov 12 '18 at 21:24






if (! fscanf("%d %d .. )" is invalid, this fscanf returns 2 on success and not 2 (EOF, 0 or 1) on failure.

– Kamil Cuk
Nov 12 '18 at 21:24





1




1





You never initialize loc.

– Johnny Mopp
Nov 12 '18 at 21:24





You never initialize loc.

– Johnny Mopp
Nov 12 '18 at 21:24












1 Answer
1






active

oldest

votes


















0














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 :)






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270249%2f2d-array-i-want-to-find-which-collums-have-1s-in-them-in-each-row%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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 :)






    share|improve this answer





























      0














      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 :)






      share|improve this answer



























        0












        0








        0







        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 :)






        share|improve this answer















        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 :)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 21:36

























        answered Nov 12 '18 at 21:29









        static_caststatic_cast

        82211018




        82211018



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270249%2f2d-array-i-want-to-find-which-collums-have-1s-in-them-in-each-row%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo