Problem in C MATRIX display

Asked

Viewed 56 times

0

Hello, I would like help to try to solve the problem of an matrix of my program in C. It was created the matrix saying that it should be filled with such a character, it even works, but the last line is always not displayed the character that was defined, and no matter how many rows and how many columns I place, the same thing always happens. I’ve read and reread the entire code, but I can’t find anything unusual. I thought it was overflow but for the little knowledge I have overflow does not occur in CHAR type variable. I’m sorry if I’m wrong

Follow the blocks of code:

STATEMENTS AND LIBRARIES:

#include <stdio.h>
#include<stdlib.h>
#include<conio.h>
#define COL 7
#define ROW 4
#define FALSE 0
#define TRUE 1

void inicializa(char matriz[ROW][COL]);
void mostrarPainel(char matriz[ROW][COL]);
void comprar(char matriz[ROW][COL]);
void reservar(char matriz[ROW][COL]);
void legenda();
int menuOpcao();
int colunaY();
int linhaX();

MENU SWITCH:

inicializa(matriz); //Iniciliza a matriz com valores .(Livre)

  do{
     system("cls");//Limpa a tela                
     opcao=menuOpcao();                   
     switch(opcao){
        case 1:
          system("cls");
          system("cls"); 
          mostrarPainel(matriz);//Mostra o Painel atualizado
          comprar(matriz); // Realiza a compra de uma cadeira no painel, marcando c/ um X
          break;

        case 2:
          system("cls");
          system("cls");
          mostrarPainel(matriz);
          reservar(matriz); //Realiza a reserva de uma cadeira no painel, marcando c/ um R
          break;

        case 3:
          system("cls");
          mostrarPainel(matriz);
          getch();
          break;   

        case 4:
          loopContinue = FALSE; //condicao para saída do programa
     }
  }while(loopContinue);

  getchar();    
  return 0;       
}
-----------------------------------------------------------------------------
void inicializa(char matriz[ROW][COL]){
     int i,j;
     for(i=1;i<=ROW;i++)
       for(j=1;j<=COL;j++)
          matriz[i][j] = '.';
}

void mostrarPainel(char matriz[ROW][COL]){

  int i,j;
  printf("\n\n         %c   PAINEL DE OCUPACOES   %c\n\n",16,17);
  printf("          ");

  for(i=1;i<=COL;i++)
    printf("%d     ",i);

  for(i=1;i<=ROW;i++){
     printf("\n\n     %d",i);
     for(j=1;j<=COL;j++)
       printf("   (%c)",matriz[i][j]);     
  }
  //apresenta o menu na tela
  legenda();
}        
------------------------------------------------------------------

1 answer

0

In C (and in the vast majority of languages) the arrays (vectors are one-dimensional arrays and matrices are multidimensional arrays as you should know) have indices ranging from 0 (zero) to n-1 (where n is the size of the array) i.e., its matrix has indices in similar ways, soon when you go to do your go to go through the matrix you would have to go from 0 to COLS/ROWS -1, you can just do so:

void mostrarPainel(char matriz[ROW][COL]){

int i,j;
printf("\n\n %c PAINEL DE OCUPACOES %c\n\n",16,17);
printf(" ");

for(i=0;i<COL;i++){
    printf("%d ",i);
}

for(i=0;i<ROW;i++) {
    printf("\n\n %d",i); 
    for(j=1;j<=COL;j++) {
         printf(" (%c)",matriz[i][j]);
    }
} //apresenta o menu na tela legenda();
}

Browser other questions tagged

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