Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Programming Exercises 8 More Array Examples

This is the last set of exercises for this semester. This means that you will have no new exercises next week, but the practical classes will take place so that you can have these exercises signed-off. I am aware that you have also project 2 to think about, which is another reason for not giving you any more exercises for next week.

Programming Exercise 8.1: Reversing an Array


Write a method which takes as its input an array of integers, and reverses the numbers in the array. E.g. if the numbers in the input array are:
1 11 4 9 9 4 16 7 9 9 7 16 4 9 9 4 11 1

then the output should be: Write the method in two ways:

so that the numbers are moved within a singel array so that a new array is created which contains the numbers in reverse order.

Write a program which calls the two versions of the method to test them for correctness.

Programming Exercise 8.2: Appending Arrays


Write and test a method that has the heading:
public static int[] append (int[] a, int[] b)

in which the resulting array is obtaining by appending the elements of the array to the elements of the array b.

Programming Exercise 8.3: Noughts and Crosses


The game of noughts and crosses is played as follows: The game is played on a 3 by 3 grid by two players who take turns. The first player marks his/her moves with a cross, and the second with a nought. The objective of the game is to create a line of three of your own marks before your opponent does. A line may be vertical, horizontal or diagonal. Write a program to play the game. Each player makes his/her move by entering an integer in the range 1 to 9, where the positions on the grid are as shown in the diagram:
1 | 2 | 3 -----|-----|----4 | 5 | 6 -----|-----|----7 | 8 | 9

Your program should include methods which


Test whether an attempted move is valid (i.e. is the position into which the move is being attempted is in fact free. Test whether a valid move results in the game being won

Draws the grid after each move.

[Note that it is possible for a game to end in a draw, with the grid being filled without either player creating a line.]

Programming Exercise 8.4: Magic Squares


A Magic Square is a square array containing all the numbers from 1 up to n squared (where the array is n by n). The numbers in each row, each column and the two diagonals should all add up to the same number. Write a program which asks the user to enter a number n, followed by n squared numbers which are put into an array. The program should contain a method which tests to see whether the numbers form a magic square. Your program should make sure that the user has indeed entered all of the numbers between 1 and n squared. One possible magic square (which you can use for tesing purposes) is:
16 5 9 4 3 10 6 15 2 11 7 14 13 8 12 1

Programming Exercise 8.5: Creating a Magic Square


The following algorithm will create a magic square for odd values of n:

Place the number 1 in the middle of the bottom row. Each time you place a number, the next number should be placed in the next row down, and one place to the right, wrapping round from the right hand to the left, and from the bottom rowto the top as necessary. If the position is occupied, then place the number in the position immediately above the position where the last number was placed.

The 3 by 3 magic square generated by this method looks like this:


4 3 8 9 5 1 2 7 6

Write a program that implements this algorithm and displays the resulting magic square. Use the method developed in Exercise 8.4 to check that the square produced has the required properties of a magic square. May I remind you yet again that you should document all your programs both with comments, and by use of the README file associated with each exercise.

Edy Hermansyah, S.Si., M.Sc., Ph.D. Department of Informatics Engineering Faculty

The University of Bengkulu, Nov 2011

You might also like