Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > can we write program without loops

Reply
Thread Tools

can we write program without loops

 
 
Saurabh Saxena
Guest
Posts: n/a
 
      02-05-2004
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.
 
Reply With Quote
 
 
 
 
gabriel
Guest
Posts: n/a
 
      02-05-2004
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
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-05-2004
gabriel <(E-Mail Removed)> 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.
 
Reply With Quote
 
Kevin D. Quitt
Guest
Posts: n/a
 
      02-05-2004
On Thu, 5 Feb 2004 16:44:12 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote:
>gabriel <(E-Mail Removed)> 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
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-05-2004

"Saurabh Saxena" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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...



 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-05-2004

"Saurabh Saxena" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;
}



 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      02-05-2004
Christopher Benson-Manica wrote:

> gabriel <(E-Mail Removed)> 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
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      02-05-2004
Saurabh Saxena <(E-Mail Removed)> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To know me IS to love me."
- JIPsoft
 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      02-05-2004
Joona I Palaste wrote:

> Saurabh Saxena <(E-Mail Removed)> 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
 
Reply With Quote
 
Ed Morton
Guest
Posts: n/a
 
      02-05-2004


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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
How can to write a c program without a main() Spidey C Programming 37 12-06-2005 12:24 AM
Re: It seems that ZipFile().write() can only write files,how can empty directories be put into it? could ildg Python 0 07-02-2005 08:31 AM
Re: It seems that ZipFile().write() can only write files,how can empty directories be put into it? Jeff Epler Python 0 07-01-2005 02:06 PM
It seems that ZipFile().write() can only write files,how can empty directories be put into it? could ildg Python 0 07-01-2005 01:50 AM



Advertisments