Question: You are to modify the original election from chapter 8 so that it now uses structures rather than parallel arrays. You will need to define

You are to modify the original election from chapter 8 so that it now uses structures rather
than parallel arrays.
You will need to define a structure (struct) for a candidate which contains a name (string),
votes (int), and percentage (double).
You will then need to create a structure for an election which contains an array of 10
candidates, an integer representing how many candidates were in the election, an integer
representing the total votes received in the election, and an integer indicating the location
(index) of the winner.
Create 1 variable of this election structure in main and then pass it to any of your other
functions. You will only need to pass this one item as everything is inside.
You may not use parallel arrays. You must use the structure definitions given above in your
program.
You must use functions appropriately in you program so that very little detail is handled in
main.
Be sure to enforce rules such as there must be between 1 and 10 candidates, and numbers
of votes cant be negative. Be sure to use proper coding style (indentation, good variable
names, etc.) and document your program by commenting: 1) Heading at the top with name,
date, file name, explanation 2) comments describing the purpose of each function and how
it interacts with the other parts of the program 3) comments describing the purpose of
each variable declared 4) comments by code sections describing what is happening in that
section.
When you have completed the program, upload your .cpp file to Canvas as well as a word
document containing 2 test runs where one is exactly like the example below and the other
is an election with exactly 3 candidate. use this code as the start and modify it to meet critera #include
#include
#include
#include
#include
using namespace std;
typedef char Board[3][3]; // Define a Board as a 3 by 3 array of characters
void Display(Board b);
bool winner(const Board b, int n);
bool makeMove(Board& b, char player);
void initializeBoard(Board& b);
void makeCPUMove(Board& b);
int main(){
Board board;
initializeBoard(board); // Initialize the board with '1' to '9'
char player;
int nmoves =0;
bool gameWon = false;
bool againstCPU;
cout <<"Do you want to play against the CPU? (y/n): ";
char choice;
cin >> choice;
againstCPU =(choice =='y'|| choice =='Y');
if (againstCPU){
player ='X'; // Human player is 'X'
} else {
cout << "Player 1, enter your name: ";
cin >> player;
}
while (nmoves <9 && !gameWon){
Display(board);
if (againstCPU && player =='O'){
makeCPUMove(board);
nmoves++;
if (winner(board, nmoves)){
cout << "CPU wins!
";
gameWon = true;
}
} else {
if (makeMove(board, player)){// If a move was successfully made
nmoves++;
if (winner(board, nmoves)){
cout << "Player "<< player <<" wins!
";
gameWon = true;
} else {
player =(player =='X')?'O' : 'X'; // Switch player
}
}
}
}
if (!gameWon){
cout << "The game is a tie!
";
}
Display(board); // Display the final state of the board
return 0;
}
void initializeBoard(Board& b){
char position ='1';
for (int r =0; r <3; r++){
for (int c =0; c <3; c++){
b[r][c]= position++;
}
}
}
void Display(Board b){
cout << endl;
for (int r =0; r <3; r++){
for (int c =0; c <3; c++){
cout << b[r][c];
if (c <2) cout <<"|";
}
cout << endl;
if (r <2) cout <<"-+-+-
";
}
cout << endl;
}
bool winner(const Board b, int n){
if (n <5) return false;
for (int r =0; r <3; r++)
if (b[r][0]== b[r][1] && b[r][0]== b[r][2]) return true;
for (int c =0; c <3; c++)
if (b[0][c]== b[1][c] && b[0][c]== b[2][c]) return true;
if (b[0][0]== b[1][1] && b[0][0]== b[2][2]) return true;
if (b[0][2]== b[1][1] && b[0][2]== b[2][0]) return true;
return false;
}
bool makeMove(Board& b, char player){
int move;
cout << "Player "<< player <<", enter a number to place your "<< player <<": ";
while (true){
if (cin >> move && move >=1 && move <=9 && b[(move -1)/3][(move -1)%3]== char('0'+ move)){
b[(move -1)/3][(move -1)%3]= player;
return true;
} else {
cout << "Invalid move, try again: ";
cin.clear();
cin.ignore(numeric_limits::max(),'
');
}
}
}
void makeCPUMove(Board& b){
srand(static_cast(time(nullptr)));
int move;
do {

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!