Question

In: Computer Science

C++ QUESTION: I had originally made the program with a 8by2 array of "moves" for "knights...

C++ QUESTION: I had originally made the program with a 8by2 array of "moves" for "knights tour". Now that I am trying to make each (row) and (column) individual arrays, I am not able to get the proper output. I'm sure it is a simple error in my for loops, but I haven't been able to make it work. Can you tell me what I need to change ?

// September 11, 2019
// Program sets all of an 8 by 8 array to 0, and then, starting in the left most corner, moves
// about the array making only legal moves as a knight would in chess. Moves are designated in the
// colMove and rowMove array

#include
using namespace std;

//functions
void setToZero(int board[8][8]);
void displayBoard(int oboard[8][8]);

int main()
{
   // chess board 8x8
   int board[8][8];

   // all of the possible moves of the knight in 2 arrays
   int colMove[8] = {2, 1, -1, -2, -2, -1, 1, 2 };
   int rowMove[8] = {-1, -2, -2, -1, 1, 2, 2, 1 };

   int row = 0; // starting row
   int col = 0; // starting column
   int newRow = 0; // transition row
   int newCol = 0; // transition column
   int mover = 1; // position # / tracker increase by 1 everytime a new position is found

   // function to set values to 0
   setToZero(board);

   board[row][col] = 1; // starting position in top left set to one

   bool ableToMove = true; // set to true so that while loop always executes

   while (ableToMove)
   {
       ableToMove = false;

       for (int i = 0; i < 8; i++)
       {
           for (int j = 0; j < 8; j++)
           {

               newRow += row + rowMove[i];
               newCol += col + colMove[j];
          
               // ensures that knight is staying on the board
               if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && board[newRow][newCol] == 0)
               {
                   ableToMove = true; // becomes true if able to move
                   break;
               }
           }

       }
       if (ableToMove) // if became true, values are changed and "mover" (knight) increases

       {
           row += newRow;
           col += newCol;
           board[row][col] = ++mover;

       }
   }

   // function to display board
   displayBoard(board);

   cin.get();
   cin.get();
   return 0;
}

//function to initialize to zero
void setToZero(int fboard[8][8])
{
   for (int i = 0; i < 8; i++)
       for (int j = 0; j < 8; j++)
           fboard[i][j] = 0;

}

//function to display the board
void displayBoard(int oboard[8][8])
{
   int i, j;
   for (i = 0; i < 8; i++)
   {
       for (j = 0; j < 8; j++)
       {
           cout << " " << oboard[i][j] << " ";
       }
       cout << " " << endl;

   }

}

Solutions

Expert Solution

You had done some minor mistakes, I corrected them.

PLEASE GIVE THUMBS UP, THANKS
OUTPUT:

code:

// September 11, 2019
// Program sets all of an 8 by 8 array to 0, and then, starting in the left most corner, moves
// about the array making only legal moves as a knight would in chess. Moves are designated in the
// colMove and rowMove array

#include<iostream>
using namespace std;

//functions
void setToZero(int board[8][8]);
void displayBoard(int oboard[8][8]);

int main()
{
// chess board 8x8
int board[8][8];

// all of the possible moves of the knight in 2 arrays
int colMove[8] = {2, 1, -1, -2, -2, -1, 1, 2 };
int rowMove[8] = {-1, -2, -2, -1, 1, 2, 2, 1 };

int row = 0; // starting row
int col = 0; // starting column
int newRow = 0; // transition row
int newCol = 0; // transition column
int mover = 1; // position # / tracker increase by 1 everytime a new position is found

// function to set values to 0
setToZero(board);

board[row][col] = 1; // starting position in top left set to one

bool ableToMove = true; // set to true so that while loop always executes

while (ableToMove)
{
ableToMove = false;

for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{

newRow= row + rowMove[i];
newCol = col + colMove[j];
  
// ensures that knight is staying on the board
if ((newRow >= 0 && newRow < 8) && (newCol >= 0 && newCol < 8) && board[newRow][newCol] == 0)
{
ableToMove = true; // becomes true if able to move
break;
}
}
if(ableToMove==true)
{
    break;
       }

}
if (ableToMove) // if became true, values are changed and "mover" (knight) increases
{
row = newRow;
col = newCol;
board[row][col] = ++mover;

}

  
}

// function to display board
displayBoard(board);
cin.get();
cin.get();
return 0;
}

//function to initialize to zero
void setToZero(int fboard[8][8])
{
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
fboard[i][j] = 0;

}

//function to display the board
void displayBoard(int oboard[8][8])
{
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
cout << " " << oboard[i][j] << " ";
}
cout << " " << endl;

}
}


Related Solutions

Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to...
For C program, convert the 1-D stencil program from lab03 to use array reference (B[I]) to access array element instead of using pointers. The C program follows these steps: 1) declare two arrays, each has 100 elements; 2) use a for loop to randomly generate 100 integers and store them in one array; 3) use another for loop to do the 1-D stencil and store the result in the other array;
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array
C++ PLEASE---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- In this program, you will analyze an array of 10 characters storing a gene sequence. You will be given a subsequence of 3 characters to look for within this array. If the subsequence is found, print the message: Subsequence <XXX> found at index <i>. Where i is the starting index of the subsequence in the array. Otherwise, print Subsequence <XXX> not found. The array of characters and the subsequence will be given through standard input. Read them and...
C++ Program: Write another program (in C++) that will allocate a local static array of integers...
C++ Program: Write another program (in C++) that will allocate a local static array of integers and then a dynamic array of integers. Are they stored next to each other? You can examine this by examining the memory addresses where they are located. As described in class, on some systems the size of a dynamic array is actually stored in the bytes previous to a dynamically allocated array. Through some experiments on your own, try to see if this is...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10...
Backtracking Write a program in c++ that moves an asterisk ‘*’ in a grid of 10x10 cells. Your program should start with * on position (2,3) i.e. 3rd column of 2nd row. After printing the grid and ‘*’, your program will ask user if he wants to move the ‘*’ Up, Down, Left or Right. If user presses ‘U’, ‘D’, ‘L’ or ‘R’, your program will move the ‘*’ one position accordingly. If user presses ‘Q’, your program should terminate....
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and...
Question 2. Write a complete C++ program that uses a 2-dimensional array with 4 rows and 30 columns. Row represents sections of a course and column represents the students, value inside each position of the array is the final exam grade for each students. Fill the array with random numbers between 40 and 100. Calculate the total, average, maximum, minimum for each section. Please do it simple. code
Write a program in C that declares the following array: int. array[] = { 1, 2,...
Write a program in C that declares the following array: int. array[] = { 1, 2, 4, 8, 16, 32 } Then write some code that accepts a number between 0 and 5 from the user and stores it in a variable called "index". Write some code that retrieves the item specified by the index, like this: int item = array[index]; Then write code that outputs the corresponding array entry based on the number the user entered. Example output: The...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT