• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calculating multiple of five using Recursion

 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am trying to find next multiple of five using the function nextMultipleOfFive().
EXAMPLE: if the grades array contain 78,67,34,90 Then nextMultiple should give 80,70,35,90
but i am getting output 1,1,1,1.I am doing something wrong please tell. Thanks in advance

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find your code very difficult to read if you don't indent it consistently. I don't see where your recursive method is going wrong. Shall try it out and see what is happening.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables are ... local to the method. Kinda obvious, but keep in mind that when a method calls itself, it is different method calls. The local variable in the called method is not the same as the local variable in the caller method, even though it is the same method. You need to return the result from the called method back to the caller method.

Henry
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help.
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Local variables are ... local to the method. Kinda obvious, but keep in mind that when a method calls itself, it is different method calls. The local variable in the called method is not the same as the local variable in the caller method, even though it is the same method. You need to return the result from the called method back to the caller method.

Henry


can you please elaborate what changes should i do
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use much less code for a recursive method. Shorten it to this sort of thing:-In the case of a recursive method, instead of y, use a recursive call, probably with the argument increased or reduced by 1. You are trying to go up, so you would use myRecursiveMethod(i + 1), but most recursive calls go down, so other methods use myRecursiveMethod(i − 1).
Does that help? Remember the first operand for ?: is always of boolean type, and the rules about the middle and right operands are complicated, but I suggest you give them both the same type.
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Use much less code for a recursive method. Shorten it to this sort of thing:-In the case of a recursive method, instead of y, use a recursive call, probably with the argument increased or reduced by 1. You are trying to go up, so you would use myRecursiveMethod(i + 1), but most recursive calls go down, so other methods use myRecursiveMethod(i − 1).
Does that help? Remember the first operand for ?: is always of boolean type, and the rules about the middle and right operands are complicated, but I suggest you give them both the same type.


Thanks, it helped
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good. Show us what you have got now, which I presume is working correctly. By the way: have you tested your recursive method with non‑positive arguments?
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have changed my code like this.No, I haven't tested my code for non-positive arguments.what is the need for testing it? (actually, n value will be always positive according to my problem.)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a lot better, but it can still be improved:-No assignments inside expressions (ugh!). No need for ++. No need for another local variable.
The first method which rejects negative arguments is completely optional; you can miss it out completely
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do calculate N % 5 anyway, you can speed up the process a little:

Must you use recursion?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also,

Not easy to read though.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:If you do calculate N % 5 anyway, you can speed up the process a little . . . Must you use recursion?

I thought the OP did want to use recursion, so I kept quiet about that.
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Piet Souris wrote:If you do calculate N % 5 anyway, you can speed up the process a little . . . Must you use recursion?

I thought the OP did want to use recursion, so I kept quiet about that.


Yes, I was interested in using recursion. Thanks for your help.
 
priyanshi bhardwaj
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Piet Souris Thanks for your help and yes I was interested in using recursion.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

priyanshi bhardwaj wrote:. . . Thanks for . . .

That's a pleasure
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome!
 
Arthur, where are your pants? Check under this tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic