C代写:CS104 Matrix

Introduction

很基础的C程序,实现线性代数中的矩阵运算。然后再实现一个简单的Tic-Tac-Toe.

Requirement 1

Repeat the mat_add problem from homework 3. Write a program called mat_add.c that asks the user for 2 matrices A, and B, and displays their sum, C.

  1. Name your executable mat_add.out
  2. All numbers entered will be integers
  3. All matrices will be valid
  4. Changes
    There is now no max size for each matrix.
    You must dynamically allocate space for all arrays used.
    This means you must use malloc to create your arrays.
    int A[rows][cols] will not be accepted
  5. Each row of the matrix will be entered 1 line at a time
  6. The formula for calculating C[i][j] is C[i][j]=A[i][j]+B[i][j]
  7. Examples:
1
2
3
4
5
6
7
8
9
10
11
Please enter the number of rows: 2
Please enter the number of columns: 2
Enter Matrix A
12
34
Enter Matrix B
100200
200400
A + B =
101202
203404
1
2
3
4
5
6
7
8
9
10
11
Please enter the number of rows: 2
Please enter the number of columns: 3
Enter Matrix A
1020-30
127
Enter Matrix B
1230
-345
A + B =
11220
-2612

Requirement 2

Write a program to implement the game connect-n. Connect-n is like Connect-4 except instead of having the board be a constant 6 X 7 we will allow the user to enter the size of the board they would like to play on. In addition we will also allow the user to choose how many pieces in a row are necessary to win. The game is played as follows. Two players take turns dropping their pieces into a column until either player gets N of their pieces in a row either horizontally, vertically, or diagonally, or until their or no more spaces to play.

  1. Your program must accept 3 command line parameters in this order: number of rows, number of columns, number of pieces in a row to win
    If the user does not enter enough arguments or enters too many arguments your program should tell them the proper usage of your program and terminate.
    You may find the exit function helpful.
    The user should be allowed to create an unwinable game.
    For example a board that is 3 X 3 but requires 4 pieces in a row to win.
  2. Your program should not allow the user to make an impossible play but should continue to ask the user for a play until a valid play is entered
    Invalid plays consist of plays made outside the board or in to columns that are already full.
  3. The token used to represent Player 1 is X
  4. The token used to represent Player 2 is O, a capitol oh and not a zero
  5. After the game is over the winner should be declared or if there is no winner a tie should be declared
  6. You must split your code up into at least 2 files.
    I personally had 4 separate c files
  7. You must submit a make file named Makefile that when run compiles your program
  8. The executable created by your make file should be named connectn.out
  9. Hints
    This is your first “large” program. It took me around 300 lines of code to complete. You will want to break your problem down into many small manageable functions to make the problem easier to deal with.
    I also recommend testing each function your write as you go along to help you locate your errors early
    I had the following functions defined in my solution and they give a pretty good ordering of how you should write you should solve this problem
    read_args
    create_board, print_board, destroy board
    play_game, get_play, play_is_valid
    game_over, game_won, row_win, col_win, diag_win, right_diag_win, left_diag_win.
  10. Examples
1
2
3
4
../connectn.out
1.Not enough arguments entered
2.Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win
1
2
3
4
../connectn.out 12345
1.Too many arguments entered
2.Usage connectn.out num_rows num_columns
number_of_pieces_in_a_row_needed_to_win
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
../connectn.out 333
2 * * *
1 * * *
0 * * *
012
Enter a column between 0 and 2 to play in: 0
2 * * *
1 * * *
0 X * *
012
Enter a column between 0 and 2 to play in: 1
2 * * *
1 * * *
0 X O *
012
Enter a column between 0 and 2 to play in: 0
2 * * *
1 X * *
0 X O *
012
Enter a column between 0 and 2 to play in: 0
2 O * *
1 X * *
0 X O *
012
Enter a column between 0 and 2 to play in: 0
Enter a column between 0 and 2 to play in: 0
Enter a column between 0 and 2 to play in: -2
Enter a column between 0 and 2 to play in: 4
Enter a column between 0 and 2 to play in: 1
2 O * *
1 X X *
0 X O *
012
Enter a column between 0 and 2 to play in: 2
2 O * *
1 X X *
0 X O O
012
Enter a column between 0 and 2 to play in: 2
2 O * *
1 X X X
0 X O O
012
Player 1 Won!
1
2
3
4
5
6
7
8
9
10
../connectn.out 123
0 * *
01
Enter a column between 0 and 1 to play in: 0
0 X *
01
Enter a column between 0 and 1 to play in: 1
0 X O
01
Tie game!