Consulting

Results 1 to 6 of 6

Thread: help with understanding loops and multiplying integers

  1. #1
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location

    help with understanding loops and multiplying integers

    the goal of my assignment is to create a function that multiplies a series of integers, when given a "low integer" and the number of integers. my issue occurs because n is lower than Lo in the example my professor gave and i cant figure out how to output anything other than 1. Am i missing something super basic in my code? I have already tried stepping through it with no luck. the example was 4 and 3.

    Function MultSeries(Lo, n) 
    
    If Int(Lo) <> Lo Or Int(n) <> n Then
    MultSeries = -999
    ElseIf n <= 0 Then
    MultSeries = -999
    Else
    tot = 1
    For i = Lo To n
    tot = tot * i
    Next i
    MultSeries = tot
    End If
    End Function
    Last edited by sawyer22; 03-16-2018 at 12:59 PM. Reason: wanted to include numerical values for context

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Welcome to the forum -- it'd be worthwhile to read the FAQs in my signature

    1. I added CODE tags for you to format the macro -- you can use the [#] icon to insert them next time

    2. We don't do homework, but we will provide hints



    Option Explicit   ' always a good idea
    
    Sub drv()     ' just an easy way to debug / test
        
        'use F8 to single step
        
        'test 1
        MsgBox MultSeries(10, 5)
        'test 2
        MsgBox MultSeries(5, 10)
    
    End Sub
    
    
    'I like to always Dim and define my variables
    Function MultSeries(Lo As Long, n As Long) As Long
        
        Dim tot As Long, i As Long
        If Int(Lo) <> Lo Or Int(n) <> n Then
            MultSeries = -999
        ElseIf n <= 0 Then
            MultSeries = -999
        Else
            tot = 1
            For i = Lo To n     '   see what happens on this step
                tot = tot * i
            Next i
            
            MultSeries = tot
        End If
    End Function
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location
    ok so i stepped through it again, and the only thing i can think of is that with the first example it is taking i and increasing it based on the implied step of 1. I see on the second test it only goes through one time, but what im wondering is how i can write my code to both step +1 and step -1. I tried searching microsofts manual for a solution with no luck.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    In both cases they reach this line

    For i = Lo To n     '   see what happens on this step
    Case 1 is

    For I = 10 to 5


    Case 2 is

    For I = 5 To 10


    When single stepping, did you notice that in Case 1 the looping was never executed (since 10 is already greater than 5) and it jumped right to

    MultSeries = tot
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Newbie
    Joined
    Mar 2018
    Posts
    3
    Location
    ah ok i think i am starting to get it thank you

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    VBA uses the WorksheetFunction Object to access Excel's Functions
    MIN() and MAX() are Excel Functions
    Abs() is a VBA function that returns the Absolute value of a number

    I am a lazy typist so I usually declare a short named variable to "hold" the Worksheet Function Object
    Dim WsF as Object
    'or
    Dim WsFunc As Object
    Set Wsf = Application.WorksheetFunction
    'or
    Set WsFunc = Application.WorksheetFunction
    
    x = WsF.Max(3, 2, 6, 10) 'x = 10
    y = WsFunc.Min(3, 2, 6, 10) 'y = 2
    z = Abs(-5) 'z = 5
    That way you can loop from the Min () of Lo and n to the Max() of Lo and n


    You can decide which Sign to use
    Dim Sign As Long 'Or as Integer, Integers use less memory, but Longs are more versatile
    
    Sign = 1
    If n * Lo < 0 Then Sign = -1
    Finally multiply the result of the loop by the Sign.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •