Programming 2, Summer 2023
Week 2: Exercises

1. Longest String

Design and perform an experiment to determine the size of the longest string that C# will allow you to create.

2. S Begins With T

Write a program that reads two strings S and T, each on its own line. The program should print "true" if S starts with T, "false" otherwise. Do not use any library functions.

3. Printing with Commas

Write a program that reads a single line containing an integer which may be arbitrarily large, and writes the integer with embedded commas.

Input:

28470562348756298345

Output:

28,470,562,348,756,298,345

4. 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?

5. Integer Conversions

What value do you think each of these programs will print?

a)

int k = 1050;
WriteLine((int) (byte) k);

b)

long l = 256_256_256_256;
WriteLine((long) (byte) (int) l);

6. Lots of Casts

Consider these functions:

a)

int a(int i) {
    return (int) (float) i;
}

b)

int b(int i) {
    return (int) (double) i;
}

c)

long c(long l) {
    return (long) (double) l;
}

Which of these functions, if any, do you believe is equal to the identity function, i.e. it always returns the same value it was given? If you're not sure, run experiments to find out the answer.

7. Double Experiment

Consider these questions:

a) A double has an initial value of 1.0. How many times can we divide it by 2 before it becomes 0?

b) A double has an initial value of 1.0. How many times can we multiply it by 2 before it becomes Double.PositiveInfinity?

Guess at the answers to (a) and (b). Then write a problem that performs these experiments and prints out the resulting values.

8. 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

9. 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".

10. Integer Square Root

Write a program that reads an integer n, and computes and prints the integer square root of n, i.e. the non-negative integer i such that i2 = n. If no such integer exists, print "not a square". Do not call any library functions. Your method must run in time O(log N) in the worst case.