代写Assignment 1 CMPT 125

- 首页 >> Database作业

Assignment 1 CMPT 125
This assignment consists of two problems.
For the first problem you will submit a file with the name Sier.c, for the second problem you will
submit a file Mastermind.c.
80% of each problems mark will be based on running tests (see next paragraph for details) and
the remaining 20% will be for specific specifications of the code in the problem that are not
testable by running the code. For example, did you correctly allocate and deallocate dynamic
memory rather than using automatic arrays, or did you properly clean up before each place the
code is terminated.
Each problem will be graded by comparing the output produced by your program to the
reference solution. The given error messages should be used, and the outputs in the test plan
should be matched character by character by your output to receive all points. Parts of the
outputs of the tests will be chosen for grading. Points will be allocated only for those chosen
parts. Each part of the output chosen will represent a requirement stated in the description of
the problem. Some of the tests provided to you in the test plan will be modified by changing
input values and used for grading. Not all functionalities of the program are tested in the
provided test plan. Some additional functionalities not tested in the provided test plan will be
graded using additional tests.
PROBLEM 1 (40 points): You will generate a pattern called a Sierpinski gasket on a board of
M by N squares (M<80 and N<40). The board will be represented by a 2-D array. You should use
the exact prompts and error messages specified in the test plan below in your code. Outputs of
your code will be checked for character-by-character correspondence to the sample outputs.
You may assume that the data entered by the user using the keyboard is of the correct type
(integer, double …), but you must check that the values are in the correct ranges. You must also
check if the data in the input file is of the correct type and in the correct range.
1) Include and use a function for setting all elements in a 2-D array of size M rows by N columns
to 0. This function should have the prototype
void FillZero( int ** my2DArray, int M, int N);
2) Include and use a function to do a deep copy of the elements in 2-D array myNextGenArray
of size M rows by N columns into 2-D array my2Darray of the same dimensions. This function
will have the prototype
void CopyArray(int ** my2DArray, int**myNextGenArray, int M, int N);
This function must be implemented using pointer arithmetic.
3) Include and use a function for Calculating the next generation array. This function should have
the prototype below. In that prototype board is the 2- D (M by N) array holding the game
board. The requirements of this function are discussed later.
void NextGen(int ** board, int M, int N);
4) Include the following steps in the main program.
a. Prompt the user for the name of an output file using the prompt:
Enter the name of the output file:
b. Open the output file and check that it has been opened correctly. If the output file has
not been opened correctly print the error message below, clean up as necessary and
terminate the program.
ERROR: Input file not opened correctly.
c. Open the input file and check that it has been opened correctly. If it has not been opened
ERROR: Output file not opened correctly.
d. Prompt the user for the number of rows in the game board using the prompt:
Enter the number of rows in the board (0

e. Try to read numRows.
i. If the value of numRows is not an integer print the error message below, clean up,
then terminate the program.
ERROR: The value of numRows is not an integer
ii. If the value of the number of rows read in is out of range, print the error message
below, then repeat step e
ERROR: Read an illegal number of rows for board
TRY AGAIN, 0 < number of rows < 40
f. Prompt the user for the number of columns in the game board using the prompt:
Enter the number of columns in the board (0g. Try to read numCols.
i. If the value of numCols is not an integer print the error message below, clean up,
then terminate the program.
ERROR: The value of numCols is not an integer
ii. If the value of the numCols read in is out of range, print the error message and
prompt below, then repeat step g.
ERROR: Read an illegal number of columns for board
TRY AGAIN, 0 < number of columns < 80
h. Read the number of generations from the input file.
i. If no value has been read into the variable holding the number of generations print
the error message below, clean up as necessary and terminate the program. (This
happens when you are end of file)
ERROR: Could not read the number of generations
ii. If the value of the number of generations in the file is not an integer print the error
message below, clean up, then terminate the program.
ERROR: number of generations is not an integer
iii. If the value of the number of generations read in is out of range your program will
print the error message, clean up, then terminate the program.
ERROR: Read an illegal number of generations
i. Read the increment between successive generations from the input file.
i. If no value has been read into the variable holding the generation increment, print
the error message below, clean up as necessary and terminate the program. (This
happens when you are end of file)
ERROR: Could not read the generation increment
ii. If the value of the number of generations in the file is not an integer print the error
message below, clean up, then terminate the program.
ERROR: generation increment is not an integer
iii. If the value of the number of rows read in is out of range your program will print
the error message, clean up, then terminate the program.
ERROR: Read an illegal generation increment
j. Dynamically allocate one 2-D integer array with identifier board and with dimensions
number of rows by number of columns. Initialize all elements the board to 0. This will be
the array that represents the gameboard in the main program.
k. Initialize board[1][3]=1
l. For each generation requested your main program should: complete the following steps.
i. Call function NextGen to calculate the game board for the next generation.
ii. If this is the first generation calculated print the title line, then the game array,
then a blank line to your output file
iii. If this is the first generation calculated print the title line, then the game array,
then a blank line to your screen
iv. If this is the (P*genIncrem+1) the generation calculated print the title line, then
the game array, then a blank line to your output file
v. If this is the (P*genIncrem+1) the generation calculated print the title line, then
the game array, then a blank line to your screen
vi. If this is the last generation calculated print the title line, then the game array,
then a blank line to your output file
vii. If this is the last generation calculated print the title line, then the game array,
then a blank line to your screen
5) Include the following steps in your NextGen function.
a. Dynamically allocate one 2-D integer array with identifier nextGenBoard and with
dimensions M by N. Initialize all elements the board to 0. This will be the array that
represents the gameboard in the main program. Remember to free nextGenBoard before
leaving the function NextGen.
b. Every square on the top edge and the left-hand edge of the board must be 0 and remain
0 for every generation. Every other square in the board (not on the top or the left-hand
edge) contains a 1 or a 0.
c. To calculate what each element in nextGenBoard should be use the following relations:
If k=0 or j=0 or k=j=0 nextGenBoard[k][j] = 0;
Otherwise nextGenBoard[k][j] = ( board[k][j] + board[k-1][j] + board[k][j-1] ) % 2
d. The array holding the game board for the present generation is board. The array holding
the game board for the next generation is nextGenBoard.
e. After nextGenBoard has been completely determined deep copy the values in
nextGenBoard into array board


TEST PLAN
For each of the tests below the items shown in red are the information typed in by the user.
1) Specified input file is missing.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out1.txt
Enter the name of the input file: in1.txt
ERROR: Input file not opened correctly.
2) Specified input file is empty.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out2.txt
Enter the name of the input file: in2.txt
Enter the number of rows in the board (0<number<40) -5
ERROR: Read an illegal number of rows for board
TRY AGAIN, 0 < number of rows < 40 99
ERROR: Read an illegal number of rows for board
TRY AGAIN, 0 < number of rows < 40 9
Enter the number of columns in the board (0<number<80) -23
ERROR: Read an illegal number of columns for board
TRY AGAIN, 0 < number of columns < 80 122
ERROR: Read an illegal number of columns for board
TRY AGAIN, 0 < number of columns < 80 12
ERROR: Could not read the number of generations
EMPTY OUTPUT FILE CREATED
3) Specified input file contains one integer, 7.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out3.txt
Enter the name of the input file: in3.txt
Enter the number of rows in the board (0<number<40) 12
Enter the number of columns in the board (0<number<80) 9
ERROR: Could not read the generation increment
EMPTY OUTPUT FILE CREATED

