|
-
Nov 3rd, 2002, 07:39 PM
#1
Thread Starter
Junior Member
Functions RESOLVED
I seem to think that this program might be a little off the mark when it comes to doing a function properly. Can anyone look at the code I have produced and see. It is supposed to take in 3 grade scores, display them from smallest to largest and tell you what the grade average is, then it is supposed to tell you what grade is required to get a (60=D,70=C,80=B,90=A) and if the grade is above 100 N/A will be displayed. Mine works but I was afraid I was missing the mark with the function since I really don't have any code under the function part.
VB Code:
Option Explicit
Private Sub cmdCompute_Click()
Dim n1 As Single, n2 As Single, n3 As Single
Dim avg As Single
Dim GrA As Single, GrB As Single, GrC As Single, GrD As Single
Dim Res As String
Res = "N/A"
n1 = txtGr1.Text
n2 = txtGr2.Text
n3 = txtGr3.Text
Rank n1, n2, n3
lblGr1.Caption = n1
lblGr2.Caption = n2
lblGr3.Caption = n3
avg = (n1 + n2 + n3) / 3
lblRes.Caption = Round(avg)
GrA = (90 - (avg * 0.6)) / 0.4
GrB = (80 - (avg * 0.6)) / 0.4
GrC = (70 - (avg * 0.6)) / 0.4
GrD = (60 - (avg * 0.6)) / 0.4
lblGrA.Caption = FormatNumber(GrA, 1)
lblGrB.Caption = FormatNumber(GrB, 1)
lblGrC.Caption = FormatNumber(GrC, 1)
lblGrD.Caption = FormatNumber(GrD, 1)
If GrA > 100 Then
lblGrA.Caption = Res
End If
End Sub
Private Sub Rank(ByRef x As Single, ByRef y As Single, ByRef z As Single)
'Order scores from low (Gr1) to high (Gr3)
If x > y Then Swap x, y
If y > z Then Swap y, z
If x > y Then Swap x, y
End Sub
Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
Dim temp As Single
temp = n1
n1 = n2
n2 = temp
End Sub
Private Function Score(ByVal currAvg As Single, ByVal finalAvg As Single) As String
Score = (currAvg * 0.6) + (finalAvg * 0.4)
End Function
Last edited by ldyslp4; Nov 4th, 2002 at 06:20 PM.
-
Nov 3rd, 2002, 08:47 PM
#2
Hyperactive Member
How does the avg relate to the grade they need to get an A, B, C, or D?. Sorry I'm from Aus, we may do things differently here.
I'm trying to work out what this code is supposed to do
VB Code:
GrA = (90 - (avg * 0.6)) / 0.4
GrB = (80 - (avg * 0.6)) / 0.4
GrC = (70 - (avg * 0.6)) / 0.4
GrD = (60 - (avg * 0.6)) / 0.4
FW
-
Nov 3rd, 2002, 09:06 PM
#3
Thread Starter
Junior Member
Sorry I guess I left the most important out. To figure out the grade you need on the final the current average is 60% of the grade and the final is 40% of the grade. So that was the nearest formula I could figure out with all the options figured in to. If I could have just used one grade it would of made life a whole lot easier. I thought I would need to figure out a way for the program to know the options of either getting an A - D.
-
Nov 3rd, 2002, 09:15 PM
#4
Hyperactive Member
So the grade they got in this test acounts for 60% of the term grade. You wanrt to know what they need to get in the final to achieve an A, B, C, D?
Is that right?
-
Nov 3rd, 2002, 09:20 PM
#5
Thread Starter
Junior Member
Yes that is exactly right. My mind wandered for 2 days trying to figure out how to put that down in a program. The last test accounts for 40%
-
Nov 3rd, 2002, 11:45 PM
#6
Hyperactive Member
Here is a function that returns the score needed in the finals as a percentage.
It assumes you are entering the first score (perhaps the mark for the project) as a percentage.
What the function does is turn that percentage into a score out of 60. It then subtracts this score from the grade levels, and this gives you a mark you must get out of 40. It then turns this mark out of 40 back into a percent.
To use it, call the function for each score once for each target grade. That will mean a total of 12 calls to it.
eg textStudent1ScoreNeededForAnA.Text = GetFinalsScoreNeeded (TheMarkTheyGot, "A")
VB Code:
Private Function GetFinalsScoreNeeded(ProjectScore As Single, Grade As String) As String
Dim sngFinalsScoreNeeded As Single
Dim sngProjectScore As Single
'convert the project into a mark out of 60
sngProjectScore = ProjectScore * 0.6
'get the percent needed to get the grade
Select Case Grade
Case "A"
sngFinalsScoreNeeded = 90 - sngProjectScore
Case "B"
sngFinalsScoreNeeded = 80 - sngProjectScore
Case "C"
sngFinalsScoreNeeded = 70 - sngProjectScore
Case "D"
sngFinalsScoreNeeded = 60 - sngProjectScore
End Select
'Check if it's more than 100% needed
If (sngFinalsScoreNeeded / 40) * 100 > 100 Then
GetFinalsScoreNeeded = "N/A"
Else
'returns the percent needed as as string
GetFinalsScoreNeeded = CStr((sngFinalsScoreNeeded / 40) * 100)
End If
End Function
Good luck
FW
-
Nov 4th, 2002, 01:22 PM
#7
Thread Starter
Junior Member
freewilly I took what you gave me and revamped it to do what I needed. I now have a problem where the scores show up in my label boxes but they are the same score throughout. Where it should be showing me individual scores for each grade. The function is supposed to return the string N/A only when greater than 100. If it is greater than 100 for an A it will give me N/A on all 4 label boxes. It should be given me the final score needed in the other three boxes don't you think. Here is the revamped code.
VB Code:
Option Explicit
Private Sub cmdCompute_Click()
Dim n1 As Single, n2 As Single, n3 As Single
Dim avg As Single
Dim Res As Single
n1 = txtGr1.Text
n2 = txtGr2.Text
n3 = txtGr3.Text
Rank n1, n2, n3
lblGr1.Caption = n1
lblGr2.Caption = n2
lblGr3.Caption = n3
avg = (n1 + n2 + n3) / 3
lblRes.Caption = Round(avg)
lblGrA.Caption = Score(avg, Res)
lblGrB.Caption = Score(avg, Res)
lblGrC.Caption = Score(avg, Res)
lblGrD.Caption = Score(avg, Res)
End Sub
Private Sub Rank(ByRef x As Single, ByRef y As Single, ByRef z As Single)
'Order scores from high (Gr1) to low (Gr3)
If y > x Then Swap x, y
If z > y Then Swap y, z
If y > x Then Swap x, y
End Sub
Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
Dim temp As Single
temp = n1
n1 = n2
n2 = temp
End Sub
Private Function Score(ByVal currAvg As Single, ByVal finalAvg As Single) As String
Dim A As Single, B As Single, C As Single, D As Single
Dim sngcurrAvg As Single
Dim sngFinalScoreNeeded As Single
sngcurrAvg = currAvg * 0.6
Select Case finalAvg
Case A
sngFinalScoreNeeded = (90 - sngcurrAvg) / 0.4
Case B
sngFinalScoreNeeded = (80 - sngcurrAvg) / 0.4
Case C
sngFinalScoreNeeded = (70 - sngcurrAvg) / 0.4
Case D
sngFinalScoreNeeded = (60 - sngcurrAvg) / 0.4
End Select
If sngFinalScoreNeeded > 100 Then
Score = "N/A"
Else
Score = sngFinalScoreNeeded
End If
End Function
-
Nov 4th, 2002, 05:21 PM
#8
Hyperactive Member
ldyslp4
Why are you pasing res which is a single?
In your code the Select Case choses a case based on finalAvg
I don't see how this can work.
Try replacing
VB Code:
lblGrA.Caption = Score(avg, Res)
lblGrB.Caption = Score(avg, Res)
lblGrC.Caption = Score(avg, Res)
lblGrD.Caption = Score(avg, Res)
with
VB Code:
lblGrA.Caption = Score(avg, "A")
lblGrB.Caption = Score(avg, "B")
lblGrC.Caption = Score(avg, "C")
lblGrD.Caption = Score(avg, "D")
and change the second parameter of the Score function back to a String.
FW
-
Nov 4th, 2002, 05:54 PM
#9
Thread Starter
Junior Member
The reason I was doing that is because I have to have four different scores in my label boxes. If I use the string method won't I just be getting grades (A, B, C, D) in my boxes?
Never mind I just tried what you said and it gives me exactly what you said. Thanks alot. By the way I was writing that code that way because that is what we was given to use
VB Code:
Private Function Score (ByVal currAvg As Single, ByVal finalAvg As Single) As String
Then it went on to say to use FormatNumber to pass a value and return a string.
But at this point and time I really could care less as long as mine will do what it is required to do.
Last edited by ldyslp4; Nov 4th, 2002 at 06:00 PM.
-
Nov 4th, 2002, 06:10 PM
#10
Hyperactive Member
No, the function sets the return of Score with the code
VB Code:
If sngFinalScoreNeeded > 100 Then
Score = "N/A"
Else
Score = sngFinalScoreNeeded
End If
What this will do is convert sngFinalScoreNeeded to a string and return it to cmdCompute_Click.
The function name Score acts like a variable of the return type, in this case a string. It takes its value from the code above.
When you call
VB Code:
lblGrA.Caption = Score(avg, Res)
it places the Value that was assigned to Score, in the If statement above, into lblGrA.Caption.
How well do you understand select cases?
The case statement looks at a value and makes a decision base on you logic.
Select Case strGradeRequired
Case "A"
'code to generate the grade needed to get an A
etc
-
Nov 4th, 2002, 06:18 PM
#11
Thread Starter
Junior Member
I thought I was understanding the select case pretty well but when she gave us that statement to use for function it threw me for a loop. I was trying every way possible to make it work. In fact I started with the select case in the beginning but couldn't possibly understand how to make a formula that would work for each individual one. It seemed very redundant. I am just a beginner in this subject. When I try to do what is asked and it only works the wrong way then I get frustrated. I knew my function was supposed to contain the calculations so that it could get passed a value and then send it on to cmd click.
Thanks alot for your assistance. You have been great.
-
Nov 4th, 2002, 06:48 PM
#12
Hyperactive Member
I didn't quite have the problem clear in my mind, but i do now.
Here's how I think your code should look.
VB Code:
Private Function Score(ByVal currAvg As Single, Grade As String) As String
Dim sngFinalsScoreNeeded As Single
Dim sngScoreOurOf60 As Single
'convert the project into a mark out of 60
sngScoreOurOf60 = currAvg * 0.6
'get the percent needed to get the grade
Select Case Grade
Case "A"
sngFinalsScoreNeeded = 90 - sngScoreOurOf60
Case "B"
sngFinalsScoreNeeded = 80 - sngScoreOurOf60
Case "C"
sngFinalsScoreNeeded = 70 - sngScoreOurOf60
Case "D"
sngFinalsScoreNeeded = 60 - sngScoreOurOf60
End Select
'Check if it's more than 100% needed
If (sngFinalsScoreNeeded / 40) * 100 > 100 Then
Score = "N/A"
Else
'returns the percent needed as as string
Score = CStr((sngFinalsScoreNeeded / 40) * 100)
End If
End Function
The code to call it is
VB Code:
lblGrA.Caption = Score(avg, "A")
lblGrB.Caption = Score(avg, "B")
lblGrC.Caption = Score(avg, "C")
lblGrD.Caption = Score(avg, "E")
I have tested this and it works
You need to learn about debuging as well. If you click on the grey section to the left of the code a red dot will appear. When the code gets to the dot it will stop and display it to you. You can then step through the code by pressing F8. You can see the values of most variables by hovering over them with the mouse. You can do heaps more than this when debugging but this will get you started.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|