By the "n queens problem" we mean the problem of placing n queens on an nXn “chessboard" in such a way that no queen can capture any other on the next move. In class we solved the “8 queens" problem. Write a function that inputs an integer n and returns the number of solutions to the “n queens" problem. Your function should use the one dimensional representation for the board, the algorithm we discussed in class, and no gotos. Test your function with a main program that prompts the user for an integer n. The main program then calls the function n times, once for each number from 1 – n, and then prints the number of solutions to each of these problems, one on a line. For example, if you enter n=5 your program should output: 1. There are 2. There are 3. There are 4. There are 5. There are solutions to the 1 queens problem. solutions to the 2 queens problem. solutions to the 3 queens problem. solutions to the 4 queens problem. solutions to the 5 queens problem. Now, since each time through the loop you will need an array q of a different length, you will need to allocate the array off of the heap (which we mentioned in class) and not the run-time stack. To do this you use the "new operator" to request the heap to dynamically allocate the memory for you. For example, to get a one dimensional array of integers of size n and called q, we use the following syntax: int* q = new int[n]; This allocates the array for us dynamically, at run-time. After this we can use the array q just as if it had been declared "normally" and it has n elements denoted q[0] through q[n-1]. When we no longer need the memory that was allocated to the array, we write: delete [ ] q;
By the "n queens problem" we mean the problem of placing n queens on an nXn “chessboard" in such a way that no queen can capture any other on the next move. In class we solved the “8 queens" problem. Write a function that inputs an integer n and returns the number of solutions to the “n queens" problem. Your function should use the one dimensional representation for the board, the algorithm we discussed in class, and no gotos. Test your function with a main program that prompts the user for an integer n. The main program then calls the function n times, once for each number from 1 – n, and then prints the number of solutions to each of these problems, one on a line. For example, if you enter n=5 your program should output: 1. There are 2. There are 3. There are 4. There are 5. There are solutions to the 1 queens problem. solutions to the 2 queens problem. solutions to the 3 queens problem. solutions to the 4 queens problem. solutions to the 5 queens problem. Now, since each time through the loop you will need an array q of a different length, you will need to allocate the array off of the heap (which we mentioned in class) and not the run-time stack. To do this you use the "new operator" to request the heap to dynamically allocate the memory for you. For example, to get a one dimensional array of integers of size n and called q, we use the following syntax: int* q = new int[n]; This allocates the array for us dynamically, at run-time. After this we can use the array q just as if it had been declared "normally" and it has n elements denoted q[0] through q[n-1]. When we no longer need the memory that was allocated to the array, we write: delete [ ] q;
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education