Create a program: Eight queens are to be placed on an 8 x 8 chessboard in such a
ID: 3622515 • Letter: C
Question
Create a program:
Eight queens are to be placed on an 8 x 8 chessboard in such a way that one queen is to be in each row. A program will store an array x[] with capacity 8 to represent such a configuration. If x[r] has value c then in row r there is a queen in column c.
Write a program that asks the user to enter the columns that contain queens in the 8 rows. The program then prints the board. For example, if the user enters: 0,3,4,0,0,7,6,7 the program should print:
Q....... ...Q....
....Q...
Q.......
Q.......
.......Q
......Q.
.......Q
Explanation / Answer
please rate - thanks
if the form of the input has to be the way you showed message me I'll make the changes
I also did it as functions hope thats ok. if not message me
#include<iostream>
using namespace std;
void getcolumns(int[]);
void printboard(int[]);
int main()
{
int x[8];
getcolumns(x);
printboard(x);
system("pause");
return 0;
}
void getcolumns(int x[])
{int i;
cout<<"Enter the columns the queens are in (0-7): ";
for(i=0;i<8;i++)
{cout<<"row "<<i+1<<": ";
cin>>x[i];
while(x[i]<0||x[i]>7)
{cout<<"must be between 0 and 7 ";
cout<<"row "<<i+1<<": ";
cin>>x[i];
}
}
}
void printboard(int x[])
{int i,j;
cout<<" The board ";
for(i=0;i<8;i++)
{for(j=0;j<8;j++)
if(x[i]==j)
cout<<"Q ";
else
cout<<". ";
cout<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.