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
icon
Related questions
Question
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
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.
5. There are
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-1j.
When we no longer need the memory that was allocated to the array, we write:
delete [ ] q;
For this problem, you pass the required length for the array q to your function, which then
allocates q dynamically (using “new") and uses it to hold the solutions for the given size. Each
time the function exits, you must deallocate q by calling delete [ ] q.
For this problem, test your program for n=8.
For some more details on how to use new and delete, go to the
video section of the course web site and see the video called
"Dynamically allocated arrays."
Transcribed Image Text: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 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. 5. There are 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-1j. When we no longer need the memory that was allocated to the array, we write: delete [ ] q; For this problem, you pass the required length for the array q to your function, which then allocates q dynamically (using “new") and uses it to hold the solutions for the given size. Each time the function exits, you must deallocate q by calling delete [ ] q. For this problem, test your program for n=8. For some more details on how to use new and delete, go to the video section of the course web site and see the video called "Dynamically allocated arrays."
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Arrays
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education