# 4 – Arrays (Level 3)

## 2 Exercises

1. Print out the elements of the following array: int [] numbers = {1,2,3,4,5};
2. Declare and initialize a String array containing the days of the week. Print out a random day.
3. Output the sum of the elements in this array: int [] values = {3,5,4,7,2,3};
4. Output the average of the elements in this array: int [] values = {3,4,5,6};
5. Declare an int array of length 5. Use a for loop to prompt for 5 numbers and enter them into the array. Print out the array.
6. Declare an int array of length 4. Use a for loop to prompt for 4 numbers and enter them into the array. Print out the average of the four numbers
7. The following code will convert a String s to an array of characters c. Print out the characters: String s = “This is a string”; char [] c = s.toCharArray();
8. Use what you learned in the last question to count the number of times the letter e occurs in the String “I never saw a purple cow”

# British Informatics Olympiad: Breaking Down Problems

Here’s an old BIO question

Goldbach Conjecture

A prime number is a whole number, greater than 1, that can only be divided by itself and the number 1.  It is known that all even numbers between 4 and 300,000,000,000,000,000 are equal to the sum of two primes (a fact that is believed to be true for all larger even numbers as well, and called the Goldbach Conjecture).
For example, 30 = 7 + 23.  There are two other ways of expressing 30 as the sum of two primes, which are 11 + 19 and 13 + 17.  These are the only ways of expressing 30 as the sum of two primes, since the order of the numbers in the additions does not matter.

1(a) [ 25 marks ]

Write a program which inputs a single even number (between 4 and 10,000 inclusive) and outputs a single number which is the number of different ways the input can be expressed as the sum of two primes.

Sample run
22
3

This problem can look quite daunting at first.  Don’t let that put you off.  All the questions in the BIO are there to test you as a programmer, there will be a way to solve them.  As a matter of fact, this is a classic programming problem, one that I included as one of my 99 Java Problems

So let’s break the problem down

The Goldbach problem consists of three separate programming problems:

1. Testing if a number is prime
2. Looping over the possible pairs of numbers
3. Testing if the Goldbach Conjecture holds true for each pair of numbers in the loop

Solving the Goldbach problem requires solving these three problems

The first problem, writing a method to check if a number is prime, is a classic programming problem. Many solutions can be found online.

The second problem, thinking of all the possible pairs of numbers that need to be tested, is more interesting.  At this time it’s worth using the mathematicians trick of thinking about simple cases.   Suppose you were testing the number 10.  Which pairs of numbers add up to 10?  1+9, 2+8, 3+7, 4+ 6, 5+5.  Is that it?

What about the number 8? 12?  Does this help you to write a loop listing all the pairs of numbers adding up to 10?  Now think of the general case.

Now combine 1 & 2 to get your final solution.

Is there a more elegant way of solving the problem?  In terms of the BIO, it doesn’t matter.  As a programmer, though, you’ll always be thinking about ways to make your code more efficient.

What happens if you’ve got all the bits working but you can’t combine them?  The bad news is that you’re not going to gain very many marks.  But the good news is that you’ve had a go, and so you’re just a little bit better at coding than when you went into the exam…

# 1 Input and Output (Level 1)

## Sample Code

### Escape Characters

 Escape Sequence Character \n newline \t tab \b backspace \” double quote \’ single quote \\ backslash \uDDDD Unicode character
public class uni
{
public static void main (String args [])
{
System.out.println("\u0041");
}
}


### Simple Scanner

import java.util.Scanner;
public class Simpscan
{
public static void main (String args [])
{
Scanner scan = new Scanner(System.in);
String s = scan.next();
System.out.println("Hello " + s);
}
}


### System.out.format

double pi = 3.1415;
System.out.format("Pi is %f to 4 d.p.%n", pi);


#+RESULTS

Pi is 3.141500 to 4 d.p.


## Exercise

1. Use the \t escape character to print out a noughts and crosses grid, as shown below in fig. 1
2. Prompt the user to enter their (name). Print out “Hello” (name) “I hope you’re well”
3. Use Math.sqrt() to print out the square root of 20
4. Use Math.sqrt() to print out the square root of 20 to 2 decimal places
5. Use Math.random() to print out a random integer between 5 and 10
6. Use Math.pow() to print out 2 to the power of 8
7. Prompt the user to enter a (number). Print out “The square root of ” (number) ” is ” (answer)
8. Prompt the user to enter two numbers. Print out the average of those numbers.
9. To work out your BMI, divide your weight in kilograms by your height in metres squared. In other words BMI = w / h*h. Write a program that prompts the user to input their weight and height, and then outputs their BMI.
 o x x o o x o

# Why does the UK Government say I have to buy a Microsoft System?

In the UK, you need a DBS certificate for certain jobs or voluntary work, e.g. working with children or in healthcare.  Most people don’t think of it as another tax , but it is, and one that disproportionately affects the low paid.  It wasn’t until I tried to renew this certificate for a friend on my open source Linux system that I realised there was another tax.  Apparently you also need to buy a Microsoft License:

