Reeder Rivers
Reeder Rivers

Reputation: 11

Implementing Matrix in C but another version

I'm new to C programming and I am practicing on making a matrix but it is kind of different. I must only enter 1 number (e.g) 5. then the result will be

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

The colums and rows are lined 5 times. Please send help.

#include<stdio.h>
main(){

int mat[10][10],i,j;
int num;
int nrows, ncols;

scanf("%d",&num);

for(i=1;i<=num;i++){
    printf("%d",i);
    for(i=1;i<=num;i++){
        printf("%d",i);
    }
}


}

Upvotes: 1

Views: 60

Answers (1)

meowgoesthedog
meowgoesthedog

Reputation: 15035

Your current code is along the right lines, but:

  • The inner loop is overwriting the index of the outer loop. It should use a separate index variable e.g. the j you declared at the top
  • Instead of writing the row number at the start of each row, print a newline character \n at the end

Amended:

for(i=1;i<=num;i++){
    for(j=1;j<=num;j++){
        printf("%d",j);
    }
    printf("\n");
}

Upvotes: 1

Related Questions