Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Programmers, how do you solve this?

Programmers, how do you solve this?
Thread Tools
techweenie1
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Mar 29, 2006, 01:12 PM
 
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;
}
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Mar 29, 2006, 02:14 PM
 
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.
     
Kristoff
Mac Elite
Join Date: Sep 2000
Location: in front of the keyboard
Status: Offline
Reply With Quote
Mar 29, 2006, 03:48 PM
 
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.
     
Catfish_Man
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Mar 29, 2006, 04:18 PM
 
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...
     
Tomchu
Mac Elite
Join Date: Sep 2005
Status: Offline
Reply With Quote
Mar 29, 2006, 04:35 PM
 
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.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 29, 2006, 07:20 PM
 
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'."
     
zanyterp
Mac Enthusiast
Join Date: Apr 2003
Location: manticore or people's republic of haven
Status: Offline
Reply With Quote
Mar 30, 2006, 01:12 AM
 
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.
     
techweenie1  (op)
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Mar 30, 2006, 04:36 PM
 
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.
     
techweenie1  (op)
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Mar 30, 2006, 04:39 PM
 
and actually this was on a test for a place called Mintel...
     
Tomchu
Mac Elite
Join Date: Sep 2005
Status: Offline
Reply With Quote
Mar 30, 2006, 04:57 PM
 
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.
     
techweenie1  (op)
Dedicated MacNNer
Join Date: May 2001
Location: Chicago, IL
Status: Offline
Reply With Quote
Mar 30, 2006, 04:58 PM
 
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. )
     
Tomchu
Mac Elite
Join Date: Sep 2005
Status: Offline
Reply With Quote
Mar 30, 2006, 06:36 PM
 
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.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:51 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,