Tuesday 26 October 2010

We were given the following program specification.

Write a program that generates 50 random whole numbers between 1 and 50. display them in a list box in reverse order.

First we need to create the main steps. You should ALWAYS do  this for any program specification’s that your given. It makes the task much easier.

Main Steps:
1. Set up variables
2. populate list
3. reverse list
4. display list
5. End program

We have already donesteps 2 and 4 last week. So we did not need the refinements for step 4, but were given the refinements for steps 2 and 3

Ref Step 2.
2.1 For 50 numbers
2.2 create a random number between 1 and 20
2.3 Store this number in the array
2.4 Next Number

The VB code for these steps are:
For Intcounter = 1 to 50
Intnumber = Int(Rnd*20)+1
Intarray(Intcounter) = Intnumber
Next

This code creates 50 random numbers.

Ref Step 3:
3.1 For all 50 numbers.
3.2 Copy item at next position to new array.
3.3 Next

The VB code for these refinements are.

For Intcounter = 1 to 50
Intbackwards(Intcounter) = IntArray(51-Intcounter)
Next

This VB coding takes the random numbers generated  and displays them in reverse order. The part of the code thats in Italics, is the peice of code that does this. If Intcounter = 1, then the position of IntArray thats being displayed first would be (51-1) and would display the 50th position. If the counter was 2, the 49th position would be displayed etc, if it was 3 then position 48 etc.

We used a list box to display these numbers. The reason we used a list box rather than just print it onto the form is that it is easier to read due to the scroll option that the list box provides.