Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 10-15-2006, 12:42 AM   #1
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
A Visual BASIC Average Tester--> Problems

Yeah when I was coding a little bit today as a test to see if I can get it right on my own, I made a Average maker. Basically, it would take 3 test grades in Text boxes, add them, and divide them by using a Calculate Command. Then the user could clear these values out and re-enter them or just end the program.


Well, here's the problem, every time I make numbers higher than 50, it comes out to be more than 100%!

EX: (100,200, 250) = 183%!

Anyway, I kinda cheated by just adding a " % " symbol to the final answer. How would I make the final number aka Average into a Percent from 0-100? I forgot what Property I would have to change so please help....


Here's the code:
'Average Maker

'Calculate Button
Private Sub cmdCalculate_Click()
Dim FirstGrade As Integer
Dim SecondGrade As Integer
Dim ThirdGrade As Integer
Dim Average As Integer


FirstGrade = Val(txtFirstGrade.Text)
SecondGrade = FormatNumber(txtSecondGrade.Text)
ThirdGrade = FormatNumber(txtThirdGrade.Text)

Average = FirstGrade + SecondGrade + ThirdGrade / 3

lblAvg.Caption = Average & "%"
End Sub

'Clear Button
Private Sub cmdClear_Click()
txtFirstGrade.Text = ""
txtSecondGrade.Text = ""
txtThirdGrade.Text = ""
lblAvg.Caption = ""
txtFirstGrade.SetFocus
End Sub

'End Button
Private Sub cmdEnd_Click()
End
End Sub
Sam-U-Rai is offline   Reply With Quote
Old 10-15-2006, 10:23 AM   #2
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
If you are going to calculate out of 100%, then you're input values either have to be the percent out of 100, or you have to specify what the total possible score it, so you can get their percents.

100 + 200 + 250 = 550 / 3 = 183.3

And use parenthesis around your mathmatic operations (grouping based on order of operations) - other languages can get picky and longer operations in BASIC will look neater if you use parenthesis.
__________________
RiotCats.com, an internet domain specifically fabricated and visually erected for the appreciation of the feline kingdom!
rspassey is offline   Reply With Quote
Old 10-15-2006, 12:16 PM   #3
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
Quote:
Originally Posted by rspassey
If you are going to calculate out of 100%, then you're input values either have to be the percent out of 100, or you have to specify what the total possible score it, so you can get their percents.

100 + 200 + 250 = 550 / 3 = 183.3

And use parenthesis around your mathmatic operations (grouping based on order of operations) - other languages can get picky and longer operations in BASIC will look neater if you use parenthesis.
Is there a FormatPercent property in VB? I forgot. I can't find my notes from school so yeah...

I assigned each inputted grade as a variable as you can see and added them together and divided JUST by 3 grades.

How do I actually get it to make it outta 100 Percent? I mean, this is just a test but I'd like to know
Sam-U-Rai is offline   Reply With Quote
Old 10-15-2006, 12:24 PM   #4
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
To get it out of 100 %, you have to use the percentage calculation....

[(YOUR POINTS) / (TOTAL POSSIBLE POINTS) ] * 100%

for each grade

then add them together and divide by 3 - that will give you average percent

I no longer code in VB and thus have forgotten much of the systax - in JAVA, there is a percent format, but it often truncates too much - try google to see if there is a VB percent format.

**note that if your input grades are already in percents, then you just add them up and divide by 3.

-----
if grade are in percent:
--- (grade1 + grade2 + grade3) / 3

---
if grades are in points:
----[ ( score1 / possible1) + (score2 / possible2) + (score3 / possible3) ] / .03

Always write out the step of your program in pseudocode first; that makes it easier.

Last edited by rspassey; 10-15-2006 at 12:26 PM.
rspassey is offline   Reply With Quote
Old 10-15-2006, 12:25 PM   #5
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
Thanks alot man. That helped. I'm gonna try this right now. hehe, more variables to declare
Sam-U-Rai is offline   Reply With Quote
Old 10-15-2006, 12:27 PM   #6
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
Quote:
Originally Posted by Sam-U-Rai
Thanks alot man. That helped. I'm gonna try this right now. hehe, more variables to declare

And most likely 3 more text boxes for the total possible points on each grade.

Notice I edited my above post to correct the last calculation.. I dived by 3 the first time when I should have been divided by .03
rspassey is offline   Reply With Quote
Old 10-15-2006, 12:30 PM   #7
Wx geek
 
blue60007's Avatar
 
Join Date: Aug 2005
Location: Indiana
Posts: 6,638
I see you have test scores of 100, 200, and 250. How many possible points are those out of?

100 out of 100? 200 out of 264 points?

By adding the 3 together you're just finding the average number of points, not the average percent.

To find the percent on a grade, divide how many points you got by the total possible points. ie, if you got 100 points out of 120 possible, that's 100/120 or a 83%

Average = ((FirstGrade + SecondGrade + ThirdGrade) / (FirstGradeTotalPossible + SecondGradeTotalPossible + ThridGradeTotalPossible)) * 100
lblAvg.Caption = Average & "%"

An example here would be if you got these scores:
100 out of 120 possible
200 out of 250 possible
250 out of 275 possible

Average = ((100 + 200 + 250) / (120 + 250 + 275)) * 100
(solving this down would get you: )
Average = ((550) / (645)) * 100
Average = (550 / 645) * 100
Average ~= 0.85 * 100
Average ~= 85
(then tack on your % sign when change the caption text)

EDIT: Whoops, Ryan beat me to it.

If you wanted to add another layer of complexity, you could just declare two variables, yourPoints and totalPoints, and then keep a running total as the user enters points.