4) Specified input file contains one integer,7 and one space
followed by the character a.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out4.txt
Enter the name of the input file: in4.txt
Enter the number of rows in the board (0<number<40) 12
Enter the number of columns in the board (0<number<80) 9
ERROR: generation increment is not an integer
EMPTY OUTPUT FILE CREATED
5) Specified input file contains one integer -12.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out5.txt
Enter the name of the input file: in5.txt
Enter the number of rows in the board (0<number<40) 12
Enter the number of columns in the board (0<number<80) 9
ERROR: Read an illegal number of generations
EMPTY OUTPUT FILE CREATED
6) Specified input file contains two integers, 9 and 44.
SCREEN INPUT AND OUTPUT
Enter the name of the output file: out6.txt
Enter the name of the input file: in6.txt
Enter the number of rows in the board (0<number<40) 12
Enter the number of columns in the board (0<number<80) 9
ERROR: Read an illegal generation increment
EMPTY OUTPUT FILE CREATED
7) Case with no errors: test which generations are printed
Please refer to provided files for case 7. Too much to print
here.
8) Case 8, 2nd case without errors see next page for outputs.

Sierpinski gameboard: generation 1 Input file in8.txt
File output file out8.txt
11 screen input and output
1




Sierpinski gameboard: generation 2

1 1

1

Sierpinski gameboard: generation 3

1111
1 1
11
1

Sierpinski gameboard: generation 4

1 1

1

Sierpinski gameboard: generation 5

11 11
1 1

11
1

Sierpinski gameboard: generation 6

1 1 1 1

1 1

1 1

1
Sierpinski gameboard: generation 7

11111111
1 1 1 1
11 11
1 1
1111
1 1
11

An additional example will be added by Wednesday May 31
Enter the name of the output file: out8.txt
Enter the name of the input file: in8.txt
Enter the number of rows in the board (0<number<80) 8
Enter the number of columns in the board (0<number<80) 20
Sierpinski gameboard: generation 1
11
1
Sierpinski gameboard: generation 7
11111111
1 1 1 1
11 11
1 1
1111
1 1
11

7 1
PROBLEM 2 (60 POINTS): You will build a variant on the game MASTERMIND using C. This
variant will use the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The computer (your program) will create
an ordered group of 6 of these digits. Each of the 6 digits will be randomly selected. Each digit
can appear in the group of digits any number of times (from 0 to 6). Your program will use up to
four arrays each with six locations. Three of the arrays will hold integers. The first array, the
solution array, holds the 6 digits the player is trying to guess. The second array, the guess array
holds the 6 digits of the guess made by the player. The third array, the index array holds integers
that keep track of which integers in the guess array match or partially match the values in the
solution array.
The user will try and guess the group of digits. The user must guess the correct digits in the correct
order to win. Both the value of each digit and the position of each digit in the ordered group of
digits must be correct for the user to win. To help the user discover the correct group of digits
you give the user some information about which digits in his guess are correct. You will tell the
user how many digits are correct (the correct digit in the correct place), and how many digits are
partially correct, the correct digit is present but in the wrong place. For example, if the sequence
chosen by the computer was
1 2 4 3 2 2
and the user guessed
2 3 4 5 6 7
You would tell the user he had 1 digit correct (the 4) and 2 digits partially correct (the 2 and the
3). You do not tell the user which digits are correct or partially correct, just how many.
If the user made a second guess
2 3 2 0 1 9
You would tell the user he had 0 digits correct and 4 digits partially correct (2, 3, 2, 1)
If the users next guess was
1 2 3 4 9 9
You would tell the user he had 2 digits correct (1, 2) and 2 digit partially correct (3,4)
If the users next guess was
2 2 2 7 4 4 4
You would tell the user he had 1 digit correct (2nd 2) and 3 digits partially correct (1st 2, 3rd 2, 4)
Your game should
1. Use the pseudo random number generator ( functions rand() and srand() ) to create a
random sequence of 6 digits between 0 and 9 inclusive. Place these digits, in the order
they are generated, into your answer array.
2. Read the characters the user entered as his guess.
a. The guess will be one line of characters terminated by moving to a new line.
b. The guess may contain any characters.
c. Any space or tab characters in the guess will be ignored.
d. After reading six digits from the guess, the remaining characters in the guess will
be ignored.
e. If any characters in the guess, that are not digits, spaces, or tabs, are encountered
before 6 digits have been read the guess will be invalid.
f. Any guess that does not contain enough digits to complete the guess, and does
not contain any characters other than digits, spaces, or tabs, will cause the user
to be prompted to enter the remaining digits. (See prompt in tests below)
g. The values in each guess will be stored in an array of length six.
h. Any guess that is found to be invalid will cause an error message to be printed,
followed by a request to enter a new guess. Before requesting the new guess any
remaining characters in the stream should be cleared.
i. NOTE: you can read one character using scanf with %c, or by using getc().
Each time the user inputs a complete and correct 6-digit guess, respond to his guess by doing
each of the following:
1. Keep track of which elements in the answer and which elements in the guess have been
paired.
a. An element in the answer array can be paired with an element in the guess array if
the two elements hold the same value.
b. An element in the answer array can be paired with only one element in the guess
array. An element in the guess array can be paired with only one element in the
answer array. An element in either array need not be paired.
c. An element K in the answer array can be paired with an element H in the guess array.
i. When K = H the elements form a matched pair
ii. When K ≠ H then the elements form a partially matched pair.
d. An element in either array may not be paired.
2. Determine the index of each of the matched pairs of elements for the present guess.
3. Determine the number of matched pairs of elements for the present guess. This number is
the number printed before the word “matches” in the last line of output in the block for each
guess except the last one (the correct one).
4. If every digit in the users guess has the same value as the digit with the same index in the
answer, then tell the user he has guessed the sequence of digits and exit the program.
5. Consider only the elements of the solution that are NOT part of matched pairs of elements.
a. For each unpaired element H in the guess array
i. Consider the value of H to be represented by Q.
ii. Try to find the first unpaired element, K , of the answer array with value Q.
iii. If a matching element is found elements H and K will be paired as a partially
matched pair
b. Count the number of elements that are paired in a, this is the number of partial
matches. (Partially matched pairs)
6. Print the response to the users guess.
7. Reinitialize all the elements of your guess array to 0.
8. Repeat steps 2 to 6 for the next guess.
To make it easier to debug your program you may wish to by always using the same value of the
seed so that you have the same solution each time you run the program.
All output must exactly match the formats and messages illustrated below in the test plan. Copies
of the files will be posted on Canvas so you can be sure of the spacing. Many browsers change
the spacing shown below.

GAME 1 (windows visual studio)
Enter the integer value of the seed for the game: 45678
For each turn enter 6 digits 0 <= digit <= 9
Spaces or tabs in your response will be ignored.
Enter your guess, 6 digits
0 12 345
Your guess was
0 1 2 3 4 5
My response was
0 matches 2 partial matches
Enter your guess, 6 digits
0 1 0 1 0 1
Your guess was
0 1 0 1 0 1
My response was
0 matches 2 partial matches
Enter your guess, 6 digits
12121233333
Your guess was
1 2 1 2 1 2
My response was
4 matches 0 partial matches
Enter your guess, 6 digits
126 6 125 84
Your guess was
1 2 6 6 1 2
My response was
5 matches 0 partial matches
Enter your guess, 6 digits
1 2 6 7 1 2
YOU DID IT!!

GAME 2
Enter the integer value of the seed for the game: 144ae4
For each turn enter 6 digits 0 <= digit <= 9
Spaces or tabs in your response will be ignored
Enter your guess, 6 digits
3 57
You need to enter 3 more digits to complete your guess
6 a 4 6
ERROR: A character in your guess was not a digit or a white space
Enter your guess, 6 digits
7 8 9 7 8 9 9 9
Your guess was
7 8 9 7 8 9
My response was
0 matches 2 partial matches
Enter your guess, 6 integers separated by spaces
3699case9
ERROR: A character in your guess was not a digit or a white space
Enter your guess, 6 digits
5 5 5 8 9 5
Your guess was
5 5 5 8 9 5
My response was
0 matches 1 partial matches
Enter your guess, 6 digits
4 1 8 454 5 3 a
Your guess was
4 1 8 4 5 4
My response was
1 matches 3 partial matches
Enter your guess, 6 integers separated by spaces
1 2 4 xxx1 2 4
ERROR: could not read your input
Enter your guess, 6 digits
5 5 4 4 3 3
Your guess was
5 5 4 4 3 3
My response was
1 matches 1 partial matches
Enter your guess, 6 digits
8 4 7 4 4
You need to enter 1 more digits to complete your guess
0
YOU DID IT!!
CASE 2: Game 3
Enter the integer value of the seed for the game: a2435wt23
Try again you made an error
Enter the integer value of the seed for the game: 333333
For each turn enter 6 digits 0 <= digit <= 9
Spaces or tabs in your response will be ignored
Enter your guess, 6 digits
3 5 7 6 4 2
Your guess was
3 5 7 6 4 2
My response was
0 matches 2 partial matches
Enter your guess, 6 digits
3 5 8 8 9 9
Your guess was
3 5 8 8 9 9
My response was
1 matches 1 partial matches
Enter your guess, 6 digits
9 1 2 2 9 6
YOU DID IT!!

站长地图