can we write program without loops

S

Saurabh Saxena

can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.
 
G

gabriel

Saurabh said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

What, is your whole class coming to the NG for help? :)

Use recursion -- 'Nuff said
 
J

jacob navia

Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

OK OK Here it is:

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

int main(int argc,char *argv[])
{
greaterZero(atoi(argv[1]));
return 0;
}

d:\lcc\problem> lcc pb.c
d:\lcc\problem> pb 10

10
9
8
7
6
5
4
3
2
1

I tried to avoid any warnings. A version with error checking
follows...
 
J

jacob navia

Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

As promised, here is a version with error
checking

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

void errormessage(int);
void Continue(int);

callback errortable[2] = {
errormessage,
Continue
};

void errormessage(int a)
{
printf("Wrong input\nUsage:\npb n, where n>0\n");
exit(1);
}

void Continue(int a)
{
}


int main(int argc,char *argv[])
{
int a;

errortable[argc>1](1);
a = atoi(argv[1]);
errortable[a >0](a);
greaterZero(atoi(argv[1]));
return 0;
}
 
N

nrk

Christopher said:
How will you stop recursing without using conditionals?

No. The thing forbidden is the use of the conditional operator (?:).
You're allowed to use logical operators (I assume).

#include <stdio.h>

void print(int n) {
n && (print(n-1), 1) && printf("%d\n", n);
}

int main(void) {
print(10);
return 0;
}

-nrk.
 
J

Joona I Palaste

Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

#include <stdio.h>
int main(void) {
char input[100];
scanf("%100s", input);
printf("1 to %s\n", input);
return 0;
}
 
N

nrk

Joona said:
Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

#include <stdio.h>
int main(void) {
char input[100];
scanf("%100s", input);
printf("1 to %s\n", input);
return 0;
}

Possible UB.

-nrk.
 
E

Ed Morton

Saurabh said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

I can't imagine any possibile way that this is not a homework
assignment, which begs the question "Why are so many of us immediately
providing solutions instead of guidance?".

Ed.
 
J

Joona I Palaste

I can't imagine any possibile way that this is not a homework
assignment, which begs the question "Why are so many of us immediately
providing solutions instead of guidance?".

Why are so many of us not trying to find loopholes in the posted
assignment description that allow for solutions that technically fulfil
the posted requirements, but are useless as real homework solutions?
 
D

Derk Gwen

(e-mail address removed) (Saurabh Saxena) wrote:
# can we write the program to write no 1 to n without using
# switch,do,while,for,goto,if and conditional operator where n will be
# input by user.

As explained elsewhere, there is old trick from computational theory
where you map the logical values true and false into integers 0 and 1
(or 1 and 0), you then use the integer as an index into a pair of
of lambda expressions. Select one lambda expression and apply an
argument to it.

Looping is in fact an optimisation of the more general and powerful
concept of recursion. With if-then-else, while-do, and composition
operators, it's not that difficult to express other operators.

Like for = \initial,predicate,step,statement.
join(\.initial,while(\.predicate,\.join(statement,step)))
 
P

pete

That's not what "begs the question" means.

Anyone willing to study, can be helped by examples of working code.
When someone does poorly in their exams, because they
just only copied their homework, that's good too.
Why are so many of us not trying to find loopholes in the posted
assignment description that allow for solutions that
technically fulfil
the posted requirements, but are useless as real homework solutions?

Both the assignment and it's presentation, are too boring.
 
K

Kenneth Brody

Christopher said:
How will you stop recursing without using conditionals?

By dividing by (number-n) after printing the number. ;-)

Hopefully, the nasal demons will know how to stop the program for you.

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+X-Mozilla-Status: 0009------------+-----------------------------+
 
K

Kenneth Brody

jacob said:
Saurabh Saxena said:
can we write the program to write no 1 to n without using ^^^^^^
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

OK OK Here it is: [... snip source ...]
d:\lcc\problem> lcc pb.c
d:\lcc\problem> pb 10

10
9 [...]
2
1

I tried to avoid any warnings. A version with error checking
follows...

Sorry. If the OP hands this in for his homework assignment, he'll
fail[1], since the assignment was to show "1 to n" not "n to 1".


[1] Not that that's necessarily a bad thing. ;-)

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
S

Sean Kenwrick

jacob navia said:
Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

OK OK Here it is:

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

Isn't a>0 a conditional expression? Also your program counts down
instead of up. Here's your program modified to remove the conditional
expression and to count up as stated by the OP..

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

int orig_n;

callback callbackTable[3];

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",orig_n-a+1);
a--;
callbackTable[(a+1)%2 + (a-1)%2]=greaterZero;
callbackTable[a%2]=zero;
callbackTable[(a+1)%2 + (a-1)%2](a);
}

int main(int argc,char *argv[])
{
orig_n=atoi(argv[1]);
greaterZero(orig_n);
return 0;
}



It relies on the fact that the sign of (a-1)%2 changes when a becomes equal
to 0. Notice that in this version I have 3 entries in the callback table
of which only entries 0 and 2 are used....

Sean
 
S

Sean Kenwrick

Sean Kenwrick said:
jacob navia said:
Saurabh Saxena said:
can we write the program to write no 1 to n without using
switch,do,while,for,goto,if and conditional operator where n will be
input by user.

OK OK Here it is:

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

callback callbackTable[2] = {
zero,
greaterZero
};

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",a--);
callbackTable[a>0](a);
}

Isn't a>0 a conditional expression? Also your program counts down
instead of up. Here's your program modified to remove the conditional
expression and to count up as stated by the OP..

#include <stdio.h>
#include <stdlib.h>

typedef void (*callback)(int);
void zero(int);
void greaterZero(int);

int orig_n;

callback callbackTable[3];

void zero(int a)
{
exit(0);
}

void greaterZero(int a)
{
printf("%d\n",orig_n-a+1);
a--;
callbackTable[(a+1)%2 + (a-1)%2]=greaterZero;
callbackTable[a%2]=zero;
callbackTable[(a+1)%2 + (a-1)%2](a);
}

int main(int argc,char *argv[])
{
orig_n=atoi(argv[1]);
greaterZero(orig_n);
return 0;
}



It relies on the fact that the sign of (a-1)%2 changes when a becomes equal
to 0. Notice that in this version I have 3 entries in the callback table
of which only entries 0 and 2 are used....

Sean

Actually the callback table can be shrunk back to 2 entries simply by
dividing by 2:

callbackTable[((a+1)%2 + (a-1)%2)/2]=greaterZero;

From this it seems that any program can be re-written without requiring any
conditional expressions at all (ie no ==, <=, >=, !, >, < etc) since you can
reduce this to setting up the callback table in advance and doing some
simple subtraction and modulo arithmetic to index to the callback table to
handle each of the comparison types accordingly. Its not so clear how &&
and || would be handled though... any suggestions??

Sean
 
J

jacob navia

Isn't a>0 a conditional expression?

No. It is a boolean expression.

I can write

int a = 2;
int b = (a == 2);

Now b contains 1.

But I agree with you that it could print from 1 to n instead of
n to 1. A rearranging of the arguments makes it possible but
I think the idea is enough for the O.P.

jacob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top