Question

Write a program to place eight queens on an 8 x 8 chessboard in such a...

Write a program to place eight queens on an 8 x 8 chessboard in such a way that one queen is to be in each row. A program will use 2 DIMENIONAL array x[r][c] to do this configuration. If x[r] has value c, then in row r there is a queen in column c. Write a program that asks a user to enter the columns that contain queens in the 8 rows. The program then places the queens in these columns (one per row) and prints the board. Make sure 0 is a valid column and you can input 0 and have it display on the checker board like any other number

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in c++:

#include <iostream>

using namespace std;

int main()
{
  
char arr[8][8];
  
for(int i = 0 ; i <8; i++){
for(int j = 0; j <8; j++){
arr[i][j]='*';
}
}
int input;
for(int i = 0 ; i< 8 ; i++){
while(true){
std::cout << "Enter the column you want to put Queen(Q) in row "<<i+1 <<" (enter number b/w 0-7)" << std::endl;
cin>>input;
if(input>-1 && input <8){
break;
}
cout<<"invalid entry\n";
}
  
arr[i][input] = 'Q';
}
  
for(int i = 0 ; i <8; i++){
for(int j = 0; j <8; j++){
cout<<arr[i][j];
}
cout<<endl;
}

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program to place eight queens on an 8 x 8 chessboard in such a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard...

    please explain/ comment 3. Eight Queens Write a program that places eight queens on a chessboard (8 x 8 board) such that no queen is "attacking" another. Queens in chess can move vertically, horizontally, or diagonally. How you solve this problem is entirely up to you. You may choose to write a recursive program or an iterative (i.e., non-recursive) program. You will not be penalized/rewarded for choosing one method or another. Do what is easiest for you. 3.1. Output Below...

  • ================Data Structures C++=============== – Implement the Eight Queens Problem This is a...

    ================Data Structures C++=============== – Implement the Eight Queens Problem This is an object oriented program. This is #1 on page 187 of your book with ONE difference, I’m requiring you add a client program to test your code (a main). If you have the old book the question says “Complete the Queen and Board class for the Eight Queens problem.” On page 179 of your book is a function placeQueens that solves the Eight Queens problem. On page 180 of...

  • Complete the program that solves the Eight Queens problem. The program’s output should look similar to:...

    Complete the program that solves the Eight Queens problem. The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| Use the Queens class given. In your implementation of the Queens class, complete the body of all methods marked as “To be implemented in Programming Problem 1.” Do not change any of the global variable declarations, constructor or placeQueens methods. Here is what I have so far with notes of what is needed. public class Queens...

  • Complete the program that solves the Eight Queens problem in java only please (pages 318 through...

    Complete the program that solves the Eight Queens problem in java only please (pages 318 through 320). The program’s output should look similar to: |1|0|0|0|0|0|0|0| |0|0|0|0|0|0|1|0| |0|0|0|0|1|0|0|0| |0|0|0|0|0|0|0|1| |0|1|0|0|0|0|0|0| |0|0|0|1|0|0|0|0| |0|0|0|0|0|1|0|0| |0|0|1|0|0|0|0|0| PlaceQueens(in currColumn:integer) //places queens in columns numbered currColumn through 8 If (currColumn>8){ The problem is solved } Else { While(unconsidered squares exist in curr column and the problem is unsolved ){ Determine the next square in column currColumn that is not under attack by a queen in an...

  • in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the...

    in c++ Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide missing logic for the class Queens that will enable it to create a two-dimensional array that...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • 110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goa...

    110Marks Question No. 4 Eight queens problem: place 8 queens on a chess board so that no two queens attack each other. -state: locations of 0 to 8 queens (with no two queens attacking each other) goal test: 8 queens placed on the board (with none attacked) operator: place a queen in the left-most empty column such that it is not attacked by any other queen (and does not attack any other queen) path cost: 0 Depth first search: i....

  • Main objective: Solve the n queens problems. You have to place n queens on an n...

    Main objective: Solve the n queens problems. You have to place n queens on an n × n chessboard such that no two attack each other. Important: the chessboard should be indexed starting from 1, in standard (x, y) coordinates. Thus, (4, 3) refers to the square in the 4th column and 3rd row. We have a slight twist in this assignment. We will take as input the position of one queen, and have to generate a solution with a...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The...

    WRITE A JAVA PROGRAM using STACKS and backtracing to solves the N Queens Problem . The program takes the user's input integer for N and prints out all the solutions for N . The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, the following is the output for 4 entered for userinput. Output for 4 Queens : 1- * * Q * Q *...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT