-2

This code doesn't find the correct output

for say n= 1 (although it gives the correct output for say n= 2,3,4..etc.)

if we put n= 1 to find x then the i loop will continue from 1 to 0, hence the first term in x should vanish and leftover should be the second term 5; but it gives 0 ?

Is there any limitation on the input n to run the for loop ?I would appreciate any help.

 Function math(n As Integer) As Double

 Dim i As Integer
 Dim x As Double

 For i = 1 To n - 1
    x = (n - 1) * 2 + 5
    sum = sum + x
 Next i
 math = sum
 End Function
R3uK
  • 14,417
  • 7
  • 43
  • 77
quant1
  • 47
  • 1
  • 9
  • If you put `n=1`, the loop will run 0 (zero) times. This is what you intended, right? Otherwise you wouldn't have the for loop run `n-1` times... Also, why are you using `(n-1)` inside the loop? Shouldn't it be `(i-1)`? Or have I misunderstood the task? – Anders Marzi Tornblad Nov 30 '15 at 09:26
  • not Excel fault, more like a math problem. :) – Rosetta Nov 30 '15 at 09:26
  • Yes for n= 1 the for loop run 0 times ,then x = 5 should be the output for n=1 ? How to modify the code in that case to get x= 5 for n=1 and rest all the values of n =2,3,4 etc. to remain the same ? No, I have to essentially run the loop from i to n-1 (not i-1). Infact its a summation formula for x for different values of input n . – quant1 Nov 30 '15 at 09:29
  • what is the math function for the series you want to sum ? – Rosetta Nov 30 '15 at 09:37
  • Its not math problem rather vba issue. I have to run the summation of x= (n-1)*2 +5 for i to (n-1). Please see the other answers. – quant1 Nov 30 '15 at 09:44
  • How could x be set to anything if the loop does not run at all? I think you need to learn how loops work. – Anders Marzi Tornblad Nov 30 '15 at 09:47
  • for every `n` you give, the for `For` loop will run `n-1` times. Lets say you give `n=4`, its going to be `[(4-1)*2+5]+[(4-1)*2+5]+[(4-1)*2+5]`, and this can be reduced to `(4-1)[(4-1)*2+5]` which is `33`. Correct me if i'm wrong. Hence without the `For` loop, your function is simply `(n-1)((n-1)*2+5)` – Rosetta Nov 30 '15 at 09:50
  • 4 downvotes and no explanation ? Those "brave" downvoters should try to be a tiny bit helpful by saying what is wrong with this question !!! – iDevlop Nov 30 '15 at 10:05

3 Answers3

2

Why not simply:

Function math(n As Integer) As Double
  Math = ((n - 1) * 2 + 5) * Abs((n - 1) - (n = 1))
End Function

???

if the answer is correct then Math = (n * 2 + 3) * Abs((n - 1) - (n = 1)) would be easier to understand and make much more sense

Dirk Reichel
  • 7,989
  • 1
  • 15
  • 31
  • 1
    just for me to know... pls tell me if my code is wrong... no case, no loop, simple math... – Dirk Reichel Nov 30 '15 at 10:10
  • @quant1 it was made to fit the question... as said in comments of R3uK's anser, we are not sure if that is what you want to acive... – Dirk Reichel Nov 30 '15 at 10:32
  • I also think x =(i-1)*2+5 makes more sense. my concern is simply that if n =1 . it should just give the output 5 and if n>1 entire loop should run. – quant1 Nov 30 '15 at 10:40
1

If n = 1, you end up with For i = 1 To 0 which is incorrect and
should be expressed For i = 1 To 0 STEP -1.
So I suggest you add the STEP BYand make sure it is either 1 to -1 depending on N.

