Results 1 to 6 of 6

Thread: Sorry... still need help!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20

    Sorry... still need help!!

    Yeah i asked this question a while ago, and i thought i had the answer but it just didnt work out right. I believ i didnt specify what i needed so dont blame yourselves here it is:

    Create a Reduce Fraction application that takes an integer numerator and denominator of a fraction and then displays the fraction reduced or a message stating the fraction cannot be reduced. A fraction may be reduced by finding the largets common factor and dividing both the numerator and denominator by this factor.

    SO please, if u can just give me a jump i can use!

  2. #2

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I can never remember which is the numerator and which is the denominator. This simple function should get you started.


    VB Code:
    1. Private Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
    2.     Dim lngIdx As Long
    3.    
    4.     For lngIdx = 2 To Numerator
    5.         If Numerator Mod lngIdx = 0 Then
    6.             If Denominator Mod lngIdx = 0 Then
    7.               CommonFactor = lngIdx
    8.             End If
    9.         End If
    10.     Next
    11.  
    12.     If CommonFactor > 0 Then
    13.         Numerator = Numerator / CommonFactor
    14.         Denominator = Denominator / CommonFactor
    15.         GetCommonFactor = True
    16.     End If
    17.        
    18.     Debug.Print Numerator, Denominator, CommonFactor
    19. End Function

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    ok... heres the code im kind of useing right now...

    Private Sub Cmdreduce_Click()
    Dim numer As Integer
    Dim deno As Integer
    Dim gcf As Integer

    numer = txtNumer.Text
    deno = txtDeno.Text
    gcf = deno / numer

    Do While gcf Mod 0
    gcf = gcf - 1
    Loop

    End Sub


    i know theres something wron, so please help me with that!?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    20
    here is what is updated so far... i pretty much have the stuff but i just need to find out one thing:

    [code]

    Dim numer As Integer
    Dim deno As Integer
    Dim gcf As Integer

    numer = txtNumer.Text
    deno = txtDeno.Text
    gcf = numer

    Do While gcf Mod
    gcf = gcf - 1
    Loop

    lblFract.Caption = numer & "/" & deno & " is reduced to " & _
    numer / gcf & "/" & deno / gcf

    [code]

    i need to find out how to make the mod on the do... loop statement be greater then 0.

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Are you having trouble with the function that I wrote or did I misunderstand your problem?

    Here is an updated more efficient version.

    VB Code:
    1. Private Function GetCommonFactor(Numerator As Long, Denominator As Long, CommonFactor As Long) As Boolean
    2.     Dim lngIdx As Long
    3.    
    4.     CommonFactor = 1
    5.    
    6.     For lngIdx = Numerator To 2 Step -1
    7.         If Numerator Mod lngIdx = 0 Then
    8.             If Denominator Mod lngIdx = 0 Then
    9.                 CommonFactor = lngIdx
    10.                 Numerator = Numerator / CommonFactor
    11.                 Denominator = Denominator / CommonFactor
    12.                 GetCommonFactor = True
    13.                 Exit For
    14.             End If
    15.         End If
    16.     Next
    17.  
    18. End Function

    Call the function using something like the following.

    VB Code:
    1. Dim lngNumerator As Long
    2.     Dim lngDenominator As Long
    3.     Dim lngCommonFactor As Long
    4.    
    5.     lngNumerator = CLng(Text1.Text)
    6.     lngDenominator = CLng(Text2.Text)
    7.    
    8.     If GetCommonFactor(lngNumerator, lngDenominator, lngCommonFactor) Then
    9.         Label1.Caption = "The Fraction " & Text1.Text & "/" & Text2.Text & " has a common factor of " & lngCommonFactor & _
    10.             " and can be reduced to " & lngNumerator & "/" & lngDenominator
    11.     Else
    12.         Label1.Caption = "The Fraction " & lngNumerator & "/" & lngDenominator & " cannot be reduced."
    13.     End If
    Last edited by brucevde; Dec 19th, 2002 at 01:02 PM.

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