Matrix program error in C

Asked

Viewed 72 times

0

I am starting now in C programming and I have a problem in my C program. The problem is that the printout matrix appears all zeroed.

#include <stdio.h>
#include <stdlib.h>

    int main (){
        float m[4][4];
        int i, j;

        for(i=0;i<4;i++) {
            for(j=0;j<4;j++){
                m[i][j]=0;
         }
        }
        for(i=0;i<4;i++){
            for(j=0;j<4;j++){
                printf("%d", m[i][j]);
         }
                printf("\n");
        }
        return 0;
}
  • Good the answer below, adding knowledge, check this link: https://www.clubedohardware.com.br/forums/topic/1166436-filling-matriz-usando-for-e-dowhile/ for more examples

  • Wow so much language taggada. Did you forget to c-- and d :-)

1 answer

1


Hello. Your matrix is showing zeroed because you are zeroing the elements here: m[i][j]=0; . Put non-zero values on that line and it’ll all be over. In the example below, I entered values in the matrix equal to the product of the indices (only for demonstration).

int main (){
float m[4][4];
int i, j;

for(i=0;i<4;i++) {
    for(j=0;j<4;j++){
        m[i][j]=i*j;
 }
}
for(i=0;i<4;i++){
    for(j=0;j<4;j++){
        printf("%d", m[i][j]);
 }
        printf("\n");
}
return 0;
}
  • thank you so much!

  • I’m happy to help

  • @use_-44-18 marks the answer as right to show that your question has been answered =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.