How do I get the spaces just from the last column of the matrix?

Asked

Viewed 27 times

0

Iai galera, so my code is already working perfectly, but in the last column it also gives a space, but I do not want this space there, as I do to remove it? Leaving only spaces between numbers.

#include <stdio.h>

int linhas=0, colunas=0, soma1=0, soma2=0, contador1=0, contador2=0;




int calcDiagonalPrinc(){

  int matriz[linhas][colunas];




  return soma1;
}

int calcDiagonalSec(){

  int matriz[linhas][colunas];


 return soma2;
}



int main() {

  scanf("%d", &linhas);
  scanf("%d", &colunas);

  int matriz[linhas][colunas];

  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      scanf("%d", &matriz[i][j]);
    }
  }
  printf("\nMatriz formada:\n");
  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      printf("%d ", matriz[i][j]);
      
    }
    printf("\n");
    }

if(linhas==colunas){
  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      if(i==j){
        soma1=soma1+matriz[i][j];}
      if(matriz[i][j]<0){
        contador1++;
      }else
        if(matriz[i][j]>0){
        contador2++;
      }
        
    }
  }

  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      if((i+j)==linhas-1){
        soma2=soma2+matriz[i][j];

        
      }
    }  
  }
  printf("A diagonal principal e secundaria tem valor(es) %d e %d respectivamente.\n", soma1, soma2);
  printf("A matriz possui %d numero(s) menor(es) que zero.\n", contador1);
  printf("A matriz possui %d numero(s) maior(es) que zero.\n", contador2);

}else
  printf("A diagonal principal e secundaria nao pode ser obtida.\n");



  return 0;
}
  • Inacio. Uses "%d t " on the printf which will print the formatted matrix element. It uses Tab, it is a simple but easy to format option. He gives a tab instead of space. If you use space you will have to worry about other aspects of formatting such as spacing size and numerical elements to be able to tabulate correctly.

  • https://onlinegdb.com/Tptk8ZVOt << This link has your code. I only modified the printfs to make it more understandable what you are doing. Even if I am yours, make it readable in operations, if it is not difficult to know what is generating input.

  • Even if I use tab, it leaves a tab tbm in the last column and I don’t want it, I wish there was nothing after the last numbers in the column

1 answer

0

Assuming you’re talking about this passage:

  printf("\nMatriz formada:\n");
  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      printf("%d ", matriz[i][j]);
    }
    printf("\n");
  }

you can do:

  printf("\nMatriz formada:\n");
  for(int i=0; i<linhas; i++){
    for(int j=0; j<colunas; j++){
      printf("%d%s", matriz[i][j], (j<colunas-1) ? " " : "");
    }
    printf("\n");
  }

Note that you could do:

printf("%d%s", matriz[i][j], (j<colunas-1) ? " " : "\n");

and eliminate the printf("\n");.

Browser other questions tagged

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