Yet another fun C-puzzle
-
Perfect Mr Russel Morris is the winner :-) BTW for the record, here's my code (very similar to Russel's as the basic concept of using func pointers is same)
void Dummy(int)
{
}void Show(int i);
typedef void (*FUNC)(int);
FUNC pFunc[2];void Show(int i)
{
printf("%d\t",i);
pFunc[i<100](i+1);
}int main()
{
pFunc[0] = &Dummy;
pFunc[1] = &Show;
Show(1);
}In fact Russel's solution is better than mine. I used the < operator whoich is a conditional expression (it's valid usage as the question only disallows conditional statements) but Russel uses division which is perfect :-) Congrats again Russel. I raise my hat to you :-)
-
Hmm...
static int i=1;
typedef void (*fn) ();
fn pfn[2];void print_i(void)
{
printf("%d\n",i);
// Not a conditional! :)
// i++ / 100 will evaluate to 0 until i has reached 100
(pfn[i++ / 100])();
}void go_bye_bye()
{
exit(0);
}void main(void)
{
pfn[0] = &print_i;
pfn[1] = &go_bye_bye;(pfn\[0\])();
}
-- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy
Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)
[Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org
-
Very nice. :) I hadn't even considered using function pointers. I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. ;)
[Cheshire] I can't afford those plastic things to cover the electric sockets so I just draw bunny faces on the electric outlets to scare the kids away from them... [RLtim] Newsflash! Kids aren't afraid of bunnies. [Cheshire] Oh they will be... -Bash.org
David Stone wrote: I was talking to Nish and suggested one printf with 100 digits and 100 linebreaks. Guys, it's okay, David is from Southern California - so as I said, it's quite okay :rolleyes:
-
Hmm...
static int i=1;
typedef void (*fn) ();
fn pfn[2];void print_i(void)
{
printf("%d\n",i);
// Not a conditional! :)
// i++ / 100 will evaluate to 0 until i has reached 100
(pfn[i++ / 100])();
}void go_bye_bye()
{
exit(0);
}void main(void)
{
pfn[0] = &print_i;
pfn[1] = &go_bye_bye;(pfn\[0\])();
}
-- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy
Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)
#include <stdlib.h>
#include <stdio.h>typedef void (*funcptr)(int);
funcptr buf[2];
void count(int num)
{
printf("%d\n", 100-num);
(*buf[!!num])(num-1);
}int main()
{
buf[0] = exit;
buf[1] = count;
count(99);
}Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :D cheers, Chris Maunder
Chris Maunder wrote: Can we print more than 100? Does the program have to terminate elegantly? I came up with a quick solution but it ain't exactly pretty :-D
-
Mine was pretty similar except it doesn't use globals and uses a double-not rather than / 100 :)
#include <stdlib.h>
#include <stdio.h>typedef void (*funcptr)(int);
funcptr buf[2];
void count(int num)
{
printf("%d\n", 100-num);
(*buf[!!num])(num-1);
}int main()
{
buf[0] = exit;
buf[1] = count;
count(99);
}Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
*Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish
-
*Applause* The double not was pretty cool - more elegant than Russel's division - and a lot more neater than my comparision operator :-) Nish
Ryan Russel still wins though - 1) he posted it first 2) he uses division which while inelegant is not a conditional expression, your double not (while elegant) is still a conditional expression (valid as the question only disallows conditional statements and expressions are not statements) Nish
-
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
How about this
int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }
-
How about this
int main(int ac, char ** av) { static int i=1; printf("%d ",i++); i%101&&main(0,0); }
Cool stuff, Vivek :-) Nish
-
Cool stuff, Vivek :-) Nish
Here's an improvement (LOC reduced anyway)
int main(int ac, char ** av)
{
printf("%d ",101-ac,(ac%100)&&main(ac+1,0));
}Run the app without arguments (first time ac will be 1) Nish
-
BTW 100
printf
s don't count :rolleyes: -
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; }
this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time. -
template <long val> void printfn() { std::cout << val << "\n"; printfn<val + 1>(); } template <> void printfn<100L>() { std::cout << "100\n"; } int main(int argc, char* argv[]) { printfn<1L>(); system("PAUSE"); return 0; }
this works fine in VS.NET 2003 and gcc, but not with VC++ 6.0 which generates wrong specialization. i suppose that it also faster then previous solutions, since it is resolved in compile time.Good code, but C does not support templates :-) Nish
-
Good code, but C does not support templates :-) Nish
:-O i got carried away by this line from your original post: "An answer was later posted in C++ (and the poster said that it was not possible without C++)."
-
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yr -
This question was asked in the Trivandrum Microsoft UG forums :- Using C, print 1-100 without using any loop or conditional statements? An answer was later posted in C++ (and the poster said that it was not possible without C++). Here's his C++ solution :-
static int i=0;
class ad
{
ad()
{
i++;
cout << i;
}
}void main()
{
ad objad[100];
}I spent a part of my Sunday evening wondering if it was possible in some alternative manner and found a solution. It shouldn't take you more than 15-20 minutes to think of it (assuming you haven't done this before). Let's see who gets there first (remember - no C++, no classes). Nish :-)
Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason
-
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yrif
is a conditional statement - not allowed. -
BTW 100
printf
s don't count :rolleyes:Does one
printf
with 100 numbers count? :) sorry if this question was already asked, I didn't look at other posts... David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
assuming that if also doesnt matter n yeah also mind that i m writing code directly here, neither compiled nor checked for output -
int main(int x) { printf("%d",x); if(x<100) main(++x); }
program is expected to run from command prompt as filename 0 what do u think?? UTSAV MCA final yrutsav_verma wrote: int main(int x) { printf("%d",x); if(x<100) main(++x); } program is expected to run from command prompt as filename 0 what do u think?? In addition to "if" being conditional (and therefore you break the rules), there are other bugs in your code. The first argument to main is the number of arguments and if you invoke the prog as "filename 0" as you say, this will be equal to 2 (including the file name). Thus your prog starts printing from 2. 2nd issue is that main should ideally take 2 arguments (though most compilers will let your code compile).
-
Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } Jason
Jason Pease wrote: Does this count? void main(){ char buffer[8] strcpy(buffer,"1-100"); cout << buffer << endl; } :rolleyes: