#define IF_WRONG_CORRECT_IT "Is there something wrong with this code? How would you correct it?"
MEDIUM: As many of you know, TicTacToe is a popular game among students (Google TicTacToe if you dont know what we are talking). Students actively play the game -- especially during the lectures -- by marking ’x’ and ’o’ in a 3x3 square tile. Ofcourse, you know the rules of the game. A player might win, lose or the game ends up in a draw. We thought we will make you play the game in this course. That way you will think you are having more fun, while we make you learn some ’C’ programming.
Download tictactoe.c. The boiler-plate code represents a tic-tac-toe board by a 3x3 matrix. It defines ’x’ by CROSS and ’o’ by CIRCLE, and a position not taken by NOT_USED. You have to input the state of a tic-tac-toe board from user, and check of the player with ’x’ won or the player with ’o’ won, or if the game is a draw.
You will use pointers to parse rows and columns (instead of usual arrays). This will give you a feel for using pointers in multi-dimensional arrays.
EASY: Exercise 1: How does the memory allocation change if str is declared as below:
char str[] = "CS220";IF_WRONG_CORRECT_IT
EASY: Exercise 2: What happens if it is declared as below? IF_WRONG_CORRECT_IT
char str[] = {'C', 'S', '2', '2', '0'};
EASY: Exercise 3: Let us say, we added the code
sPtr = str; sPtr = sPtr + 2What does sPtr point to? What happens when we
int ret = printf("sPtr = %s\n", sPtr);IF_WRONG_CORRECT_IT What is the value of ret? Why?
EASY: Exercise 4: What happens if you declare sPtr as
char *sPtr = "CS220"IF_WRONG_CORRECT_IT
Download the strfn.c file. This defines a template code that calls different string library functions (Google "library functions in C"). Using them solve the below questions:
EASY: Exercise 1: What happens if we print str2 before assigning (second printf in the code)? IF_WRONG_CORRECT_IT
EASY: Exercise 2: What happens if we change strcpy to (man strcpy):
str2 = strcpy(str2, str1);IF_WRONG_CORRECT_IT
EASY: Exercise 3: What happens if:
sPtr = strcpy(str2, str1);IF_WRONG_CORRECT_IT
EASY: Exercise 4: What happens if:
sPtr = str1; strcpy(str2,sPtr);IF_WRONG_CORRECT_IT
EASY: Exercise 5: During strcmp, what happens if we enter a long string (greater than 10 chars)?
MEDIUM: Exercise 6: What kind of error checking do the string functions (strcmp, strlen, strcpy) do?
MEDIUM: Exercise 7: Write your own versions for strlen, strcpy and strcmp. (K&R page 99)
HARD: Exercise 8: Memory management (K&R page 100-102): NOTE: Interesting exercise. Highly recommended. Write your own memory management module that has two functions alloc(...) and afree(...) that allocates memory and frees memory. Users can call alloc(...) function, for example, to allocate memory for a string. When the users do not need the memory, they will call afree(...). Take a look at the K&R book for implementation hints. This will be your starting point for many important memory management routines that you will later use.
Download the str2D.c file. This defines a template code that plays with an array of strings. Using them solve the below questions:
EASY: Exercise 1: What is sizeof(matrix)? Draw a pictorial diagram of how the memory is allocated?
EASY: Exercise 2: Find out the number of command line arguments. Print the length of each string in argv[]. Draw a pictoral diagram to show the memory allocation.
MEDIUM: Exercise 3: Assume matrix is declared as:
char matrix[ROWS][COLS];
EASY: Exercise 4: Let
char* ptrToStr;Point ptrToStr to matrix[0]. Print ptrToStr. What happens if we do ptrToStr++? Print ptrToStr now.
HARD: Exercise 5: Let
char** ptrTo2DStr;Point ptrTo2DStr to matrix (cast it explicitly). What happens if we do ptrTo2DStr++? Print ptrTo2DStr. IF_WRONG_CORRECT_IT
Download the fileIODemo1.c file. This defines a template code for file input and output. The code reads a list of english words from a dictionary file (say, english.0 file) and prints the words. Using the code, solve the below questions:
EASY: Exercise 1: What happens if the file is opened in "w" "w+" "r+" "a" "a+" modes? Caution: Backup the dictionary file before trying.
EASY: Exercise 2: Can you write the loadDictionary program without passing the wordCount?
MEDIUM: Exercise 3: Write your own version of grep command. Hint: Take a look at thefind program (K&R, page 117). Extend this program is your own version of the grep command.
MEDIUM: Exercise 4-5: Exercises 5-10 and 5-13 in K&R (page 118)