JavaScript, Lesson 5 - Homework Questions

Q1

Write a program that creates an array with four rows and four columns, and then asks the user to enter values for all the elements. The program should then "rotate" the array clockwise, as shown in this example:

4 11 6 3
7 2 4 0
3 1 8 0
9 6 8 1
9 3 7 4
6 1 2 11
8 8 4 6
1 0 0 3

Q2

Twelve children (Jim, Sandra, Freddie, Richard, Diane, Mary, Natalie, Jeremy, Henry, William, Sarah and Charles) are to sit at desks in a rectangular arrangement (3 rows of 4 columns of desks). However, William and Jim must sit in the front row (nearest the teacher) because they tend to mess around in class. Sarah must not sit next to Diane as they tend to copy each others' work. Henry must not sit adjacent to (within one space of in any direction) Freddie or Natalie and Sandra and Jim must not be in the same row.

Write a program which asks for a seating plan from the user and checks to make sure it meets those restrictions. The program should then display the seating plan.

Q3

Write a computer program that allows two human players to play noughts-and-crosses (tic-tac-toe) against each other. The program should detect when either player has won, or when the game is a draw. The board position can be displayed in terms of words (as at the moment you probably don't know how to draw the board in terms of pictures yet).

Q4

Create a three-dimensional array which represents a section of the galaxy. Your array should have 10 rows, 10 columns and 10 layers. The star ship Enterprise is at position (1, 3, 4), a Klingon battleship at (7, 9, 7) and stars at (1, 3, 9), (4, 2, 7), (2, 7, 3), (8, 5, 6) and (7, 2, 5).

  1. How would you display this information on the screen (which can only represent two dimensions)?

  2. Add lines to your program which allows the captain of the Enterprise to plot a course to intercept the Klingons, and which informs him if the course will take him through a star.

Q5

A magic square is a square grid full of numbers where each row, each column and each of the two diagonals adds to the same number. For instance, a 3-by-3 magic square containing the numbers 1, 2, 3 etc. up to 9 will have rows, columns and diagonals where the numbers add to give 15. Write a program which allows the user to enter the numbers, and then reports whether the array forms a magic square. More importantly, the user should be able to alter the numbers in the array, with the program reporting the sums of the rows, columns and diagonals, and then print a congratulatory message when the array finally is a magic square.

Q6

A prime number is one which can only be divided by two whole numbers - 1 and itself. Hence 7 is a prime number because the only whole numbers that divide into it are 1 and 7. On the other hand 8 is not a prime number because it also divides by 2 and 4. The first prime number is 2, not 1!

The ancient Greek mathematician Eratosthenes developed this method for finding all the prime numbers up to a certain limit (say 100):

  • You start with an array of numbers, from 2 to 100.

  • You then report the first of those numbers (which is 2) as the first prime number.

  • Then remove all multiples of 2 from the list (i.e. 2 itself, 4, 6, 8 etc. up to 100) by setting those entries in the array to -1 (meaning "this number is defunct!")

  • Then find the first non-defunct number in the array (which will be 3), report it as a prime and remove all its multiples by setting the corresponding elements to -1.

  • Repeat the process until all the numbers in the array are defunct.

Write a program that allows the user to enter an upper limit (it needn't be 100) and then displays all the prime numbers up to that limit using this "Sieve of Eratosthenes".