can we write program without loops

Discussion in 'C Programming' started by Saurabh Saxena, Feb 5, 2004.

  1. 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.
     
    Saurabh Saxena, Feb 5, 2004
    #1
    1. Advertising

  2. Saurabh Saxena

    gabriel Guest

    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
     
    gabriel, Feb 5, 2004
    #2
    1. Advertising

  3. gabriel <> 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.
     
    Christopher Benson-Manica, Feb 5, 2004
    #3
  4. On Thu, 5 Feb 2004 16:44:12 +0000 (UTC), Christopher Benson-Manica
    <> wrote:
    >gabriel <> 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
     
    Kevin D. Quitt, Feb 5, 2004
    #4
  5. Saurabh Saxena

    jacob navia Guest

    "Saurabh Saxena" <> wrote in message
    news:...
    > 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...
     
    jacob navia, Feb 5, 2004
    #5
  6. Saurabh Saxena

    jacob navia Guest

    "Saurabh Saxena" <> wrote in message
    news:...
    > 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;
    }
     
    jacob navia, Feb 5, 2004
    #6
  7. Saurabh Saxena

    nrk Guest

    Christopher Benson-Manica wrote:

    > gabriel <> 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
     
    nrk, Feb 5, 2004
    #7
  8. Saurabh Saxena <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To know me IS to love me."
    - JIPsoft
     
    Joona I Palaste, Feb 5, 2004
    #8
  9. Saurabh Saxena

    nrk Guest

    Joona I Palaste wrote:

    > Saurabh Saxena <> 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
     
    nrk, Feb 5, 2004
    #9
  10. Saurabh Saxena

    Ed Morton Guest

    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.
     
    Ed Morton, Feb 5, 2004
    #10
  11. Ed Morton <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "To err is human. To really louse things up takes a computer."
    - Anon
     
    Joona I Palaste, Feb 5, 2004
    #11
  12. Saurabh Saxena

    Derk Gwen Guest

    (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!
     
    Derk Gwen, Feb 5, 2004
    #12
  13. Christopher Benson-Manica wrote:
    > How will you stop recursing without using conditionals?


    <news:>
     
    Jeremy Yallop, Feb 5, 2004
    #13
  14. Saurabh Saxena

    pete Guest

    Joona I Palaste wrote:
    >
    > Ed Morton <> 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
     
    pete, Feb 5, 2004
    #14
  15. Saurabh Saxena

    Ed Morton Guest

    pete wrote:
    > Joona I Palaste wrote:
    >
    >>Ed Morton <> 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.
     
    Ed Morton, Feb 5, 2004
    #15
  16. Christopher Benson-Manica wrote:
    >
    > gabriel <> 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------------+-----------------------------+
     
    Kenneth Brody, Feb 5, 2004
    #16
  17. jacob navia wrote:
    >
    > "Saurabh Saxena" <> wrote in message
    > news:...
    > > 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." |
    +---------+----------------------------------+-----------------------------+
     
    Kenneth Brody, Feb 5, 2004
    #17
  18. "jacob navia" <> wrote in message
    news:bvtskl$4h4$...
    >
    > "Saurabh Saxena" <> wrote in message
    > news:...
    > > 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
     
    Sean Kenwrick, Feb 6, 2004
    #18
  19. "Sean Kenwrick" <> wrote in message
    news:bvult5$bgo$...
    >
    > "jacob navia" <> wrote in message
    > news:bvtskl$4h4$...
    > >
    > > "Saurabh Saxena" <> wrote in message
    > > news:...
    > > > 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
     
    Sean Kenwrick, Feb 6, 2004
    #19
  20. Saurabh Saxena

    jacob navia Guest

    > 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
     
    jacob navia, Feb 6, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. could ildg
    Replies:
    0
    Views:
    477
    could ildg
    Jul 1, 2005
  2. Jeff Epler
    Replies:
    0
    Views:
    406
    Jeff Epler
    Jul 1, 2005
  3. could ildg
    Replies:
    0
    Views:
    514
    could ildg
    Jul 2, 2005
  4. Spidey
    Replies:
    37
    Views:
    1,283
    Keith Thompson
    Dec 6, 2005
  5. Me
    Replies:
    2
    Views:
    254
Loading...

Share This Page