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 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
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 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 and 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: Heading at the top with name,
date, file name, explanation comments describing the purpose of each function and how
it interacts with the other parts of the program comments describing the purpose of
each variable declared 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 test runs where one is exactly like the example below and the other
is an election with exactly candidate. use this code as the start and modify it to meet critera #include
#include
#include
#include
#include
using namespace std;
typedef char Board; Define a Board as a by array of characters
void DisplayBoard b;
bool winnerconst Board b int n;
bool makeMoveBoard& b char player;
void initializeBoardBoard& b;
void makeCPUMoveBoard& b;
int main
Board board;
initializeBoardboard; Initialize the board with to
char player;
int nmoves ;
bool gameWon false;
bool againstCPU;
cout Do you want to play against the CPU? yn: ;
char choice;
cin choice;
againstCPU choice y choice Y;
if againstCPU
player X; Human player is X
else
cout "Player enter your name: ;
cin player;
while nmoves && gameWon
Displayboard;
if againstCPU && player O
makeCPUMoveboard;
nmoves;
if winnerboard nmoves
cout "CPU wins!
;
gameWon true;
else
if makeMoveboard player If a move was successfully made
nmoves;
if winnerboard nmoves
cout "Player player wins!
;
gameWon true;
else
player player XO : X; Switch player
if gameWon
cout "The game is a tie!
;
Displayboard; Display the final state of the board
return ;
void initializeBoardBoard& b
char position ;
for int r ; r ; r
for int c ; c ; c
brc position;
void DisplayBoard b
cout endl;
for int r ; r ; r
for int c ; c ; c
cout brc;
if c cout ;
cout endl;
if r cout
;
cout endl;
bool winnerconst Board b int n
if n return false;
for int r ; r ; r
if br br && br br return true;
for int c ; c ; c
if bc bc && bc bc return true;
if b b && b b return true;
if b b && b b return true;
return false;
bool makeMoveBoard& b char player
int move;
cout "Player player enter a number to place your player : ;
while true
if cin move && move && move && bmove move char move
bmove move player;
return true;
else
cout "Invalid move, try again: ;
cin.clear;
cin.ignorenumericlimits::max
;
void makeCPUMoveBoard& b
srandstaticcasttimenullptr;
int move;
do
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
