Help With Compsci? Recursion.
#1
Posted 05 May 2008 - 04:51 PM
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?
#2
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
#3
Posted 06 May 2008 - 10:53 AM
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
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?
#4
Posted 06 May 2008 - 11:25 AM
#5
Posted 06 May 2008 - 03:19 PM
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?
#6
Posted 06 May 2008 - 03:29 PM
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
#7
Posted 06 May 2008 - 03:32 PM
(she has Houston under her Location thing)
#8
Posted 06 May 2008 - 03:33 PM
(she has Houston under her Location thing)
haha.. my bad...
thot it was me since you quoted me
#10
Posted 06 May 2008 - 06:52 PM
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..?
#11
Posted 06 May 2008 - 07:01 PM
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)
#12
Posted 06 May 2008 - 08:14 PM
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!
#13
Posted 07 May 2008 - 08:16 PM
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:













