在C中修改2d数组

时间:2014-04-29 11:49:15

标签: arrays 2d

我必须使用二维阵列编写一个简单的影院预订程序。所有我能做到的100%是打印数组这里是代码。

#include <stdio.h>

void main()
{
    printf("Cinema seat outlet :\n\n");

    int i, j, a[10][10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    for(i = 0; i <= 9; i++)
    {
        for(j = 0; j <= 9; j++)
            printf("%d", a[i][j]);

        printf("\n");
    }

    printf("\n\nMake a booking for what seat U desire :\n\n");
}

现在我只想在特定位置预订座位并将其更改为1.表示座位已预订。 任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

您提出的基本程序,您可以进一步修改以供您使用

#include <stdio.h>
#include <string.h>

void main()

{
  printf("Cinema seat outlet :\n\n");
  int i, j, a[10][10],row,column;
  memset(a,0x00,sizeof(a));   /* all the array is initialized to zero*/
  printf("below shows the seating arrangement \n\n");

  printf(" \t");              /* compensate offset of row number*/

  for(i = 1; i <= 10; i++)    /* Prints the column number (+1 since i starts from 0)*/
  printf(" %d ",i);

  printf("\n");
  for(i = 0; i <= 9; i++)
  {
    printf("%d\t",(i+1));        /* Prints the row number (+1 since i starts from 0)*/   
        for(j = 0; j <= 9; j++)
              printf(" %d ",  a[i][j]);
        printf("\n");
  }
  printf("\n\nEnter the row number of the set you desire:\n\n");
  scanf("%d",&row);
  printf("\n\nEnter the column number of the set you desire:\n\n");
  scanf("%d",&column);
  a[row-1][column-1] = 1;       /* set the seat booked (-1 cz; user express data in +1     format)*/
  printf("The seats of the theatre\n\n");

  printf(" \t");              /* compensate offset of row number*/     
  for(i = 1; i <= 10; i++)    /* Prints the column number (+1 since i starts from 0)*/
  printf(" %d ",i);

  printf("\n");
  for(i = 0; i <= 9; i++)
  {
    printf("%d\t",(i+1));        /* Prints the row number (+1 since i starts from 0)*/   
        for(j = 0; j <= 9; j++)
              printf(" %d ",  a[i][j]);
        printf("\n");
  }
}

答案 1 :(得分:0)

您始终可以将数组索引作为行和列开始,如a[row][column]。 现在,您可以设置由rowcolumn指定的特定座位,或检查是否已采取此特定座位。