I want to write a C function that will print 1 to N one per each line on the stdout where N is a int parameter to the function. The function should not use while, for, do-while loops, goto statement, recursion, and switch statement. Is it possible? I want to find an answer to this as this a challenge question
write all possible output to a string first, and null terminate it where the output should stop.
this is a rather dirty solution, but given the limitations, all I can think of,
except for using assembler, off course.
char a[]="1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n"/*...*/;
main(n,v)char**v;{n=atoi(v[1]);
#define c(x)(n>x?n-x:0)
a[n+c(1)+c(9)+c(99)+c(999)+c(9999)+c(99999)+c(999999)+c(9999999)/*+...*/]=0;
puts(a);}
Given that MAX_INT==2147483647
on popular architectures, we only need to go up to +c(999999999)
. Typing out that initial string might take a while, though...
N is not fixed, so you can't unrole the loop. And C has no iterators as far as I know.
You should find something that mimics the loop.
Or thinking outside the box:
(for example N is limited to 1000, but it is easy to adapt)
int f(int N) {
if (N >= 900) f100(100);
if (N >= 800) f100(100);
if (N >= 700) f100(100);
...
f100(n % 100);
}
int f100(int N) {
if (N >= 90) f10(10);
if (N >= 80) f10(10);
if (N >= 70) f10(10);
...
f(n % 10);
}
int f10(int N) {
if (N >= 9) func();
if (N >= 8) func();
if (N >= 7) func();
...
}
If you know the upper limit of N you can try something like this ;)
void func(int N)
{
char *data = " 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n";
if (N > 0 && N < 12)
printf("%.*s", N*3, data);
else
printf("Not enough data. Need to reticulate some more splines\n");
}
Joke aside, I don't really see how you can do it without recursion or all the instructions you mentioned there. Which makes me more curious about the solution.
Edit: Just noticed I proposed the same solution as grombeestje :)
This does it:
int main ()
{
printf ("1 to N one per each line\n");
return 0;
}
Here is another one:
#include <stdlib.h>
#include <stdio.h>
int main (int c, char ** v) {
char b[100];
sprintf (b, "perl -e 'map {print \"$_\\n\"} (1..%s)'", v[1]);
system (b);
return 0;
}
You can use setjmp and logjmp functions to do this as shown in this C FAQ
For those who are curious to why someone have a question like this, this is one of the frequently asked questions in India for recruiting fresh grads.
You can do this by nesting macros.
int i = 1;
#define PRINT_1(N) if( i < N ) printf("%d\n", i++ );
#define PRINT_2(N) PRINT_1(N) PRINT_1(N)
#define PRINT_3(N) PRINT_2(N) PRINT_2(N)
#define PRINT_4(N) PRINT_3(N) PRINT_3(N)
:
:
#define PRINT_32(N) PRINT_31(N) PRINT_31(N)
There will be 32 macros in total. Assuming size of int
as 4 bytes. Now call PRINT_32(N)
from any function.
Edit: Adding example for clarity.
void Foo( int n )
{
i = 1;
PRINT_32( n );
}
void main()
{
Foo( 5 );
Foo( 55 );
Foo( 555 );
Foo( 5555 );
}
I'd go for using longjmp()
#include <stdio.h>
#include <setjmp.h>
void do_loop(int n) {
int val;
jmp_buf env;
val = 0;
setjmp(env);
printf("%d\n", ++val);
if (val != n)
longjmp(env, 0);
}
int main() {
do_loop(7);
return 0;
}
#include <stdlib.h>
int callback(const void *a, const void *b) {
static int n = 1;
if (n <= N)
printf("%d\n", n++);
return 0;
}
int main(int argc, char *argv) {
char *buf;
/* get N value here */
buf = malloc(N); // could be less than N, but N is definitely sufficient
qsort(buf, N, 1, callback);
}
I think it doesn't count as recursion.
With blocking read, signals and alarm. I thought I'd have to use sigaction and SA_RESTART, but it seemed to work well enough without.
Note that setitimer/alarm probably are unix/-like specific.
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
volatile sig_atomic_t counter;
volatile sig_atomic_t stop;
void alarm_handler(int signal)
{
printf("%d\n", counter++);
if ( counter > stop )
{
exit(0);
}
}
int main(int argc, char **argv)
{
struct itimerval v;
v.it_value.tv_sec = 0;
v.it_value.tv_usec = 5000;
v.it_interval.tv_sec = 0;
v.it_interval.tv_usec = 5000;
int pipefds[2];
char b;
stop = 10;
counter = 1;
pipe(pipefds);
signal(SIGALRM, alarm_handler);
setitimer(ITIMER_REAL, &v, NULL);
read(pipefds[0], &b, 1);
}
I'm very disappointed that this doesn't work. To me, the phrase "a function is called after any previously registered functions that had already been called at the time it was registered" suggests that it is possible to register atexit handlers after they have started to be called. That is, a handler can register another handler. Otherwise, how is it even possible for there to exist a function which has been called at the time another function is registered? But for me the call to atexit is returning 0 success, but not actually resulting in another call. Anyone know why, have I made some silly error?
#include "stdio.h"
#include "stdlib.h"
int count = 0;
int limit = 10;
void handler() {
printf("%d of %d\n", ++count, limit);
if (count < limit) atexit(handler);
}
int main(int argc, char **argv) {
if (argc > 1) limit = atoi(argv[1]);
atexit(handler);
}
By the way, not recursion because atexit doesn't call its parameter, it queues it to be called later. Obviously the C runtime contains a loop to call atexit handlers, but that loop exists whether you actually register any atexit handlers or not. So if this program contains a loop, so does every C program ;-)
@Kjetil Jorgensen
I am searching for the answer why "read(pipefds[0], &b, 1);" is required?
Could you please explain or point me to some Doc ?
This takes the integer N from the command line and prints out from 1 to N
#include <stdio.h>
#include <stdlib.h>
int total;
int N;
int print16(int n)
{
printf("%d\n",n+0x01); total++; if (total >= N) exit(0);
printf("%d\n",n+0x02); total++; if (total >= N) exit(0);
printf("%d\n",n+0x03); total++; if (total >= N) exit(0);
printf("%d\n",n+0x04); total++; if (total >= N) exit(0);
printf("%d\n",n+0x05); total++; if (total >= N) exit(0);
printf("%d\n",n+0x06); total++; if (total >= N) exit(0);
printf("%d\n",n+0x07); total++; if (total >= N) exit(0);
printf("%d\n",n+0x08); total++; if (total >= N) exit(0);
printf("%d\n",n+0x09); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0A); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0B); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0C); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0D); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0E); total++; if (total >= N) exit(0);
printf("%d\n",n+0x0F); total++; if (total >= N) exit(0);
printf("%d\n",n+0x10); total++; if (total >= N) exit(0);
}
int print256(int n)
{
print16(n);
print16(n+0x10);
print16(n+0x20);
print16(n+0x30);
print16(n+0x40);
print16(n+0x50);
print16(n+0x60);
print16(n+0x70);
print16(n+0x80);
print16(n+0x90);
print16(n+0xA0);
print16(n+0xB0);
print16(n+0xC0);
print16(n+0xD0);
print16(n+0xE0);
print16(n+0xF0);
}
int print4096(int n)
{
print256(n);
print256(n+0x100);
print256(n+0x200);
print256(n+0x300);
print256(n+0x400);
print256(n+0x500);
print256(n+0x600);
print256(n+0x700);
print256(n+0x800);
print256(n+0x900);
print256(n+0xA00);
print256(n+0xB00);
print256(n+0xC00);
print256(n+0xD00);
print256(n+0xE00);
print256(n+0xF00);
}
int print65536(int n)
{
print4096(n);
print4096(n+0x1000);
print4096(n+0x2000);
print4096(n+0x3000);
print4096(n+0x4000);
print4096(n+0x5000);
print4096(n+0x6000);
print4096(n+0x7000);
print4096(n+0x8000);
print4096(n+0x9000);
print4096(n+0xA000);
print4096(n+0xB000);
print4096(n+0xC000);
print4096(n+0xD000);
print4096(n+0xE000);
print4096(n+0xF000);
}
int print1048576(int n)
{
print65536(n);
print65536(n+0x10000);
print65536(n+0x20000);
print65536(n+0x30000);
print65536(n+0x40000);
print65536(n+0x50000);
print65536(n+0x60000);
print65536(n+0x70000);
print65536(n+0x80000);
print65536(n+0x90000);
print65536(n+0xA0000);
print65536(n+0xB0000);
print65536(n+0xC0000);
print65536(n+0xD0000);
print65536(n+0xE0000);
print65536(n+0xF0000);
}
int print16777216(int n)
{
print1048576(n);
print1048576(n+0x100000);
print1048576(n+0x200000);
print1048576(n+0x300000);
print1048576(n+0x400000);
print1048576(n+0x500000);
print1048576(n+0x600000);
print1048576(n+0x700000);
print1048576(n+0x800000);
print1048576(n+0x900000);
print1048576(n+0xA00000);
print1048576(n+0xB00000);
print1048576(n+0xC00000);
print1048576(n+0xD00000);
print1048576(n+0xE00000);
print1048576(n+0xF00000);
}
int print268435456(int n)
{
print16777216(n);
print16777216(n+0x1000000);
print16777216(n+0x2000000);
print16777216(n+0x3000000);
print16777216(n+0x4000000);
print16777216(n+0x5000000);
print16777216(n+0x6000000);
print16777216(n+0x7000000);
print16777216(n+0x8000000);
print16777216(n+0x9000000);
print16777216(n+0xA000000);
print16777216(n+0xB000000);
print16777216(n+0xC000000);
print16777216(n+0xD000000);
print16777216(n+0xE000000);
print16777216(n+0xF000000);
}
int print2147483648(int n)
{
/*
* Only goes up to n+0x70000000 since we
* deal only with postive 32 bit integers
*/
print268435456(n);
print268435456(n+0x10000000);
print268435456(n+0x20000000);
print268435456(n+0x30000000);
print268435456(n+0x40000000);
print268435456(n+0x50000000);
print268435456(n+0x60000000);
print268435456(n+0x70000000);
}
int main(int argc, char *argv[])
{
int i;
if (argc > 1) {
N = strtol(argv[1], NULL, 0);
}
if (N >=1) {
printf("listing 1 to %d\n",N);
print2147483648(0);
}
else {
printf("Must enter a postive integer N\n");
}
}
Another thingy (on linux) would be to do as below where 7 is N
int main() {
return system("seq 7");
}
/// <summary>
/// Print one to Hundred without using any loop/condition.
/// </summary>
int count = 100;
public void PrintOneToHundred()
{
try
{
int[] hey = new int[count];
Console.WriteLine(hey.Length);
count--;
PrintOneToHundred();
}
catch
{
Console.WriteLine("Done Printing");
}
}