soompi forums: Help With Compsci? - soompi forums

Jump to content

Page 1 of 1

Help With Compsci? Recursion.

#1 User is offline   ny-sw / ny_sw. 

  • no more regrets. (:
  • Pip
  • Group: Members
  • Posts: 2,087
  • Joined: 04-October 05

Posted 05 May 2008 - 04:51 PM

If the method is
public static void whatsIt(int n)
{

if (n >10)
{
whatsIt(n/10);
System.out.print(n%10);
}
}

Would the output of whatsIt(347) be 743 or 347?
"Pain is temporary, but PRIDE is forever EXCLAMATIONPOINT!"
0

#2 User is offline   awdark 

  • Cookie Monster
  • Icon
  • Group: Administrators
  • Posts: 9,591
  • Joined: 04-October 05

Posted 05 May 2008 - 05:16 PM


whatsIt(347)
Goes in and since its greater than 10 it goes into the if statement.
It recurses on the whatsIt and re-calculates whatsIt with 347/10.
whatsIt(34) the .7 is lost.
It is greater than 10, divides again and recurses whatisIt(3)
It is now less than 10, so it will output 3%10 (3/10 = 0.3 so remainder is 3).

= 3

I hope im right... otherwise it will be a bit embarrassing lol
0

#3 User is offline   watcher 

  • Dubya A. Teacher
  • Icon
  • Group: Friends of Soompi
  • Posts: 5,870
  • Joined: 06-October 05

Posted 06 May 2008 - 10:53 AM

i got 47

whatsIt(347)
n>10 = true; [n=347]
calls -> whatsIt(347/10)
n>10 = true; [n=34]
calls -> whatsIt(34/10)
n>10 = false; [n=3]
exit -> whatsIt(34/10)
print n%10 [n=34] -> "4"
exit -> whatsIt(347/10)
print n%10 [n=347] -> "7"
exit -> whatsIt(347)
end program

QUOTE (noraesonagi @ May 5 2008, 05:51 PM) <{POST_SNAPBACK}>
If the method is
public static void whatsIt(int n)
{

if (n >10)
{
whatsIt(n/10);
System.out.print(n%10);
}
}

Would the output of whatsIt(347) be 743 or 347?

0

#4 User is offline   watcher 

  • Dubya A. Teacher
  • Icon
  • Group: Friends of Soompi
  • Posts: 5,870
  • Joined: 06-October 05

Posted 06 May 2008 - 11:25 AM

sorry... double post...
0

#5 User is offline   Crispy 

  • Member
  • Icon
  • Group: Friends of Soompi
  • Posts: 2,849
  • Joined: 26-June 06

Posted 06 May 2008 - 03:19 PM

QUOTE (watcher @ May 6 2008, 01:53 PM) <{POST_SNAPBACK}>
i got 47

whatsIt(347)
n>10 = true; [n=347]
calls -> whatsIt(347/10)
n>10 = true; [n=34]
calls -> whatsIt(34/10)
n>10 = false; [n=3]
exit -> whatsIt(34/10)
print n%10 [n=34] -> "4"
exit -> whatsIt(347/10)
print n%10 [n=347] -> "7"
exit -> whatsIt(347)
end program


Agreed, there should only be a 2-digit output since the third instance (recursion) of the function never answers true for the if statement.
But then again, I didn't compile the code and I haven't messed around with Comp. Sci. stuff in years.

Off-topic, but do you go to UH?
Grip me with your eyes.
0

#6 User is offline   watcher 

  • Dubya A. Teacher
  • Icon
  • Group: Friends of Soompi
  • Posts: 5,870
  • Joined: 06-October 05

Posted 06 May 2008 - 03:29 PM

QUOTE (Crispy @ May 6 2008, 04:19 PM) <{POST_SNAPBACK}>
Agreed, there should only be a 2-digit output since the third instance (recursion) of the function never answers true for the if statement.
But then again, I didn't compile the code and I haven't messed around with Comp. Sci. stuff in years.

Off-topic, but do you go to UH?


nope... haha... that was completely random...
graduated from SC many years ago
0

#7 User is offline   Crispy 

  • Member
  • Icon
  • Group: Friends of Soompi
  • Posts: 2,849
  • Joined: 26-June 06

Posted 06 May 2008 - 03:32 PM

^ Lawl, I meant the original poster... ;p
(she has Houston under her Location thing)
Grip me with your eyes.
0

#8 User is offline   watcher 

  • Dubya A. Teacher
  • Icon
  • Group: Friends of Soompi
  • Posts: 5,870
  • Joined: 06-October 05

Posted 06 May 2008 - 03:33 PM

QUOTE (Crispy @ May 6 2008, 04:32 PM) <{POST_SNAPBACK}>
^ Lawl, I meant the original poster... ;p
(she has Houston under her Location thing)


haha.. my bad...
thot it was me since you quoted me tongue.gif
0

#9 User is offline   Voltage 

  • Member
  • Pip
  • Group: Members
  • Posts: 199
  • Joined: 22-October 05

Posted 06 May 2008 - 05:58 PM

just paste that into a java ide next time and run it smile.gif
0

#10 User is offline   ny-sw / ny_sw. 

  • no more regrets. (:
  • Pip
  • Group: Members
  • Posts: 2,087
  • Joined: 04-October 05

Posted 06 May 2008 - 06:52 PM

I actually go to Bellaire HS and will be going to SJU next year. biggrin.gif
I thought UH compsci learns C++ first? According to my compsci teacher..

I found out why it was 347 so thanks guys :]

Explanation:
public static void whatsIt(int n)
{

if (n >10)
{
whatsIt(n/10);
System.out.print(n%10);
}

}

whatsIt(347) calls whatsIt(34) which calls whatsIt(3)...
Because its recursive, whatsIt(3) has to finish computing before (34) and (347) so it prints out 3%10 = 3, 34%10 = 4, and 347%10 = 7.
Result : 347.

I might cancel my AP score, but I'm really curious as to what I got... :/ What to do, what to do..?
"Pain is temporary, but PRIDE is forever EXCLAMATIONPOINT!"
0

#11 User is offline   Crispy 

  • Member
  • Icon
  • Group: Friends of Soompi
  • Posts: 2,849
  • Joined: 26-June 06

Posted 06 May 2008 - 07:01 PM

QUOTE (noraesonagi @ May 6 2008, 09:52 PM) <{POST_SNAPBACK}>
I actually go to Bellaire HS and will be going to SJU next year. biggrin.gif
I thought UH compsci learns C++ first? According to my compsci teacher..

I found out why it was 347 so thanks guys :]

