Homework 5 Solution

$29.99 $18.99

Write a recursive function that takes a positive integer parameter n, and prints the squares of the integers from 1 to n, separated by commas. It should print the squares of the odd integers in descending order first and then following with the squares of the even integers in ascending order. It does not print…

You’ll get a: . zip file solution

 

 
Categorys:
Tags:

Description

Rate this product
  1. Write a recursive function that takes a positive integer parameter n, and prints the squares of the integers from 1 to n, separated by commas. It should print the squares of the odd integers in descending order first and then following with the squares of the even integers in ascending order. It does not print a newline character.

For example,

On input 4, it should print 9, 1, 4, 16

On input 1, it should print 1

On input 7, it should print 49, 25, 9, 1, 4, 16, 36

  1. Consider a puzzle Jump that consists of a row of positive integers, except the rightmost number, which is always a zero. A peg is placed on the leftmost number. At each step, you may move the peg either left or right a distance equal to the number under the peg, but not off either end of the row of numbers. The goal is to move to the rightmost number. For example,

_

Start: 3 2 3 1 6 2 5 1 0

_

1st: 3 2 3 1 6 2 5 1 0 move right 3

_

2nd: 3 2 3 1 6 2 5 1 0 move left 1

_

3rd: 3 2 3 1 6 2 5 1 0 move right 3

_

4th: 3 2 3 1 6 2 5 1 0 move right 2

_

last: 3 2 3 1 6 2 5 1 0 move right 1

There may be more that one solution. But not all puzzles are solvable. In the following example, you jump back and forth between the two 3s and you never reach the other numbers.

_

Start: 3 2 1 3 0

Write a recursive function that has two parameters, an array of integers and an integer for the starting index. The method should return true if the puzzle can be solved, and false otherwise. You may assume that the integers in the array are positive, except the rightmost one, which is zero, and the start index is not out of bounds.

To avoid repeated looping among 2 or more positions, when you move to a position, change the number at that position to an invalid number (such as – 1) so you can recognize that you have been at that position before. For example, in the unsolvable puzzle above, you would start at position 0 and change its value to – 1, move to position 3 and change its value to -1, try to move to position 6 (non existing) and then try position 0. Since position 0 has a – 1, you have visited it before and the path traveled so far will not lead you to a solution (see below) .

_

Start:

3

2

1

3

0

1st:

-1

2

1

_

0

move to the right 3

3

2nd:

_

2

1

-1

0

move to the left 3

-1