Lab 10. Arrays
Write an application that draws a graph of the rainfall by month for a
particular city.
You will use an array of integers to hold each month's rainfall in inches.
For this exercise, we will use the Rnd() function to fill the array with
integers between 0 and 10.
You will need the following controls to complete today's lab:
- A picture box to output the graph. The example
has a size of 240 by 125 and a white (web colors) background.
- A label to output the total yearly rainfall and
average monthly rainfall.
- A button to fill the array with random integers
between 0 and 10 inclusive.
- A button to draw the graph.
- A button to calculate the total and average.
You will need at minimum the following variables to complete
today's lab:
- An array of 12 Integer
variables to hold each month's rainfall in inches.
- An Integer variable to store the total rainfall
for the year.
- An Integer variable to store the max value of
the array.
- A SolidBrush object that you keep changing its
color property or 3 SolidBrush objects (one
blue, one black, and one
red).
- A Font object used to write each month's
rainfall amount in the graph.
Button click event algorithms
Get Rainfall
This button fills the array with random integers between 0 and 10
inclusive.
For 0 to UBound of array
fill element with random integer: Int(Rnd() * 11)
Next
Graph
This button draws a bar graph based on the values in the array. You should
draw a blue rectangle for each month except the
month(s) with the highest amount of rainfall. The month(s) with the highest
amount of rainfall should have a red rectangle as
seen in the example above.
get max value of array (use a max function that passes in the array and returns the max value).
For 0 to Ubound of array
If element equals max Then
fill red rectangle (see below)
Else
fill blue rectangle (see below)
End If
Draw black outline of rectangle
Draw number of inches above rectangle(use CreateGraphics.DrawString function)
Next
Print Stats
This just outputs the total inches for the year and the average monthly
rainfall to the label.
get total number of inches for year (use a total function that passes in the array and returns the total of all values in the array).
output to the label the total and then a newline (vbNewLine)
output to the label the average (total / array.Length)
How to draw/fill rectangles using a For loop
A rectangle needs a SolidBrush, x coordinate, y coordinate, width, and heigth.
- x coordinate: Since my picture box is 240 units wide, I made each month 20 units wide. So, you need to set this x coordinate equal to the index value * 20.
- y coordinate: Since my picture box is 125 units high and it is 0 at the top, you could make the y coordinate equal to 125 - # of inches for that month. Now, since that makes a rather flat graph, I actually multiplied the # of inches by 4. So the y coordinate equals 125 - (4 * # of inches).
- width: The width as stated earlier is just 20 units (240 / 12).
- heigth: The heigth needs to go from the y coordinate to the bottom (125), so that would be 4 * # of inches.
How to draw the # of inches above each bar
Use the DrawString function. The DrawString function needs a String output, a Font object, a SolidBrush object, an x coordinate, and a y coordinate.
- String: This is the # of inches for that month.
- Font: Here is how I declared my Font Object:
Dim myFont As New Font("arial", 8, FontStyle.Bold)
- SolidBrush: Make it black to have black numbers.
- x coordinate: The same as the x coordinate for the rectangle it is above, but I added 2 units to center it more (index * 20 + 2)
- y coordinate: Similar to the x coordinate, you need to make this close to the top of the rectangle for this month. I used the same formula but added 13 units (125 - (4 * # of inches) + 13) or you could just put (112 - (4 * # of inches)).