I’m looking forward to the time when you have to use Apple Pay, that way I can subsidise two US corporations at the same time.

# 5 Common Linux Misconceptions

## 1. The Linux Filesystem Hierarchy is a logical, sensible structure

It’s not. It’s something that has been cobbled together in an attempt to unify diverging practices. In an age of terabyte hard drives its easy to forget a time when you were limited to just a couple of floppy drives for storage and had to store programs across different locations.

That’s why program files today are stored in /usr and /opt. And /usr/local. And /usr/bin and /usr/sbin. Oh and /bin and /sbin. When you can’t store everything in one place, you have to make (sometimes artificial) distinctions between where things go. Remember a time when you used to boot up a computer with one floppy, then put in a second floppy with an application program, then a third floppy with data files?

Someone designing a directory structure in a world where terabyte HDDs are the norm might be persuaded it was a good idea to keep bootup files separate in /bin. But /usr/sbin? /opt? I don’t think so.

Have a look at Gobolinux <http://www.gobolinux.org/> As they say

GoboLinux is an alternative Linux distribution which redefines the entire filesystem hierarchy.

If you want to know what goes where, there’s a nice overview of the Linux Directory Structure here: <http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html>

## 2. Bash is just like the Windows shell, except with different commands

Here’s a simple test. How do you use the ls command to list directories only?

Checking the man page, it’s obviously ls -d, right?

Wrong.

Do a Google search on linux list directories only and you’ll find a lot of people making the same mistake. It all comes down to failing to realise that its Bash that’s expanding the * wildcard, and not the ls command itself.

If you’re going to use Bash, you’re going to have to take a little time to read up on how it works. This is a great site: <http://linuxcommand.org/>

(My preferred method is ls -d */, btw.)

## 3. The .exe files are in there somewhere

It’s easy to accept that file extensions aren’t necessary in Linux, but it’s hard to shake the idea that something like exe files are still lurking in there somewhere, albeit under different names.

Not really. Linux is far, far more modular than Windows. This is down to the philosophy on which the system is built: programs tools chain their input and output to produce results. A typical Windows application is a flat pack wardrobe, something that solves one problem. A typical Linux application has been formed from many parts using the equivalent of saws, hammers, screwdrivers and chisels.

As an example, a GUI based program that would be a single exe file in Windows would typically be a graphic interface to command line utility in Linux. In terms of user experience, there should be no difference, but behind the scenes, things are very different.

## 4. Linux is faster/uses less resources than Windows

That may have been true back in the Windows Vista Bloatware days, and it may be true for lightweight distros like Puppy Linux <http://puppylinux.org/> and Damn Small Linux <http://www.damnsmalllinux.org/>, but its not so true now.

Windows 8 may have the worst user interface ever devised, but it installs quickly, loads quickly and runs quickly, matching if not beating systems like Ubuntu 14.04 in benchmark tests.

Why mention Ubuntu 14.04? Well, that leads onto the next point…

## 5. It doesn’t matter what it looks like, so long as it works

It does.

It really, really matters.

You might do everything via the command line. You might think that Openbox is all you need as far as desktop management goes, but you’re in a minority.

If you’re the only person using your computer, then fine, but if your less technologically inclined partner, friends, parents or children are relying on you to provide the IT then all of a sudden looks really matter.

Of course they does. In a world slowly being taken over by Apple this is so obvious it shouldn’t need saying.

The command line is fantastic, lxde is a miracle of economy, damn small linux is damn fine, but they’re not what most people regard as a solution. What most people want is something that looks good and is so easy to use they don’t even have to think about it. And there’s nothing wrong with that. I don’t care how my shirts are made as long as they look smart and feel comfortable. Most people feel the same way about their computer.

# Welcome

This blog’s tagline is adapted from the Emacs Org-Mode motto. It seemed appropriate, as I seem to have spent most of my life writing novels and short stories (of which you can find out more at www.tonyballantyne.com) or teaching computer coding.

I’ve amassed a lot of material over the years, and I wanted to share it with people who may not have had the same access to education as people living in my country are lucky enough to have. If you want to change the world, become a teacher.

As the the teaching of coding seems to be coming back into fashion, I’ve also included my thoughts on the pedagogy of this subject.

# 6 GTD: Reviewing

Now that you’ve got yourself organised, you need to remind yourself what the jobs are that you need to do.

Review appropriate lists at appropriate times. For example

– Check Calendar at the start of the day
– Look at General Tasks to be done
– When on the phone, look for tasks marked phone
– When in a meeting, look at the tasks for that meeting
Review your whole system once a week.
– Check Calendar for forthcoming events
– Check projects have a Next Action
– Check Next Actions are being performed
– Check Wait list to see if anything needs chasing
– Check Maybe list to see if anything is ready to proceed
– Pause or drop projects that aren’t going ahead

If you’re not up to date at the weekly review you won’t be able to fool yourself that your system is remembering for you. You’ll go back to worrying that you’ve forgotten something.