Explanation:
public static void whatsIt(int n)
{

if (n >10)
{
whatsIt(n/10);
System.out.print(n%10);
}

}

whatsIt(347) calls whatsIt(34) which calls whatsIt(3)...
Because its recursive, whatsIt(3) has to finish computing before (34) and (347) so it prints out 3%10 = 3, 34%10 = 4, and 347%10 = 7.
Result : 347.

I might cancel my AP score, but I'm really curious as to what I got... :/ What to do, what to do..?

But doesn't the modulus not return a value since 10 doesn't go into 3?
(I don't remember... it's been so longgg)

And I don't know about UH going with C++ first, although that sounds kinda correct.
I'm actually technically a Comp. Sci. minor (Computer Engineering major), but haven't taken a single Comp. Sci. class in my college career yet, ahaha.
(because I'm switching to English soon, anyways)

AND!
Weird question, I know, but what's Bellaire HS like?
Because my sister and I were having a random argument the other day because she said that Bellaire HS was full of crime and stuff, while I've heard only good things about it. (we always fight about stupid stuff that doesn't even relate to us)
Grip me with your eyes.
0

#12 User is offline   watcher 

  • Dubya A. Teacher
  • Icon
  • Group: Friends of Soompi
  • Posts: 5,870
  • Joined: 06-October 05

Posted 06 May 2008 - 08:14 PM

QUOTE (noraesonagi @ May 6 2008, 07:52 PM) <{POST_SNAPBACK}>
I actually go to Bellaire HS and will be going to SJU next year. biggrin.gif
I thought UH compsci learns C++ first? According to my compsci teacher..

I found out why it was 347 so thanks guys :]

Explanation:
public static void whatsIt(int n)
{

if (n >10)
{
whatsIt(n/10);
System.out.print(n%10);
}

}

whatsIt(347) calls whatsIt(34) which calls whatsIt(3)...
Because its recursive, whatsIt(3) has to finish computing before (34) and (347) so it prints out 3%10 = 3, 34%10 = 4, and 347%10 = 7.
Result : 347.

I might cancel my AP score, but I'm really curious as to what I got... :/ What to do, what to do..?


whatsIt(3) fails the if logic at n>10. it wont print the 3...
it's impossible... it's... it's.... ahhh!! im going crazy! tongue.gif
0

#13 User is offline   ny-sw / ny_sw. 

  • no more regrets. (:
  • Pip
  • Group: Members
  • Posts: 2,087
  • Joined: 04-October 05

Posted 07 May 2008 - 08:16 PM

I think I put the bracket in the wrong place again... o__o;
Yeah, that'd be the case if the bracket actually belonged there (the second to last one)...
>__> I hope I didn't do something stupid like that on the AP lol
SORRY D:
"Pain is temporary, but PRIDE is forever EXCLAMATIONPOINT!"
0

Share this topic:


Page 1 of 1

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users