wu :: forums
« wu :: forums - In three ways »

Welcome, Guest. Please Login or Register.
Nov 8th, 2024, 6:04pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: SMQ, Icarus, Eigenray, ThudnBlunder, Grimbal, william wu, towr)
   In three ways
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: In three ways  (Read 7891 times)
Misha Kruk
Guest

Email

In three ways  
« on: Aug 1st, 2002, 8:50pm »
Quote Quote Modify Modify Remove Remove

How well do you know C? "for" loops? Wink
 
Find three ways to make the program below to print 20 copies of the dash character '-' by changing/adding one character:
 
int i, n=20;
for (i=0; i < n; i--) {printf("-");};
 
Wrong solutions:
 
    * Changing "n=20" into "n=-20" won't work.
    * Changing "i--" into "i++" doesn't satisfy the one character condition.
    * Changing "i=0" into "i=40" won't work.
    * Changing "i < n" into "i < -n" wont' work.  
IP Logged
-D-
Guest

Email

Re: In three ways  
« Reply #1 on: Aug 1st, 2002, 10:24pm »
Quote Quote Modify Modify Remove Remove

Well... since the initial code is obviously wrong.. the first answer would be to "fix" the code...
 
 
for( i = 0; -i < 20; i-- ) ...
 
IP Logged
-D-
Guest

Email

Re: In three ways  
« Reply #2 on: Aug 1st, 2002, 10:26pm »
Quote Quote Modify Modify Remove Remove

Answer #2... change    i--   to    n--
 
IP Logged
AJ
Guest

Email

Re: In three ways  
« Reply #3 on: Aug 4th, 2002, 7:49am »
Quote Quote Modify Modify Remove Remove

Third one:
 for(i = 0 ; i + n ; i--)
 
Tricy one Wink
IP Logged
-D-
Guest

Email

Re: In three ways  
« Reply #4 on: Aug 4th, 2002, 11:51am »
Quote Quote Modify Modify Remove Remove

good one AJ.... that was tricky.
-D-
IP Logged
Barukh
Uberpuzzler
*****






   


Gender: male
Posts: 2276
Re: In three ways  
« Reply #5 on: Sep 22nd, 2003, 2:20am »
Quote Quote Modify Modify

This nice puzzle has a variation:
 
Find a way to make this program print 21 copies of '-'. The requirements are the same.
IP Logged
Sir Col
Uberpuzzler
*****




impudens simia et macrologus profundus fabulae

   
WWW

Gender: male
Posts: 1825
Re: In three ways  
« Reply #6 on: Sep 22nd, 2003, 5:54am »
Quote Quote Modify Modify

Are we supposed to find 3 ways?
 
Here's one:
int i, n=20;  
for (i=0; i < -n; i--) {printf("-");};
IP Logged

mathschallenge.net / projecteuler.net
wowbagger
Uberpuzzler
*****





242002184 242002184    


Gender: male
Posts: 727
Re: In three ways  
« Reply #7 on: Sep 22nd, 2003, 6:20am »
Quote Quote Modify Modify

Sir Col, your solution doesn't work (as already noted in Misha Kruk's original post), because already the first test of i < -n is false, so there will be no "-" characters at all.
IP Logged

"You're a jerk, <your surname>!"
Sir Col
Uberpuzzler
*****




impudens simia et macrologus profundus fabulae

   
WWW

Gender: male
Posts: 1825
Re: In three ways  
« Reply #8 on: Sep 22nd, 2003, 8:53am »
Quote Quote Modify Modify

Duh, silly me. I was thinking that it looped until, rather than while.
 
In which case... hmm?  Huh
IP Logged

mathschallenge.net / projecteuler.net
James Fingas
Uberpuzzler
*****





   
Email

Gender: male
Posts: 949
Re: In three ways  
« Reply #9 on: Sep 22nd, 2003, 10:19am »
Quote Quote Modify Modify

Haha! Very very sneaky! The answer is too beautiful to give away, but here's what I was thinking about when I happened to stumble on the answer: could we use a bitwise operator instead of '+'?
IP Logged

Doc, I'm addicted to advice! What should I do?
Sir Col
Uberpuzzler
*****




impudens simia et macrologus profundus fabulae

   
WWW

Gender: male
Posts: 1825
Re: In three ways  
« Reply #10 on: Sep 22nd, 2003, 2:05pm »
Quote Quote Modify Modify

Ooh, that is very sneaky. I'd better not give too much away. Wink
 
Nice puzzle, Barukh.
IP Logged

mathschallenge.net / projecteuler.net
Dudidu
Full Member
***





   


Posts: 227
Re: In three ways  
« Reply #11 on: Oct 8th, 2003, 11:00am »
Quote Quote Modify Modify

I'm sorry but I think that the answer should be posted so
everybody could learn from it (if they want to).
So...
  Wink for (i = 0 ; ~i < n ; i--) Wink
 
 
 
IP Logged
Niharika Jeena
Guest

Email

Re: In three ways  
« Reply #12 on: Apr 25th, 2005, 2:47am »
Quote Quote Modify Modify Remove Remove

U can print it 21 times by adding "~" before i in condition..
  for(i=0;~i<n;i--)
  this will give the solution
IP Logged
d_ram22
Newbie
*



Coder

  d_ram22  


Gender: male
Posts: 2
Re: In three ways  
« Reply #13 on: Apr 7th, 2009, 8:36am »
Quote Quote Modify Modify

Good puzzle there Grin
the fixed questions is :
Make following program print - 20 times by making one character change (add/modify).
 
#include<stdio.h>
main() {
 
int i, n=20;  
 
for (i=0; i < n; i++) {printf("-");};
}
 
Third Answer:
hidden:

#include<stdio.h>
main() {
 
int i, n=20;  
 
for (i=0; i ^ n; i++) {printf("-");};
}
IP Logged
Eigenray
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 1948
Re: In three ways  
« Reply #14 on: Apr 7th, 2009, 12:17pm »
Quote Quote Modify Modify

on Apr 7th, 2009, 8:36am, d_ram22 wrote:
Good puzzle there Grin
the fixed questions is :
Make following program print - 20 times by making one character change (add/modify).
 
#include<stdio.h>
main() {
 
int i, n=20;  
 
for (i=0; i < n; i++) {printf("-");};
}

Well that's silly.  For example, you could replace any of the spaces with a \t or a \n, or most of the \ns with spaces.  Or add/remove a space.  Or remove the last semicolon.  Or add a semicolon.  And of course you can also change i < n to i - n.
 
With one character change, what possible numbers of '-'s can you print?
0, 16, 20, 21, 40, infinity, ...?
Though technically undefined, with gcc<3 or gcc-4 -O(0,1) (or gcc-4 -(fwrapv, fno-strict-overflow), etc.), there are also 2147483648 and 2147483649, if sizeof(int)==4.  And then there is undefined (but probably positive and finite on a 32-bit machine).
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board