Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++ ASAP) I doneed your help for C++ program!! This is modifying the code. I th

ID: 3739712 • Letter: #

Question

(C++ ASAP) I doneed your help for C++ program!!

This is modifying the code. I think my code is getting close, but I am really struggling..

SO I need your help!

(Read carefully the following instruction please!!)

I will attach the source code under the following instruction.

**Please do not use any pointer or class.

**The output should be exactly like the instruction.

----------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>

#include<cmath>

using namespace std;

int board[100] = {0};

bool place(int row, int column)

{

for(int i = 1; i <= row - 1; ++i)

{

if(board[i] == column) //

{

return false;

}

else if (abs(board[i] - column) == abs(i - row))

{

return false;

}

}

return true;

}

void print(int n)

{

for (int i = 1; i <= n; i++)

{

for (int j = 1; j <= n; j++)

{

if (board[i] == j)

{

cout << "Q";

}

else

{

cout << ".";

}

}

cout << endl;

}

cout << endl;

}

void queen(int row, int n)

{

for (int column = 1; column <= n; column++)

{

if(place(row, column))

{

board[row] = column;

if (row == n)

{

print(n);

}

else

{

queen(row + 1, n);

}

}

}

}

int main()

{

int size, num;

cout << "What size board do you want?" << endl;

cin >> size;

cout << "How many queens do you want to place?" << endl;

cin >> num;

queen(1, size);

}

Problem B: N-Queens (20 points) First ask the user to enter what size board they want (you can assume it will be less than 100), then how many queens you want to place on this board. (This problem is actually fairly popular and is called the "N-Queens" problem, but I would suggest not googling for code as you should not be taking large portions of code from online.) The queens need to be in positions where each queen cannot immediately take another queen (i.e. not in any increment of 45 degrees from another). If it is not possible to put all the queens requested on the board, simply say "impossible" and don't show anything else. You the following format 1. character 'Q' for a queen 2. character'.' (period) for an immediately capture-able square 3. character '-' (hyphen) for an open space 4. Use no additional markers other than the above when showing the grid. 5. The word "impossible" must appear if no valid configuration exists. Hint: This is quite similar to the "sudokuSolver.cpp" that we did in class, so I would suggest starting with that code and modify it Example 1 (user input is underlined): What size board do you want? 3 How many queens do you want to place? Impossible. Example 2 (user input is underlined): What size board do you want? 4 How many queens do you want to place? 4

Explanation / Answer

Answer:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#define N 8
using namespace std;
void displayValue(int test[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout<<test[i][j]<<" ";
cout<<endl;
}
}
bool isSafe(int test[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
{
if (test[row][i])
return false;
}
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (test[i][j])
return false;
}

for (i = row, j = col; j >= 0 && i < N; i++, j--)
{
if (test[i][j])
return false;
}

return true;
}
bool Service(int test[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++)
{
if ( isSafe(test, i, col) )
{
test[i][col] = 1;
if (Service(test, col + 1) == true)
return true;
test[i][col] = 0;
}
}
return false;
}
bool Testservice()
{
int test[N][N] = {0};
if (Service(test, 0) == false)
{
cout<<"Solution does not exist"<<endl;
return false;
}
displayValue(test);
return true;
}
int main()
{
Testservice();
return 0;
}