CMPSC 122 Homework #1
1) Consider the following code snippet. It can produces a segmentation fault, which will occur if a program tries to access an invalid memory address.
#include
using namespace std;
int* foo() {
int x = 0;
return &x;
}
int main() {
int* y = foo();
*y = 10;
cout << "y = " << *y << endl;
}
Explain why this is occurring. (10 points)
2) Without changing any of the function signatures or adding any global variables, rewrite the code above so that foo returns the address of an integer that can be used in the main function. (10 points)
3) Consider the code below, which is similar to the code in problem 1 in that it is passing the address of a local variable to another function. This code is ok, where problem 1’s is not. Explain why. (10 points)
#include
using namespace std;
void foo(int *x) {
*x = 0;
}
int main() {
int y = 10;
foo(&y);
cout << "y = " << y << endl; }
4) Consider the following variant of the two-player game called Blotto. Each player is given 100 soldiers to position on 10 battlefields, numbered 1 to 10. A player wins a battlefield i, if they have put more soldiers than the other player on that battlefield AND if the difference between the soldiers is greater than or equal to the difference in a battlefield they have won with a number less than i. Winning a battlefield i is worth i points, and the player with the most points wins.
For example, suppose that player 1 allocates their soldiers as follows: [1,1,0,5,6,7,16,11,18,35] and player 2 allocates their soldiers as follows: [10,10,10,10,10,10,10,10,10,10].
Battlefield
1
2
3
4
5
6
7
8
9
10
Total
Player 1 Allocation
1
1
0
5
6
7
16
11
18
35
Player 2 Allocation
10
10
10
10
10
10
10
10
10
10
Player 1 Margin of Victory
6
1
8
25
Player 1 Points
7
9
10
26
Player 2 Margin of Victory
9
9
10
5
4
3
Player 2 Points
1
2
3
6
In this game, player 1 would win, by scoring 26 points versus player 2’s 6 points. Note that player 2 placed more soldiers on some battlefields but did not have a non-decreasing margin of victory, so they did not get credit for these. For example, on Battlefield 4, they won by 5 soldiers. However, their biggest victory on a previous battlefield (#3) was by 10 soldiers, so they do not earn any points for battlefield 4. Similarly player 1 did not earn any points for Battlefield 8.
Complete the functions in the following code, based on the post-conditions, in order to create a fully functioning game of Blotto. (65 points)
Notes:
Do not create any new functions.
Avoid the use of “magic” numbers in the code. The only numeric literal that you should
use is 0. Use the named constants instead (e.g. BATTLEFIELD_COUNT, SOLDIER_COUNT,
etc., as definied in blotto.h)
The rand() method returns a random number in the range 0 to RAND_MAX (where
RAND_MAX is at least 32,767).
blotto.cpp
#include
#include
#include
#include “blotto.h” // Defines COMPUTER_MOVES, BATTLEFIELD_COUNT, and COMPUTER_MOVE_COUNT
using namespace std;
// Post-condition: returns a dynamically allocated array with 10 elements, containing an
// allocation of 100 soldiers to the 10 battlefields. The values in the array should come from // user input (i.e. cin). The code should ensure that the user adds exactly 100 soldiers, no // more, no less. Note: throughout this code, replace the question marks with valid code.
??? getPlayerMove() {
???
}
// Post-condition: selects a randomly selected array of 10 elements from one of the rows of // the COMPUTER_MOVES 2-d array. The row are output, and the array is returned.
// The output should look like: “The computer selected <#, #, #, #, #, #, #, #, #, #>.”
// where the # are replaced with the soldier counts.
??? getComputerMove() {
int index = rand() % COMPUTER_MOVE_COUNT; ???
}
// Pre-condition: player and opponent each contain a valid allocations // Post-condition: the number of points that player scores in this game ??? getPlayerPoints(??? player, ??? opponent) {
???
}
int main() {
srand(time(0)); // Seed the random number generator
cout << "Colonel Blotto\n\n";
char choice = 'y';
while (choice == 'y') {
// TODO: Complete this method to play a game of Blotto. Be sure that your program does // not contain a memory leak.
??? player = getPlayerMove();
??? computer = getComputerMove();
??? playerPts = getPlayerPoints(player, computer); ??? computerPts = getPlayerPoints(computer, player);
cout << "You scored " << playerPts << " and the computer scored " << computerPts << endl;
if (playerPts > computerPts) cout << "You win!!!\n";
else if (playerPts < computerPts) cout << "You lose.\n";
else cout << "You tie.\n";
???
cout << "Would you like to play again (y/n)? ";
cin >> choice;
}
return 0; }
5) Provide an allocation for this game that you think will score the highest average points in games against your classmates. Note that the goal is the highest average points, not the most wins. Up to 5 points of extra credit will be awarded to the top entries. (5 points + up to 5 points extra credit)