473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

can we write program without loops

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.
Nov 14 '05 #1
22 4509
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.


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

Use recursion -- 'Nuff said

--
gabriel
Nov 14 '05 #2
gabriel <no@no--spam.com> spoke thus:
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.

Use recursion -- 'Nuff said


How will you stop recursing without using conditionals?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
On Thu, 5 Feb 2004 16:44:12 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
gabriel <no@no--spam.com> spoke thus:
Use recursion -- 'Nuff said


How will you stop recursing without using conditionals?


Short-circuiting.

--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #4

"Saurabh Saxena" <sa******@vit.ac.in> wrote in message
news:30**************************@posting.google.c om...
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...

Nov 14 '05 #5

"Saurabh Saxena" <sa******@vit.ac.in> wrote in message
news:30**************************@posting.google.c om...
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;
}

Nov 14 '05 #6
nrk
Christopher Benson-Manica wrote:
gabriel <no@no--spam.com> spoke thus:
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.

Use recursion -- 'Nuff 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.

--
Remove devnull for email
Nov 14 '05 #7
Saurabh Saxena <sa******@vit.ac.in> scribbled the following:
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;
}

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
Nov 14 '05 #8
nrk
Joona I Palaste wrote:
Saurabh Saxena <sa******@vit.ac.in> scribbled the following:
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.
--
Remove devnull for email
Nov 14 '05 #9


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.


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.

Nov 14 '05 #10
Ed Morton <mo****@lsupcaemnt.com> scribbled the following:
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.
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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To err is human. To really louse things up takes a computer."
- Anon
Nov 14 '05 #11
sa******@vit.ac.in (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)))
--
Derk Gwen http://derkgwen.250free.com/html/index.html
I ASSURE YOU WE'RE OPEN!
Nov 14 '05 #12
Christopher Benson-Manica wrote:
How will you stop recursing without using conditionals?


<news:sl*******************@hehe.cl.cam.ac.uk>

Nov 14 '05 #13
Joona I Palaste wrote:

Ed Morton <mo****@lsupcaemnt.com> scribbled the following:
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.
I can't imagine any possibile way that this is not a homework
assignment, which begs the question

That's not what "begs the question" means.
"Why are so many of us
immediately providing solutions instead of guidance?".


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.

--
pete
Nov 14 '05 #14


pete wrote:
Joona I Palaste wrote:
Ed Morton <mo****@lsupcaemnt.com> scribbled the following:
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.

I can't imagine any possibile way that this is not a homework
assignment, which begs the question

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


s/begs/raises.

Sorry for the conufsion caused.

Ed.

Nov 14 '05 #15
jacob navia wrote:

"Saurabh Saxena" <sa******@vit.ac.in> wrote in message
news:30**************************@posting.google.c om...
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." |
+---------+----------------------------------+-----------------------------+
Nov 14 '05 #16
Christopher Benson-Manica wrote:

gabriel <no@no--spam.com> spoke thus:
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.

Use recursion -- 'Nuff 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------------+-----------------------------+
Nov 14 '05 #17

"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:bv**********@news-reader3.wanadoo.fr...

"Saurabh Saxena" <sa******@vit.ac.in> wrote in message
news:30**************************@posting.google.c om...
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
Nov 14 '05 #18

"Sean Kenwrick" <sk*******@hotmail.com> wrote in message
news:bv**********@titan.btinternet.com...

"jacob navia" <ja***@jacob.remcomp.fr> wrote in message
news:bv**********@news-reader3.wanadoo.fr...

"Saurabh Saxena" <sa******@vit.ac.in> wrote in message
news:30**************************@posting.google.c om...
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


Nov 14 '05 #19
> 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

Nov 14 '05 #20
jacob navia wrote:
Isn't a>0 a conditional expression?


No. It is a boolean expression.


Wrong. It's a relational expression. See the grammar.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #21


If you use *conditionals* (if,...), yes, create a recursive function using a
global variable as a counter. If you dont mind a never-ending loop then its
possible. Something like:

int n=0;
int m=0;

void main() {
scanf("%d",m);
printn();
}
void printn() {
n++;
printf("1 to %d",n);
printn();
}

The above code is not perfect - its just an example I cooked up thats
definitely comes packaged with plenty of bugs - its purpose is only to
understand the logic. If you want to eliminate that too, put the code in a
header file and call it. Going further still, you can pass control to
another application!

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 dont like bottom-posting!!!
If you use an email client or read the NGs using Google then you can see the
whole thread so why bother with bottom-posting? Besides, if we were all
meant to bottom-post, NG clients would automatically start at the end of the
original post.
Nov 14 '05 #22
"Nitin" <k_*******@yahoo.co.in> wrote in message
news:c1*********@news-dxb.emirates.net.ae...
!!!I dont like bottom-posting!!!
And I "dont" like wrong apostrophication ;-)
If you use an email client or read the NGs using Google then you can see the whole thread so why bother with bottom-posting?
An email client to read NGs? Yes, /some/ email lients double as news
clients, but OE does not define Usenet. Besides, posts are not guaranteed to
turn up in any specific order, or indeed at all. I need to post a correction
to my own reply to someone that I sent on Friday, and I've been desperately
waiting it to turn up ever since.
Besides, if we were all
meant to bottom-post, NG clients would automatically start at the end of the original post.


See above re OE and Usenet. Positioning you at the bottom of the post is
also not ideal, because the news client does not know which parts of the
original message you are going to reply to.

Peter (trying to stop using OE but being too lazy)
Nov 14 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ron Adam | last post by:
Hi, I'm having fun learning Python and want to say thanks to everyone here for a great programming language. Below is my first Python program (not my first program) and I'd apreciate any...
12
by: Steven Bethard | last post by:
So I need to do something like: for i in range(len(l)): for j in range(i+1, len(l)): # do something with (l, l) where I get all pairs of items in a list (where I'm thinking of pairs as sets,...
1
by: Starx | last post by:
I have a while loop in my program that is going to execute a very large number of loops (well over 100000000). Sometimes this can take quite some time for the computer to process so I'd like the...
88
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
7
by: Gernot Frisch | last post by:
Hi, I want to have a representative information about the read and write speeds of a drive (cf-card, HDD and RAM-Disk). I think the buffering of the filesystem must be fooled somehow for my...
18
by: HYRY | last post by:
I want to join two mono wave file to a stereo wave file by only using the default python module. Here is my program, but it is much slower than the C version, so how can I increase the speed? I...
3
by: kalyankiran33 | last post by:
I have com across a program in which I should take 17 as a global constant. I have to take 17 inputs. for those inputs .. I have to find the number of even numbers entered the number of odd...
3
by: jm.suresh | last post by:
I am trying to measure the time the processor spends on some operation, and I want this measure to not depend on the current load of the machine. But doing the following prints different values...
12
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.