|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (9 bit)
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 |
|
|
|
|
|
#2 |
|
~ Ryan ~
|
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! |
|
|
|
|
|
#3 | |
|
Member (9 bit)
Join Date: Sep 2006
Location: NYC
Posts: 272
|
Quote:
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
|
|
|
|
|
|
|
#4 |
|
~ Ryan ~
|
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. |
|
|
|
|
|
#5 |
|
Member (9 bit)
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
|
|
|
|
|
|
#6 | |
|
~ Ryan ~
|
Quote:
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 |
|
|
|
|
|
|
#7 |
|
Wx geek
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. |
|
|
|
|
|
#8 |
|
Member (9 bit)
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. |
|
|
|
|
|
#9 |
|
~ Ryan ~
|
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.
|
|
|
|
|
|
#10 | |
|
Come in Ray...
Join Date: Sep 2004
Posts: 1,668
|
Quote:
|
|
|
|
|
|
|
#11 | |
|
~ Ryan ~
|
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
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 |