Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[C++ Programming]: Assistance on debugging I'm having troubles debugging my code which is to write a program for the variation of NIM. I get a

[C++ Programming]: Assistance on debugging

I'm having troubles debugging my code which is to write a program for the variation of "NIM". I get a lot of run-time and logical errors throughout this program.

For one, a logic error would occur in the void rod_stats function and the smallest and largest number of stones will not display correctly. The rod selection would also select incorrectly and I'm not quite sure why yet. Additionally, I am unable to debug the rest of my program as I get a segmentation fault during the stone_removal function(s). I'm also encountering other miscellaneous errors as I test out different inputs and I'd encounter infinite loops as I go along.

A solution .exe file was provided and I am supposed to follow this template to get the same result.

image text in transcribed

And here (below) is my program

#include

#include

using namespace std;

// FUNCTION PROTOTYPES GO HERE:

//void_read_rods is function to read in number of rods from user

int read_rods(const int MAX_RODS);

//read_stones is function to read in num stones from user

void read_stones(int rodList[], int numRods, const int MAX_STONES);

//helper function for read_stones

void read_stones_helper(int rodList[], int i, const int MAX_STONES);

//function to draw rods w/percentages and display stats

void draw (int rodList[], int numRods);

//helper function for draw function

void draw_helper (int i, int numStones, int totalStones);

//function to display rod statistics

void rod_stats (int rodList[], int numRods);

//helper function of rod_stats to display smallest numStones

void smallest_rod (int rodList[], int numRods);

//helper function for the largest number of numStones on a rod

void largest_rod (int rodList[], int numRods);

//helper function to display average

double average_rod (int rodList[], int numRods);

//function to check if rods have zero stones or not

bool empty_check (int rodList[], int numRods);

//function to prompt player's next move

void your_move

(int rodList[], int numRods, int playerID, int chosenRod, int stonesRemoved);

//helper function for your_move to prompt player to choose a rod

int choose_a_rod (int playerID);

//helper function for your_move and secondary to chose_a_rod to validate choice

void rod_validation (int playerID, int chosenRod, int numRods);

//helper function for your_move and tritary to chose_a_rod to validate choice

void rod_validation_2 (int rodList[], int playerID, int chosenRod);

int stone_removal_prompt (int rodList[], int stonesRemoved, int chosenRod);

void stone_removal_validation (int rodList[], int stonesRemoved, int chosenRod);

void removal (int rodList[], int chosenRod, int stonesRemoved);

void congrats (int playerID);

void change_players (int & playerID);

int main()

{

// Define variables and constants here

//MAX_RODS is the maximum number of rods that the game allows for

const int MAX_RODS(20);

//MAX_STONES is the max number of stones per rod

const int MAX_STONES(10);

/umRods is the number of rods selected by the user

int numRods(0);

//pointer variable

int * rodList;

int playerID(1);

int chosenRod;

int stonesRemoved;

// Algorithm:

// Prompt and read number of rods to numRods variable

numRods = read_rods(MAX_RODS);

cout

//array declaration here because numRods is called an mod. in read_rod func.

rodList = new int [numRods];

// Prompt and read the number of objects in each rod

read_stones (rodList, numRods, MAX_STONES);

cout

// Draw the rods with percentages

draw (rodList, numRods);

cout

// Display statistics

rod_stats (rodList, numRods);

// WHILE some rod is NOT empty DO

while (empty_check(rodList, numRods) == 0)

{

//empty_check function will return true (anything other than 0) if not empty

// Prompt and read the next player's move

your_move (rodList, numRods, playerID, chosenRod, stonesRemoved);

// Remove the specified number of objects from the specified rod

removal (rodList, chosenRod, stonesRemoved);

// IF all the heaps are empty, THEN

if (empty_check(rodList, numRods)==0)

{

// Print a message congratulating the winning player.

congrats(playerID);

}

// ELSE

else

{

// Redraw the rods with percentages

draw(rodList, numRods);

// Display statistics

rod_stats (rodList, numRods);

cout

}

// Change to the other player

change_players(playerID);

// END IF

// END WHILE

}

return 0;

}