iDevlop
  • 24,841
  • 11
  • 90
  • 149
  • Ok. I am running again after this modification. – quant1 Nov 30 '15 at 09:34
  • It is giving the output 10 for math(1) but it should give 5 . If here n=1 , Else loop should simply calculate x= (0)*2 +5 = 5 but it gives 10 ? Ref : R3uK answer below. – quant1 Nov 30 '15 at 09:39
1

In the for loop, if you don't precise the Step, the variable will only increment by 1.

And here, you start at 1 to go to 0, so the loop won't execute, you need to test n to cover both cases :

Function math(n As Integer) As Double
If n < 0 Then Exit Function
Dim i As Integer
Dim x As Double
Dim Summ As Double

Select Case n
    Case Is > 1
        For i = 1 To n - 1
           x = (i - 1) * 2 + 5
           Summ = Summ + x
        Next i
    Case Is = 1
        Summ = (n - 1) * 2 + 5
    Case Is = 0
        Summ = 5
    Case Else
        MsgBox "This case is not supported", vbInformation + vbOKOnly
        Exit Function
End Select

math = Summ
End Function
R3uK
  • 14,417
  • 7
  • 43
  • 77
  • Ok ,let me check by running this . – quant1 Nov 30 '15 at 09:34
  • It is giving the output 10 for math(1) but it should give 5 . If here n=1 , Else loop should simply calculate x= (0)*2 +5 = 5 but it gives 10 ? – quant1 Nov 30 '15 at 09:39
  • `Sum` was declared a local variable, I corrected that, so give the edit a try! ;) Do you need negative and 0 to work as input? – R3uK Nov 30 '15 at 09:53
  • I just need 0, Its ok now. – quant1 Nov 30 '15 at 09:56
  • Ok and is the result for math(0) = 3? – R3uK Nov 30 '15 at 10:00
  • sorry not ok...math(0) =3 not 5 ..then what to do? – quant1 Nov 30 '15 at 10:04
  • If you just replace n by 0 in the formula, it'll clearly gave 3, so I corrected it so that you'll have the desired output! ;) – R3uK Nov 30 '15 at 10:08
  • yes formula clearly gives 3 ..but let me check whether it should be x= (n-1)* 2+5 or(i-1)*2+5 to get my desired output. – quant1 Nov 30 '15 at 10:14
  • @DirkReichel Indeed that would make far more sens if the inside the loop we use `x = (i - 1) * 2 + 5`, so I corrected it! This should be good now! – R3uK Nov 30 '15 at 10:20
  • 1
    @R3uK still we dont know what he is going to achive. if there is a simple lineup of numbers like 0 = 3, 1 = 5, 2 = 7, 3 = 18, 4 = 33... would help... – Dirk Reichel Nov 30 '15 at 10:25
  • Supposed i have replaced to x= (i-1)*2 +5 ... so math(1) = 5 . but math(3) should be 12 ... because the loop will run from i= 1 to 2 i.e. x= [(1-1)*2+5 ]+[(2-1)*2+5] =12 not 18 ? – quant1 Nov 30 '15 at 10:37
  • Do you know the expected output of your function? Like math(0)=5 math(1)=5 math(2)=? math(3)=? – R3uK Nov 30 '15 at 10:47
  • @R3uK Please look at this question(same as the above code in context of summation) . Infact I am trying to code this basically. Its on the same track if you remember the previous question few days back (which was suggested by you and Charles) .http://stackoverflow.com/questions/33997744/summation-code-with-loop-in-vba – quant1 Nov 30 '15 at 11:46
  • 1
    @quant1 then pls add the orginal formula to your question (the one you want to calculate here)... would help a lot in helping... – Dirk Reichel Nov 30 '15 at 12:05
  • @Dirk Infact I have mentioned that in the new question link. Infact when n=1 then the j loop should not run same as 1 to 0 here and give the value Q(1) as the second term of the formula .I have given the proper detail .Please see here on this new link.. Thanks Dirk. http://stackoverflow.com/questions/33997744/summation-code-with-loop-in-vba – quant1 Nov 30 '15 at 13:01