?
Solved

A program that accepts the 5 grades given then average it.

Posted on 2003-03-03
7
Medium Priority
?
133 Views
Last Modified: 2010-05-01
I used the following declarations:

Dim num, ave
num = so and so....
num = so and so....

My problem is this:
num = g1 + g2 + g3 + g4 + g5
ave = num/5

If i have inputted 5 same grades equivalent to 1, the output of my program would be 2222.2 instead of 1. Again i f i have inputted 5 same grades equivalent to 2, the output of my program would be 4444.4

Please help me. Thanks
0
Comment
Question by:chatle
7 Comments
 
LVL 3

Expert Comment

by:DeAn
ID: 8057798
is this what you mean?

    Dim num As Single, ave As Single
    Dim g1 As Single, g2 As Single, g3 As Single, g4 As Single, g5 As Single
    g1 = 2
    g2 = 2
    g3 = 2
    g4 = 2
    g5 = 2
   
    num = g1 + g2 + g3 + g4 + g5
    ave = (num / 5) * 2222.2
   
    MsgBox ave
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 400 total points
ID: 8057865
The problem is that you have declared the variables without type and they have therefore defaulted to variant. More specifically you have probably assigned the value of a textbox to each variable thus giving a subtype of string. Using the + operator on these will give you string concatenation rather than arithmetic addition. Your calculation is then correct as it is dividing 11111 by 5 to get 2222.2.

Better to ensure that you are always using numbers.

Dim num as Single
Dim g1 As Single
Dim g2 As Single
Dim g3 As Single
Dim g4 As Single
Dim g5 As Single
Dim ave As Single
g1 = CSng(Text1.Text)
g2 = CSng(Text2.Text)
g3 = CSng(Text3.Text)
g4 = CSng(Text4.Text)
g5 = CSng(Text5.Text)
num = g1 + g2 + g3 + g4 + g5
ave = num /5
Msgbox ave
0
 

Expert Comment

by:legendsfan
ID: 8057996
This sub will allow you to use as many or few grades as you would like.  Now you can figure grades halfway through the semester. Just add more textboxes as when more grades are required

Private Sub Command1_Click()
    Dim g1 As String
    Dim g2 As String
    Dim g3 As String
    Dim g4 As String
    Dim g5 As String
    Dim intNumGrades As Integer
    Dim ctl As Control
    Dim dblTotal As Double
   
    dblTotal = 0
    intNumGrades = 0
   
    g1 = Text1.Text
    g2 = Text2.Text
    g3 = Text3.Text
    g4 = Text4.Text
    g5 = Text5.Text
   
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox And ctl <> "" Then
            dblTotal = dblTotal + CInt(ctl.Text)
            intNumGrades = intNumGrades + 1
        End If
    Next
    Label1.Caption = dblTotal / intNumGrades
   
End Sub
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 101

Expert Comment

by:mlmcc
ID: 8058121
Be sure to give credit to you rexperts when you turn your homework in.

mlmcc
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8059300
Another piece of advice:

ALWAYS delcare the TYPE of ALL your variables.  It is a very bad habit to simply use

Dim MyVar

rather than

Dim MyVAr as _________

where the _________ is the name of a SPECIFIC TYPE, like Integer, or Single, or String, or Date.

AW

0
 

Author Comment

by:chatle
ID: 8151220
Dean,

For the effort you've done, i give you 10 points only. you answer is not actually what i meant.
Thanks again.

Chatle
0
 

Author Comment

by:chatle
ID: 8151238
Dear Tim,

Thanks for your answer. Sorry for the delayed of my rating.
You're such a wonderful programmer.


Regards,

Chatle
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question