ie,
yourPoints = FormatNumber(txtFirstGrade.Text)
yourPoints = yourPonits + FormatNumber(txtSecondGrade.Text)
yourPoints = yourPonits + FormatNumber(txtThirdGrade.Text)

totalPoints = FormatNumber(txtFirstGradePossible.Text)
totalPoints = totalPonits + FormatNumber(txtSecondGradePossible.Text)
totalPoints = totalPonits + FormatNumber(txtThirdGradePossible.Text)

Average = (yourPoints / totalPoints) * 100

Just another way to do the same thing.
__________________
"It is the way of man to make monsters and it is the nature of monsters to destroy their makers."

Last edited by blue60007; 10-15-2006 at 12:37 PM.
blue60007 is offline   Reply With Quote
Old 10-15-2006, 01:15 PM   #8
Member (9 bit)
 
Sam-U-Rai's Avatar
 
Join Date: Sep 2006
Location: NYC
Posts: 272
I got it to work! hehe. NICE! I added 3 more test scores so now the user can put in 6 test grades in a Quarter so he or she can find out their average.


thnx for the help.


If I did however use Format Number instead of Val function, it always tries to debug it.

EX: if I put in 5xy for the first score, then when I do the calculate button, it would have an error so I just have to deal with it for now.

HERE"S THE CODE SO FAR:

'Average Maker
'10-15-06
'Programmed by Sam-U-Rai

Option Explicit
Dim Average As Integer
Dim TotalPoints As Integer


'Calculate Button
Private Sub cmdCalculate_Click()
Dim FirstGrade As Integer
Dim SecondGrade As Integer
Dim ThirdGrade As Integer
Dim FourthGrade As Integer
Dim FifthGrade As Integer
Dim SixthGrade As Integer

Dim FirstPercent As Integer
Dim SecondPercent As Integer
Dim ThirdPercent As Integer
Dim FourthPercent As Integer
Dim FifthPercent As Integer
Dim SixthPercent As Integer

Dim FinalAnswer As Integer

TotalPoints = 100
FirstGrade = Val(txtFirstGrade.Text)
SecondGrade = Val(txtSecondGrade.Text)
ThirdGrade = Val(txtThirdGrade.Text)
FourthGrade = Val(txtFourthGrade.Text)
FifthGrade = Val(txtFifthGrade.Text)
SixthGrade = Val(txtSixthGrade.Text)

FirstPercent = (FirstGrade / TotalPoints) * 100
SecondPercent = (SecondGrade / TotalPoints) * 100
ThirdPercent = (ThirdGrade / TotalPoints) * 100
FourthPercent = (FourthGrade / TotalPoints) * 100
FifthPercent = (FifthGrade / TotalPoints) * 100
SixthPercent = (SixthGrade / TotalPoints) * 100

Average = (FirstPercent + SecondPercent + ThirdPercent + FourthPercent + FifthPercent + SixthPercent) / 6
FinalAnswer = Average / TotalPoints * 100

lblYourAvgIs.Visible = True
lblAvg.Visible = True
lblAvg.Caption = FinalAnswer & "%"
End Sub
'Clear Button
Private Sub cmdClear_Click()
txtFirstGrade.Text = ""
txtSecondGrade.Text = ""
txtThirdGrade.Text = ""
txtFourthGrade.Text = ""
txtFifthGrade.Text = ""
txtSixthGrade.Text = ""

lblAvg.Caption = ""
lblYourAvgIs.Visible = False
lblAvg.Visible = False
txtFirstGrade.SetFocus
End Sub
'End Button
Private Sub cmdEnd_Click()
End
End Sub

Last edited by Sam-U-Rai; 10-15-2006 at 01:25 PM.
Sam-U-Rai is offline   Reply With Quote
Old 10-15-2006, 02:11 PM   #9
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
On a side note, if you wanted to more efficiently use system resources; you could simply reassign the GRADE values as your precents. Since PCs have so much RAM, and computering power, now, that really isn't an issue, but if this were 15 years ago, things would have been different.
rspassey is offline   Reply With Quote
Old 10-15-2006, 02:35 PM   #10
Come in Ray...
 
faulkner132's Avatar
 
Join Date: Sep 2004
Posts: 1,668
Quote:
Originally Posted by rspassey
On a side note, if you wanted to more efficiently use system resources; you could simply reassign the GRADE values as your precents. Since PCs have so much RAM, and computering power, now, that really isn't an issue, but if this were 15 years ago, things would have been different.
The compiler does things like this for you, no need to worry about it in your code. The most important thing to remember is always make sure your code is readable.
faulkner132 is offline   Reply With Quote
Old 10-15-2006, 03:36 PM   #11
~ Ryan ~
 
Join Date: Jun 2005
Location: Jackson TN
Posts: 3,516
Send a message via AIM to rspassey Send a message via MSN to rspassey
Quote:
Originally Posted by faulkner132
The compiler does things like this for you, no need to worry about it in your code. The most important thing to remember is always make sure your code is readable.

I guess I didn't explain well enough.

I mean instead of using the 6 percent variables, you could just modify the value of each Grade variable... removes 6 variables.

And i agree... code is useless to others without following decent code conventions and commenting.
rspassey is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual BASIC side scrolling dataDude General Discussion 0 07-27-2006 01:54 AM
Visual Basic compatability question waiting Software Discussion & Support 3 11-07-2004 08:45 PM
Microsoft Visual C++ Runtime Library Problems disptchr Windows Legacy Support (XP and earlier) 2 07-06-2004 04:39 PM
Visual Studio .NET - Connecting to Web Server Problems Lain Web Design / Development 0 02-10-2004 01:50 PM
Visual Basic Runtime Error 1004 homer15 Web Design / Development 0 04-22-2002 03:52 PM


All times are GMT -5. The time now is 05:14 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2