Results 1 to 12 of 12

Thread: Functions RESOLVED

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23

    Thumbs up 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:
    1. Option Explicit
    2. Private Sub cmdCompute_Click()
    3. Dim n1 As Single, n2 As Single, n3 As Single
    4. Dim avg As Single
    5. Dim GrA As Single, GrB As Single, GrC As Single, GrD As Single
    6. Dim Res As String
    7. Res = "N/A"
    8. n1 = txtGr1.Text
    9. n2 = txtGr2.Text
    10. n3 = txtGr3.Text
    11. Rank n1, n2, n3
    12. lblGr1.Caption = n1
    13. lblGr2.Caption = n2
    14. lblGr3.Caption = n3
    15. avg = (n1 + n2 + n3) / 3
    16. lblRes.Caption = Round(avg)
    17. GrA = (90 - (avg * 0.6)) / 0.4
    18. GrB = (80 - (avg * 0.6)) / 0.4
    19. GrC = (70 - (avg * 0.6)) / 0.4
    20. GrD = (60 - (avg * 0.6)) / 0.4
    21. lblGrA.Caption = FormatNumber(GrA, 1)
    22. lblGrB.Caption = FormatNumber(GrB, 1)
    23. lblGrC.Caption = FormatNumber(GrC, 1)
    24. lblGrD.Caption = FormatNumber(GrD, 1)
    25. If GrA > 100 Then
    26.     lblGrA.Caption = Res
    27. End If
    28.    
    29. End Sub
    30.  
    31. Private Sub Rank(ByRef x As Single, ByRef y As Single, ByRef z As Single)
    32. 'Order scores from low (Gr1) to high (Gr3)
    33.     If x > y Then Swap x, y
    34.     If y > z Then Swap y, z
    35.     If x > y Then Swap x, y
    36. End Sub
    37.  
    38. Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
    39.     Dim temp As Single
    40.     temp = n1
    41.     n1 = n2
    42.     n2 = temp
    43. End Sub
    44.  
    45. Private Function Score(ByVal currAvg As Single, ByVal finalAvg As Single) As String
    46.   Score = (currAvg * 0.6) + (finalAvg * 0.4)
    47.    
    48.        
    49.        
    50.            
    51. End Function
    Last edited by ldyslp4; Nov 4th, 2002 at 06:20 PM.

  2. #2
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    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:
    1. GrA = (90 - (avg * 0.6)) / 0.4
    2. GrB = (80 - (avg * 0.6)) / 0.4
    3. GrC = (70 - (avg * 0.6)) / 0.4
    4. GrD = (60 - (avg * 0.6)) / 0.4
    FW

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23
    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.

  4. #4
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    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?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23
    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%

  6. #6
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    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:
    1. Private Function GetFinalsScoreNeeded(ProjectScore As Single, Grade As String) As String
    2.     Dim sngFinalsScoreNeeded As Single
    3.     Dim sngProjectScore As Single
    4.    
    5.     'convert the project into a mark out of 60
    6.     sngProjectScore = ProjectScore * 0.6
    7.    
    8.     'get the percent needed to get the grade
    9.     Select Case Grade
    10.         Case "A"
    11.             sngFinalsScoreNeeded = 90 - sngProjectScore
    12.         Case "B"
    13.             sngFinalsScoreNeeded = 80 - sngProjectScore
    14.         Case "C"
    15.             sngFinalsScoreNeeded = 70 - sngProjectScore
    16.         Case "D"
    17.             sngFinalsScoreNeeded = 60 - sngProjectScore
    18.     End Select
    19.     'Check if it's more than 100% needed
    20.     If (sngFinalsScoreNeeded / 40) * 100 > 100 Then
    21.         GetFinalsScoreNeeded = "N/A"
    22.     Else
    23.         'returns the percent needed as as string
    24.         GetFinalsScoreNeeded = CStr((sngFinalsScoreNeeded / 40) * 100)
    25.     End If
    26. End Function

    Good luck

    FW

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23
    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:
    1. Option Explicit
    2.  
    3. Private Sub cmdCompute_Click()
    4. Dim n1 As Single, n2 As Single, n3 As Single
    5. Dim avg As Single
    6. Dim Res As Single
    7. n1 = txtGr1.Text
    8. n2 = txtGr2.Text
    9. n3 = txtGr3.Text
    10. Rank n1, n2, n3
    11. lblGr1.Caption = n1
    12. lblGr2.Caption = n2
    13. lblGr3.Caption = n3
    14. avg = (n1 + n2 + n3) / 3
    15. lblRes.Caption = Round(avg)
    16. lblGrA.Caption = Score(avg, Res)
    17. lblGrB.Caption = Score(avg, Res)
    18. lblGrC.Caption = Score(avg, Res)
    19. lblGrD.Caption = Score(avg, Res)
    20.  
    21.  
    22.    
    23. End Sub
    24.  
    25. Private Sub Rank(ByRef x As Single, ByRef y As Single, ByRef z As Single)
    26. 'Order scores from high (Gr1) to low (Gr3)
    27.     If y > x Then Swap x, y
    28.     If z > y Then Swap y, z
    29.     If y > x Then Swap x, y
    30. End Sub
    31.  
    32. Private Sub Swap(ByRef n1 As Single, ByRef n2 As Single)
    33.     Dim temp As Single
    34.     temp = n1
    35.     n1 = n2
    36.     n2 = temp
    37. End Sub
    38.  
    39. Private Function Score(ByVal currAvg As Single, ByVal finalAvg As Single) As String
    40. Dim A As Single, B As Single, C As Single, D As Single
    41. Dim sngcurrAvg As Single
    42. Dim sngFinalScoreNeeded As Single
    43. sngcurrAvg = currAvg * 0.6
    44.  
    45. Select Case finalAvg
    46. Case A
    47.     sngFinalScoreNeeded = (90 - sngcurrAvg) / 0.4
    48. Case B
    49.     sngFinalScoreNeeded = (80 - sngcurrAvg) / 0.4
    50. Case C
    51.     sngFinalScoreNeeded = (70 - sngcurrAvg) / 0.4
    52. Case D
    53.     sngFinalScoreNeeded = (60 - sngcurrAvg) / 0.4
    54. End Select
    55.  
    56. If sngFinalScoreNeeded > 100 Then
    57.     Score = "N/A"
    58. Else
    59.     Score = sngFinalScoreNeeded
    60. End If
    61.          
    62. End Function

  8. #8
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    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:
    1. lblGrA.Caption = Score(avg, Res)
    2. lblGrB.Caption = Score(avg, Res)
    3. lblGrC.Caption = Score(avg, Res)
    4. lblGrD.Caption = Score(avg, Res)
    with
    VB Code:
    1. lblGrA.Caption = Score(avg, "A")
    2. lblGrB.Caption = Score(avg, "B")
    3. lblGrC.Caption = Score(avg, "C")
    4. lblGrD.Caption = Score(avg, "D")
    and change the second parameter of the Score function back to a String.

    FW

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23
    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:
    1. 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.

  10. #10
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    No, the function sets the return of Score with the code
    VB Code:
    1. If sngFinalScoreNeeded > 100 Then
    2.     Score = "N/A"
    3. Else
    4.     Score = sngFinalScoreNeeded
    5. 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:
    1. 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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Maine
    Posts
    23
    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.

  12. #12
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    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:
    1. Private Function Score(ByVal currAvg As Single, Grade As String) As String
    2.  
    3.     Dim sngFinalsScoreNeeded As Single
    4.     Dim sngScoreOurOf60 As Single
    5.    
    6.     'convert the project into a mark out of 60
    7.     sngScoreOurOf60 = currAvg * 0.6
    8.    
    9.     'get the percent needed to get the grade
    10.     Select Case Grade
    11.         Case "A"
    12.             sngFinalsScoreNeeded = 90 - sngScoreOurOf60
    13.         Case "B"
    14.             sngFinalsScoreNeeded = 80 - sngScoreOurOf60
    15.         Case "C"
    16.             sngFinalsScoreNeeded = 70 - sngScoreOurOf60
    17.         Case "D"
    18.             sngFinalsScoreNeeded = 60 - sngScoreOurOf60
    19.     End Select
    20.     'Check if it's more than 100% needed
    21.     If (sngFinalsScoreNeeded / 40) * 100 > 100 Then
    22.         Score = "N/A"
    23.     Else
    24.         'returns the percent needed as as string
    25.         Score = CStr((sngFinalsScoreNeeded / 40) * 100)
    26.     End If
    27. End Function
    The code to call it is
    VB Code:
    1. lblGrA.Caption = Score(avg, "A")
    2. lblGrB.Caption = Score(avg, "B")
    3. lblGrC.Caption = Score(avg, "C")
    4. 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
  •  



Click Here to Expand Forum to Full Width