TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bruce D. Wedding
from: Jasen Betts
date: 2004-06-06 16:24:40
subject: [C] C Puzzles

Hi Bruce.

05-Jun-04 07:03:02, Jasen Betts wrote to Bruce D. Wedding

Some erata to my earlier post.

 JB>  or use the trinary operator as "if" and recursion to loop back.

there was a missing parenthesis, and anyway a single recursive function
using && for if is sufficient.

 #include 
  void count(int n)
  {
  n<=100 && (printf("%d\n",n),
count(n+1),printf("%d\n",n));
  }
  main(){
  count(1);
  };

 BDW>> 3. Find if the given number is a power of 2.

#include 

 JB>  unsigned ispow2(int x)
 JB>  {
 JB>  unsigned n;
 JB>  for(n=1;n;n<<=1) if(x=n)return TRUE;

should be

      for(n=1;n;n<<=1) if(x==n)return TRUE;

 JB>  return FALSE;
 JB>  }

 BDW>> 5. Write a function in different ways that will return f(7) = 4 and
 BDW>> f(4) = 7

someone wanted 4 ways, here's 3 more different ways.

  JB> int f(int x) { return 11 - x ;}
  JB> int f(int x) { return 28 / x ;}
      int f(int x) { return x ^ 3  ;}
      int f(int x) { return 7 - (x&3) ;}
      int f(int x) { return x==4 ? 7 : 4 ;}

I was wrong about the number of other soluutions too.
it's pow(n,n) not factorial(n)

 BDW>> 6. Remove duplicates in array

 JB> this one has been done to death here already.

 also incompletely specified.
 -is ordering of the results critical, is O(n^2) performace acceptable ?

  BDW>> 7. Finding if there is any loop inside linked list.

....
 JB>     l=l->next

there was missing semicolon here.

8.2
 BDW> OK, write code to determine if a value is unsigned or signed

this one was messed up. should be.

  if(  value >= 0  && ~value >= 0 )
     { puts("unsigned");}
  else
     { puts("signed");};

 -=> Bye <=-
---
* Origin: Entropy isn't what it used to be. (3:640/1042)
SEEN-BY: 633/267 270
@PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.