Computer Science & C Programming


C Programming

The following code illustrates how to define/use subprograms and the two fundamentals instruction for "loops" (for) and "branching" (if).

#define DIM 2 // Constant
char cells[DIM*DIM];

// Subprograms definitions
void init() {
  for(int pos=0; pos<DIM*DIM; pos=pos+1) {
    cells[pos] = ' ';
  }
}

void print() {
  for(int pos=0; pos<DIM*DIM; pos++) 
    printf("%c",cells[pos]);
  printf("\n");
}

void play(char c,int p) {
  if(cells[p]==' ') cells[p] = c;
}

void main() {
  init();
  print();
  play('X',1);
  print();
}