CISC105 Summer 2007 Lab07
Notes before you begin:
- You will write one program for each of the following problems. You may start each program using a previous program as a base if it seems suitable.
- Be sure to save each program as its own .c file. Name them lab07.n.c, where "n" is the number in the program list below. For example, the name of the file for the first program will be lab07.1.c
- Style counts. This includes header comments, comments on any tricky lines of code, good variable naming, good spacing and indentation. These are worth points to you.
- When scripting your lab, you will compile and run each one. For each .c file, you will first cat the file, then compile it, then run a.out and fully demonstrate the capability of the program. If your program does not compile, do not attempt to run it. Since it doesn't compile, no new a.out file is created--thus if you try to run it, you'll just be running the previous file which did compile.
- Please Remember that the lab is due electronically by Sunday at 11:55p.m. A 10% penalty is assessed for each day that the lab is late, and this penalty depends upon the electronic submission time (from myCourses).
Programs
- Write a program that uses a 10x10 two-dimensional array of integers to store the multiplication tables. Each element of the 2d-array should be equal to the product of its indices. For example:
Index | 0 | 1 | 2 | 3 | 4 |
0 | 0 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 2 | 3 | 4 |
2 | 0 | 2 | 4 | 6 | 8 |
3 | 0 | 3 | 6 | 9 | 12 |
4 | 0 | 4 | 8 | 12 | 16 |
And so on. Fill this array using nested for loops. Then, once it is full, use another set of nested for loops to print the table to the screen. Make sure it is nicely formatted and easy to read. You do not need to include the row/column headers (in bold here). Also, you may leave out the "0" row and column if you wish, though it is not required.
- This problem uses tictactoe.c, found in the labs/lab07 directory on the website. Save this file to your home directory, and then make a copy of it as your lab07.2.c. Get familiar with the program; it is very similar to what we worked on in class. Note: the system("clear"); call has been removed because it adds strange characters to the script. Do not add it back, at least not in the final version.
Add a function to this program that tests whether the board is full. The function should return an integer, 1 if the board is full and 0 otherwise. Change the loop in main such that it calls this new function and quits if the board is full. You may remove the tests for -1 in the loop--we will assume that the game continues until the board is full. This means that you may also want to rearrange things in the loop somewhat (moving the printf/scanf to the beginning of the loop, perhaps).
- Make a copy of your lab07.2.c for this problem. Add another function to your tic-tac-toe program, which tests whether there is a winner. This function should return a character: 'X' if X is the winner, 'O' if O is the winner, or '-' if there is no winner on the board. Once again, change your loop in main. It should now immediately end if there is a winner. Lastly, in main, the program should print a message declaring who has won: X, O or the Cat.
- The following problems deal with structs. Write a program that creates a new data type using structs, called Student. This data type should consist of:
integer id
double gpa
20 character array, firstName
20 character array, lastName
In main, create a variable of type Student. Next, fill the variable with values of your choosing. Remember that you need to use string functions to give values to the character arrays inside of student. You don't need to get user input here, just hard-code the statements into main.
This program should compile, but not print anything if it is run.
- Make a copy of lab07.4.c for this problem. Add to it a void function that takes a single Student as a parameter, and nicely prints the values inside it. Call this function from main, passing in the Student you created for the previous problem.
- Once again, make a copy of lab07.5.c for this problem. Add another function, this one called getNewStudent(). The function should have no parameters, and should have a return type of Student. It should declare a new student variable inside the function, and then fill it with values from user input. Once the new Student is filled, return the variable.
In main, call getNewStudent and assign the returned value to the student variable you created in lab07.4.c. This should assign the user-specified values to this variable. Test this by printing the contents of the variable with your student-printing function from the last problem.
Submission
You should have a total of 6 programs, named lab07.1.c to lab07.6.c. Make a single script file (named lab07.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab07.1.c, then do the same with lab07.2.c, etc.
Submit all program and script files on MyCourses before 11:55 Sunday night. Give the paper version of your script to your TA next Monday at the beginning of lab. Write your name and the lab number on the front of your paper submission. Also, if something doesn't work properly, please circle it in bright ink and make a note for your TA explaining the problem. You will lose less points for a problem you recognize and can explain (at least a little) than for one you don't seem to notice.