Visual Basic 6.0 Guide
Variables - Programming Tasks

Calculating Average Exam Marks

form design

The user will type an exam mark into the text box provided. When they click OK, the mark entered will be added to a running total and also be added to the listbox on the right. When the Show Mean button is pressed, the mean average of the exam marks will be calculated and displayed in the textbox shown.

Step 1 - Designing The Form

Design your form like the one above. Pay attention to the way that you name objects.

Step 2 - Setting Up Global Variables

You will need to declare 2 variables in the General section of the code window. Both variables will be integers. One variable will be used to store a running total of the marks entered. The other variable will record how many variables have been entered. We will use the information stored in these two variables to calculate the mean of the marks.

Step 3 - Initialising The Variables.

In the form's load event, make the 2 variables equal to 0.

Step 4 - Program The OK Button

Double click the OK button to bring up the event procedure template. You will need to write lines of code to do the following.

1. Declare an integer variable to store the number that has just been entered
2. Make that variable equal to the number entered in the text box
3. Put that number to the listbox on the right
4. Add the number to the global variable for the running total of marks eg intRunningTotal = intRunningTotal + intNum
5. Add 1 to the global variable storing the number of marks entered eg intNumEntered = intNumEntered + 1
6. Clear the text box by setting its text property to "".
7. Set the focus (cursor position) to the text box eg txtExamMark.setFocus

Step 5 - Program The Show Mean Button

Double click on the button to bring up the event procedure template. You will need to write lines of code to do the following.

1. Declare a variable of the type single to store the mean
2. Make that variable the mean by setting it to the total of the marks divided by the number of marks entered.
3. Set the text property of the mean mark box to the mean.

Step 6 - Test, Debug, Print, Annotate

Test your program by putting in values that are easy for you to check in your head (eg the mean average of 3, 4, 5 is 4). Take a screen shot of your working program and paste it into a Word Document. Copy all of your code and paste it into Word. Print out this document and annotate each line of code to explain what it does.