Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

The Reversal Game

For this assignment you are to write and test a number of functions to solve a puzzle. As usual, your program will be marked by compiling it using g++ on Linux; therefore you should test your program with this compiler and in the Linux OS before submitting it. If you fail to do this, and if your program does not compile it will not be marked.

The Game
The reversal game is a game played with permutations of the integers from 1 to n. A permutation is a set of numbers arranged in a particular order. For example, the possible permutations of the integers from 1 to 3 are: {1,2,3}, {1,3,2}, {2,1,3}, {2,3,1}, {3,1,2} and {3,2,1}. Notice that there are n! = 3! = 3*2*1 = 6 such permutations. Also note that values in sets are unique - that is, a set cannot contain duplicates. So {2,1,2} is not a set (it is actually referred to as a bag).

In the reversal game you start with any permutation of the integers 1 to n you like. Here, for example, is one permutation where n = 5:

3 4 5 1 2 is one permutation of the integers from 1 to 5

Playing the game consists of repeatedly reversing the order of the first m digits, where m is always the first number of the permutation. The game stops when the first number of the permutation is 1. The goal is to do as many reversals as possible.

In the permutation given above, m, the first number, is 3, so you reverse the first 3 numbers of the permutation:

3 4 5 1 2 - the first number is 3 so reverse the first three numbers (3 4 5 becomes 5 4 3)
5 4 3 1 2
Now the first number is a 5, and so you reverse the first 5 numbers (i.e. all of them):

5 4 3 1 2 - reverse the first five numbers
2 1 3 4 5


Objective
To play the reversal game you reverse the first m elements of the permutation, where m is the first element of the permutation. The game ends when m equals 1.

The goal of the reversal game is to maximize the score for a particular value of n, in other words to find a permutation of 1, 2, 3, ..., n that needs the greatest number of reversals to get 1 to the start of the permutation.

For even relatively small values of n this is a hard problem to solve since there are n! = 1 * 2 * ... * n different permutations of 1 to n, and there is no obvious pattern to how high-scoring permutations are distributed. And recall that factorials get large very quickly; 5! = 120, 10! = 3,628,800 and 15! = 1,307,674,368,000.
1 - Check Permutation
Write a function that checks to see if its integer array parameter is a permutation, i.e. that it contains all the integers from 1 to n in any order. The function has parameters for the permutation (and integer array) and n (the size of the array)
You should perform enough tests that you are confident isPermutation works correctly in all cases.
2 - Create Permutation
Write a function that generates and returns a permutation where the values are in ascending order. The permutation should be created in a dynamic array, and the function should have a single integer parameter that specifies the permutation's size.
3 - Permutation Score
Write a function that computes and returns the score for a permutation, i.e. the number of reversals required to make the first element of the array equal to 1.
You should perform enough tests that you are confident score works correctly in all cases.

Freeing Dynamic Memory
It's often tricky to determine when to free dynamic memory. Essentially you need to free it when it is no longer required (but not before) and also before you lose the opportunity to free it. Let's look at a concrete example - the score function. Assume that you make a copy as I suggested, then score might look something like this (written in pseudo-code, not C++).
int score(arr)
result = 0
perm = copy(arr) //where perm is an int* to an array in dynamic memory
while not done //i.e. while perm[0] != 1
reverse(perm)
increment result
delete[] perm
return result
Note that you can't de-allocate the memory allocated to perm until you've finished using it, so this can't be done in the loop that is passing perm to the reverse function. On the other hand the memory can't be de-allocated outside the score function since there is no way of accessing the pointer outside the function, and, in any case, it isn't needed at that point. Thus the appropriate time to call delete[] is just before returning from score.
4 - Permutation Print
Write a function that prints a permutation and its score and then a newline (endl) character.
5 - Brute Force
Write a function that uses "brute force" to find and return the permutation with the highest score for small values of n. The function should find the permutation with the highest score by testing every possible permutation of that size.

6 - Brute Force Test
Write a second function called bruteForceTest that uses the bruteForce function to find and then print the highest scoring permutations for values of n from 2 to 11.

7 - High Scoring Permutations
Create and implement your own algorithm for finding high-scoring permutations. To show that it works, have your program create the following output file.

8 - Scoring Output File

Your output file will be scored by comparing it to a set of permutations that the marker has created. Each of your permutations will be scored as follows:

§ 0 if your permutation's score is lower than the marker's permutation score
§ 1 if your permutation's score is greater than, or equal to, the marker's permutation score

Attachment:- Game.rar

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92294253

Have any Question?


Related Questions in Computer Engineering

Could you help me to solve the following stats problemthe

Could you help me to solve the following stats problem? The number of patients waiting for flu vaccine at A hospital has the following probability distributions. x 1 2 3 4 p(x) 0.2 0.3 0.4 0.1 What is the variance of num ...

Question a student has a first name fname a last name lname

Question : A student has a first name (fname), a last name (lname), an identification number (id), a gender (male/female), a course name (cname) and the number of lectures (lcount) attended by the student. a. Define gend ...

What is vsftpd name two other server packages that may be

What is vsftpd? Name two other server packages that may be used for the same purpose

The task is to implement closest pair algorithm along with

The task is to implement closest pair algorithm. Along with the implementation in c++, I want two additional things. Thing 1. Test the correctness of your algorithm in the following way: Generate 100 random points in the ...

What strategies might help redirect a disruptive student

What strategies might help redirect a disruptive student? What strategies might help an unmotivated student?

What are some topics that must be covered in a business

What are some topics that must be covered in a business case presented to management?

Question a add a navigation form to your applicationa

Question: A) Add a Navigation form to your application. A navigation form makes it easy for users to switch between the various forms and reports in your database. Navigation forms are a great addition to any desktop dat ...

Question suppose you are building a java application for

Question : Suppose you are building a Java application for storing the names and ages of children in a family. Describe the strategy that you would use in order to determine the data types you would need for your applica ...

Question suppose a process in host c has a udp socket with

Question : Suppose a process in Host C has a UDP socket with port number 6789. Suppose both Host A and Host B each send a UDP segment to Host C with destination port number 6789. Will both of these segments be directed t ...

Refer to the reading e-business strategy how to benefit

Refer to the reading, "E-Business Strategy: How to Benefit From a Hype" and review its alignment between such models as SWOT and Five Forces and the e-business that it uses as a model. In your posting, address the follow ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As