|
|
Programmers, how do you solve this?
|
|
|
|
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status:
Offline
|
|
What initial values of a and c are required such that the final values of a and b are:
a = 32
b = 4
int a,b,c
a = ?
b = 0
c = ?
for( b=0; a<12; b++ )
{
a = (a+a) * c;
}
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
This sounds like a homework question. I'm probably lazy enough that, if I were assigned that question, I'd write a program to generate and try versions of that program in a big nested loop until it works.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status:
Offline
|
|
If I were assigned that question, I'd switch schools to one that has a better grip on appropriate CS material. This is trivial math, not computer science.
|
signatures are a waste of bandwidth
especially ones with political tripe in them.
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Aug 2001
Status:
Offline
|
|
Originally Posted by Kristoff
If I were assigned that question, I'd switch schools to one that has a better grip on appropriate CS material. This is trivial math, not computer science.
Good point...
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2005
Status:
Offline
|
|
Uh ... you're kidding, right? I did this in about 15 seconds in my head.
Code:
a = 1, c = 1
2 = 1+1 0
4 = 2+2 1
8 = 4+4 2
16 = 8+8 3
32 = 16+16 4
The number on the right is the iteration of the loop (b). I don't do the "* c" part, because I'm just multiplying by 1 there.
Kristoff is right.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Look at the loop condition: a<12. Your final a value would be 16.
Still agree that it looks like essentially a basic math problem. Don't see how this is CS.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status:
Offline
|
|
CS in the manner that it is an intro to for loop logic? and the book example/practice exercises gives this as a question?
|
some people are like slinkys: they don't do much, but are fun to push down stairs.
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status:
Offline
|
|
I kept on ending up with 16 too due to the condition on a, that's why I asked...thought I might have missed something.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status:
Offline
|
|
and actually this was on a test for a place called Mintel...
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2005
Status:
Offline
|
|
Originally Posted by Chuckit
Look at the loop condition: a<12. Your final a value would be 16.
Still agree that it looks like essentially a basic math problem. Don't see how this is CS.
Good point -- missed that. In any case, the fix is freaking simple. Make them both negative.
Code:
a = -1, c = -1
0 (-1 + -1) * -1 = 2
1 (2 + 2) * -1 = -4
2 (-4 + -4) * -1 = 8
3 (8 + 8) * -1 = -16
4 (-16 + -16) * -1 = 32
You manage to "sneak" past the loop condition with the -16, which is then turned into a 32 to give you the final answer and break out of the loop.
|
|
|
|
|
|
|
|
|
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status:
Offline
|
|
Also the answer I put down for this problem was A=2 C=1. I "didn't pass" the test ... so I'm guessing the correct answer for this problem was A=1 C=1. I'm pretty pissed though that this stupid problem, the only one I got wrong (4 Problem test) got me cut from the interview process. I always hate these sort of problems too, because I'm never quite clear on what they want...to me A=2 C=1 seems like the better answer since if you actually run that through a loop on a computer you'll end up with a=16 when b is 3 ... and a=32 when b is 4 (upping the condition on a<=16) if you did this with A=1 C=1 (upping the condition of course so A can go past 16 you end up with B=5 when the loop finishes with A=32...I guess it really all depends on what number you want to be off when the loop ends...A or B ... I guess what I'm saying is B iterates one more time than A does...so B reaches 4 but a doesn't reach 32 + 32 ...
b=0 a=2+2 c=1
b=1 a=4+4 c=1
b=2 a=8+8 c=1
b=3 a=16+16 c=1
b=4 a=32+32 c=1
(
Last edited by techweenie1; Mar 30, 2006 at 05:11 PM.
)
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2005
Status:
Offline
|
|
Yeah, that's what I did the first time, but I missed the loop condition. You will never get to the 5th iteration of the loop (b=4) if 'a' hits 16 after the 4th. My solution with the negative numbers (a = -1, c = -1) works fine because on the 4th iteration, 'a' is -16. By the next iteration, 'a' is positive 32, so we exit out of the loop.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|