Week 2: Exercises

1. Rotate a Matrix

Write a program that reads an N x N matrix of integers and prints it out rotated 90 degrees to the right.

Input:

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

Output:

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

2. Zombies

Some zombies are chasing a player. Here is a top-down view:

Image credit: © Copyright 2014-2020, Juan Linietsky, Ariel Manzur and the Godot community (CC-BY 3.0)

Each zombie can see infinitely far in a 180° field of view as indicated by the light blue half-circular shapes above. In the picture above, zombie A can see the player, but zombie B cannot.

Write a program that reads, on three lines:

Each position or vector is a pair of floating-point numbers. The program should print "can see" if the zombie can see the player, otherwise "cannot see".

3. Sorting Two Values

Write a program that reads a sequence of integers on a single line. The sequence is guaranteed to contain at most two distinct values. For example, it might be

  17 17 17 11 17 11 11 11 17 11 17 11 11

The program should print out the sequence in sorted order:

  11 11 11 11 11 11 11 17 17 17 17 17 17 

How efficient is your program?

4. Int Experiment

We know that in C# the 'int' type represents a signed 32-bit integer.

a) Pretend that you know that 'int' is signed, but don't know how many bits it has. Write a program that will determine the size of 'int' experimentally, and will write output such as

    32 bits

b) Modify and rerun your program to determine the size of the C# type 'long'.

5. Largest Product in a Series

Solve Project Euler's problem 8, assuming that the input number is read from standard input.

6. Largest Product in a Grid

Solve Project Euler's problem 11, assuming that the input number is read from standard input.

7. Large Sum

Solve Project Euler's problem 13, assuming that the input numbers are read from standard input.

8. Power Digit Sum

(Project Euler, problem 16)

Write a program that determines the sum of the digits of the number 21000.

9. Counting Sundays

Solve Project Euler's Problem 19.

10. Number Spiral Diagonals

Solve Project Euler's problem 28.