Home > Mobile >  For C: Why does my Matrix output have the incorrect formatting? (Beginner)
For C: Why does my Matrix output have the incorrect formatting? (Beginner)

Time:04-30

My inputs repeat themselves rather then starting from blank and letting me input the next integer. An additional issue is that I am unable to print the matrix like "100 200" and then "300 400" on the next line. I have tried messing with the newlines and taking out the scan function but nothing seems to work.

#include<stdio.h>

int rows;
int colums;
int i;
int j;

int main() {
    int mat[100][100];
    printf("Please enter the number of rows: ");
    scanf("%d",&rows);
    printf("Please enter the number of columns: ");
    scanf("%d",&colums);
    printf("Enter Matrix A\n");
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j) {
            scanf("%d", &mat[i][j]);
            printf("%d",mat[i][j]);
        }
    }
    printf("Enter Matrix B\n");
    int matb[100][100];
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j) {
            scanf("%d", &matb[i][j]);
            printf("%d",matb[i][j]);
        
        }
    }
    printf("A B =\n");
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j){
            int new;
            new = mat[i][j] matb[i][j];
            printf("%d",new);
        }
    }
}

CodePudding user response:

When you're reprinting the numbers after the user enters them, end with a newline so they don't enter the next number on the same line.

When you're printing the sums, put a space after each number, and a newline after each row.

I've annotated the changes below with comments.

#include<stdio.h>

int rows;
int colums;
int i;
int j;

int main() {
    int mat[100][100];
    printf("Please enter the number of rows: ");
    scanf("%d",&rows);
    printf("Please enter the number of columns: ");
    scanf("%d",&colums);
    printf("Enter Matrix A\n");
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j) {
            scanf("%d", &mat[i][j]);
            printf("%d\n",mat[i][j]); // added newline
        }
    }
    printf("Enter Matrix B\n");
    int matb[100][100];
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j) {
            scanf("%d", &matb[i][j]);
            printf("%d\n",matb[i][j]); // added newline
        
        }
    }
    printf("A B =\n");
    for(int i = 0; i < rows;   i){
        for(int j = 0; j < colums;   j){
            int new;
            new = mat[i][j] matb[i][j];
            printf("%d ",new); // added space
        }
        printf("\n"); // added newline
    }
}

CodePudding user response:

I was looking through your code and saw the following conceptual errors: You do declare i,j at the main beginning but also declared locals for each for loop, that’s not necessarily you can choose one way or another, You cant obtain a printed results by rows because you are printing each value since you have the print statement nested inside both loops, for row printing you have to take out the print of the second loop. Also you have to print newline like was pointed in the other answer.

#include<stdio.h>

int rows;
int colums;
int i;
int j;

int main() {
    int mat[100][100];
    printf("Please enter the number of rows: ");
    scanf("%d",&rows);
    printf("Please enter the number of columns: ");
    scanf("%d",&colums);
    printf("Enter Matrix A\n");
    for(i = 0; i < rows;   i){
        for(j = 0; j < colums;   j) {
            scanf("%d", &mat[i][j]);
        }
        printf("\n");
    }
    printf("Enter Matrix B\n");
    int matb[100][100];
    for(i = 0; i < rows;   i){
        for(j = 0; j < colums;   j) {
            scanf("%d", &matb[i][j]);
        }
        printf("\n");
    }
    printf("A B =\n");
    int new;
    for(i = 0; i < rows;   i){
        for(j = 0; j < colums;   j){
            new = mat[i][j] matb[i][j];
            printf("%d ", new);
        }
        printf("\n");
    }
}

That's it Output:

> cc test.c -o test.exe && test
Please enter the number of rows: 2
Please enter the number of columns: 2
Enter Matrix A
10 20

30 40

Enter Matrix B
1 2

3 4

A B =
11 22
33 44

  •  Tags:  
  • c
  • Related