// FUNCTION DEFINITIONS GO HERE:

int read_rods(const int MAX_RODS)

{

//PARAMETER COMMENTS: const int MAX_RODS is max number of rods (20) for game

//Declaring variable numRods which is number of Rods in game

int numRods(0);

//Prompt and read in numRods from user

cout

cin >> numRods;

//Making sure that numRods is less than MAX_RODS and greater than 1. Error msg

if ((numRods>MAX_RODS) || (numRods

{

cout

cout

cin >> numRods;

}

//return the value of numRods to the main function for usage

return (numRods);

}

void read_stones(int rodList[], int numRods, const int MAX_STONES)

{

/* PARAMETER COMMENTS: rodList[] is the array holding the rods PBR

numRods is the user input number of rods

MAX_STONES is a constant integer holding a value of 10

*/

//for loop to iterate through the rods

for (int i=0; i

{

//calling helper function read_stones to read in numstones for each rod

read_stones_helper(rodList, i, MAX_STONES);

}

}

void read_stones_helper(int rodList[], int i, const int MAX_STONES)

{

/* PARAMETER COMMENTS: rodList[] is the array holding the rods PBR

numRods is the user input number of rods

MAX_STONES is a constant integer holding a value of 10

*/

//display and input the number of stones on each rod

cout

cin >> rodList[i];

//reprompting user for numStones in rod if doesn't match criteria

if(rodList[i] >10 || rodList[i]

{

cout

cout

cin >> rodList[i];

}

}

void draw (int rodList[], int numRods)

{

/* PARAMETER COMMENTS: rodList[] is default PBR array holding the stones

in each rod

numRods is the user input number of rods there is in teh game

*/

//variable for the total number of stones in all the rods

int totalStones(0);

for (int k=0; k

{

totalStones += rodList[k];

}

//variable for holding number of stones for each rod

int numStones(0);

//For loop to display numRods with their stones

for (int i=0; i

{

numStones = rodList[i];

draw_helper(i, numStones, totalStones);

double percentage = (double) numStones/(double) totalStones *100;

//FIXME: fix io formatting

cout

cout

}

}

void draw_helper (int i, int numStones, int totalStones)

{

/* PARAMETER COMMENTS: int i is the array index, so that stones can be drawn

for each specific rod at value i (taken from draw function)

numStones is the number of stones for the rod

totalStones is the total number of stones in the game

*/

cout

//

//for loop to output the number of stones rep by * in each rod

for (int j=0; j

{

cout

}

}

void rod_stats (int rodList[], int numRods)

{

/* PARAMETER COMMENTS: rodList is the array which holds the rods and numStones

numRods is the number of rods in the game

*/

//calling of helper functions

smallest_rod(rodList, numRods);

largest_rod (rodList, numRods);

average_rod(rodList, numRods);

cout

}

void smallest_rod (int * rodList, int numRods)

{

/* PARAMETER COMMENTS: rodList is the array which holds the rods and numStones

numRods is the number of rods in the game

*/

//variable to set the smallest rod as the first one and then check in for loop

int smallest = 0;

//j is a variable to iterate through the small_rods array below

int j(0);

//array for holding smallest rods if there are more than one

int small_rods[numRods];

small_rods[j]=smallest;

//for loop to iterate through the rods to determine stats

for (int i=0; i

{

if (rodList[i]

{

smallest= i;

small_rods[j]=smallest;

}

else if (rodList[i]==rodList[smallest])

{

//move forward one spot in small_rod array to assign another spot

j++;

//assign the rod value i to the location j in the smallest_rod array

small_rods[j]=i;

}

}

//calling the function

//another for loop to display all the smallest with the same values

for (int k=0; k

{

cout

//FIXME: does not output number of stones in smallest rod. Outputs 0

}

}

//FIXME: Does not register correct rod to be the largest one

void largest_rod (int * rodList, int numRods)

{

/* PARAMETER COMMENTS: rodList is the array which holds the rods and numStones

numRods is the number of rods in the game

*/

//variable to set the smallest rod as the first one and then check in for loop

int largest = 0;

//j is a variable to iterate through the small_rods array below

int j(0);

//array for holding smallest rods if there are more than one

int large_rods[numRods];

large_rods[j]=largest;

//for loop to iterate through the rods to determine stats

for (int i=0; i

{

if (rodList[i] > rodList[largest])

{

largest= i;

large_rods[j]=largest;

}

else if (rodList[i]==rodList[largest])

{

//move forward one spot in large_rod array to assign another spot

j++;

//assign the rod value i to the location j in the large_rod array

large_rods[j]=i;

}

}

//calling the function

//another for loop to display all the smallest with the same values

for (int k=0; k

{

cout

//FIXME: does not output number of stones in largest rod. Outputs 0

}

}

double average_rod (int rodList[], int numRods)

{

/* PARAMETER COMMENTS: rodList is the array which holds the rods and numStones

numRods is the number of rods in the game

*/

double total_objects = 0.0, average_objects;

int rods_with_zero_objects = 0;

for (int i = 0; i

{

int num_objects = rodList[i];

total_objects += num_objects;

if (num_objects == 0)

{

rods_with_zero_objects++;

}

}

//calculate average

average_objects = total_objects / (numRods - rods_with_zero_objects);

cout

}

bool empty_check (int rodList[], int numRods)

{

//iterate through the array to check if the rods are empty

for (int i=0; i

{

//If one rod has anything more than zero, it's not empty return false

if (rodList[i]>0)

{

return(false);

}

//if all the rods have stones equal to zero, return true

else

{

return(true);

}

}

}

void your_move

(int * rodList, int numRods, int playerID, int chosenRod, int stonesRemoved)

{

/* PARAMETER COMMENTS: rodList[] is the array of rods and stones

numRods is the size of teh array, also the user inputted number of rods

playerID is the player ID either integer 1 or 2

chosenRod is the rod that the player chooses to manipulate.

stonesRemoved is the number of stones the player chooses to remove

*/

//call helper function to prompt user to choose a rod

chosenRod = choose_a_rod (playerID);

rod_validation (playerID, chosenRod, numRods);

rod_validation_2 (rodList, playerID, chosenRod);

stone_removal_prompt (rodList, stonesRemoved, chosenRod);

}

int choose_a_rod (int playerID)

{

int chosenRod;

cout

cin >> chosenRod;

return (chosenRod);

}

void rod_validation (int playerID, int chosenRod, int numRods)

{

while (1)

{

if ((chosenRod (numRods-1)))

{

cout

choose_a_rod(playerID);

}

else

{

break;

}

}

}

void rod_validation_2 (int * rodList, int playerID, int chosenRod)

{

while (1)

{

if (rodList[chosenRod]

{

cout

choose_a_rod(playerID);

}

else

{

break;

}

}

}

int stone_removal_prompt (int * rodList, int stonesRemoved, int chosenRod)

{

cout

cin >> stonesRemoved;

return(stonesRemoved);

}

void stone_removal_validation (int * rodList, int stonesRemoved, int chosenRod)

{

while(1)

{

if ((stonesRemoved

{

cout

stone_removal_prompt (rodList, stonesRemoved, chosenRod);

}

else

{

break;

}

}

}

void removal (int * rodList, int chosenRod, int stonesRemoved)

{

rodList[chosenRod]-=stonesRemoved;

}

void congrats (int playerID)

{

cout

}

void change_players (int & playerID)

{

if (playerID==1)

{

playerID=2;

}

else

{

playerID=1;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions