Computer Science I

MC140.01

Spring 2001

FAQ

last updated: Tue Jan 16 18:07:21 EST 2001
back to MC140.01 home page

Hints for assignment #9

(1) Reading a line of input with spaces in it.

In C, any white space will terminate a string when read using the scanf() function with the "%s" format specifier. White space includes blank spaces, carriage returns and tabs.

But you may want to input a string that has spaces in it. So you need a special function to do this.

This program contains a function called readInput() that you can paste into your homework. This function reads characters from the keyboard and stores them (including spaces) into a single string. When a newline character is read (i.e, you press the <Enter> key), it stops reading input and returns the whole string (including spaces, but not including the newline character).

(2) Reading a single character.

You might want to read in one character, for example, you might ask the user:

Enter another record (y/n)?

and want to read either y or n from the keyboard.

You might have lines like this in your code to do this:

char more;
...
printf( "Enter another record (y/n)? " );
scanf( "%c",&more );

The problem is that in C, when you type y (or any one character) followed by the enter key, actually 3 characters go into the input buffer: y, carriage return and linefeed. So you need to read in a string rather than a single character, in order to flush the carriage return and linefeed from the input buffer (otherwise subsequent input will be confused because you have these extra characters in your input buffer). Here's how to change the code:

char more[2];
...
printf( "Enter another record (y/n)? " );
scanf( "%s",more );
Then you compare more[0] to 'y' and 'n'. (Notice the single quotes because you are comparing to a single character, not a string.)


Hints for assignment #7

(1) Passing two-dimensional arrays as function arguments.
When you pass one-dimensional arrays as function arguments, you don't need to specify the size of the array, either in the function prototype or in the function header itself. Here's what we've done in class:

#include <stdio.h>

void printDice( int dice[] );

int main( void ) {
  int dice[5] = { 6, 3, 4, 5, 2 };
  printDice( dice );
  return( 0 );
} /* end of main() */

void printDice( int dice[] ) {
  int i;
  for ( i=0; i<5; i++ ) {
    printf( "%d ",dice[i] );
  } /* end for i */
  printf( "\n" );
} /* end of printDice() */
However, when passing two-dimensional arrays as function arguments, you must specify the dimensions both in the function prototype and in the function header. Here's how the tic-tac-toe program might look using a board[][] variable that is local to main() instead of being declared globally as you did for assignments #5 and #6.
#include <stdio.h>

void printBoard( int board[3][3] );

int main( void ) {
  int board[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  printBoard( board );
  return( 0 );
} /* end of main() */

void printBoard( int board[3][3] ) {
  int i, j;
  for ( i=0; i<3; i++ ) {
    for ( j=0; j<3; j++ ) {
      printf( "%d ",board[i][j] );
    } /* end for j */
    printf( "\n" );
  } /* end for i */
} /* end of printBoard() */

(2) Labeling rows and columns when printing a grid.
Suppose you wanted to print out your tic-tac-toe board, labelling its rows and columns as follows:

   | a | b | c |
---+---+---+---+
 d | 1 | 2 | 3 |
---+---+---+---+
 e | 4 | 5 | 6 |
---+---+---+---+
 f | 7 | 8 | 9 |
---+---+---+---+
The numbers (1..9) are the contents of the board[][] variable. The letters (a..c,d..f) are column and row labels, respectively. All the other characters -- spaces, vertical bars (|), dashes (-) and plusses (+) -- are characters that you print to make the board look nifty, the way it does in the picture above.
So in order to print the first line, you could simply print a line of text like this:
printf( " | a | b | c |\n" );
And to print the second line, you could print a line of text like this:
printf( "---+---+---+---|\n" );
To print the third line, you'll need to intersperse the "nifty" characters with the board elements, like this:
printf( " d | %d | %d | %d |\n",board[0][0],board[0][1],board[0][2]);
The fourth line is just a repeat of the second line.
You can continue in this way to print out the whole board, nifty style.

If you do this, you'll notice some patterns and perhaps be able to figure out how to write the code more concisely -- using nested for loops, as in the printBoard() from part 1, above.

You might even take advantage of the ASCII table. Suppose instead of printing
printf( " | a | b | c |\n" );
you had a character variable called column_label which you initialized to 'a' and then incremented it after you'd printed the letter 'a':

char column_label = 'a';
int j;
printf( " |" );
for ( j = 0; j<3; j++ ) {
  printf( " %c |",column_label );
 column_label++;
 } /* end for j */

You could do the same thing with a row_label variable.

(3) Please hand in assignment #7 on MONDAY APRIL 2. This is an extension from the original Friday 30 March deadline!