What is happening in my C program?

5

I just have to add the lines but the values do not make sense:

Hereisthecode:

#include<stdio.h>#include<stdlib.h>intmain(){intmatriz[6][6],i,h,somalinha[6];for(i=0;i<=4;i++){for(h=0;h<=4;h++){printf("Digite os valores de uma matriz 3x3:\n");
            scanf("%d",&matriz[i][h]);
            somalinha[i] = somalinha[i]+matriz[i][h];
        }
        printf("O resultado da soma da linha %d eh %d\n",i,somalinha[i]);
    }

    return 0;
}
    
asked by anonymous 12.12.2016 / 04:56

2 answers

7

The code has some problems. I gave an organized one and solved the problems. The include does not make sense, the array is not zero in the statement, so the sum does not occur correctly and the statement is being greater than it should, suffice, if it is 3X3 declare only this, a modernized one:

#include <stdio.h>

int main() {
    int matriz[3][3], somalinha[3] = { 0 };
    for (int i = 0; i < 3; i++) {
        for (int h = 0; h < 3; h++) {
            printf("Digite os valores de uma matriz 3x3:\n");
            scanf("%d", &matriz[i][h]);
            somalinha[i] += matriz[i][h];
        }
        printf("O resultado da soma da linha %d eh %d\n", i, somalinha[i]);
    }
}

See working on ideone and on CodingGround .

    
12.12.2016 / 05:19
5

You forgot to initialize the vector somalinha with values equal to zero. The program is collecting memory garbage.

Another detail is that you declare a 6x6 array, but it uses a 5x5 array in the loop repetition.

    
12.12.2016 / 05:04