some good problem

This is a discussion on some good problem within the C Programming forums, part of the General Programming Boards category; Hi all In a colelge test we were asked to write a Program that prints nums from 1 - 100 ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    22

    some good problem

    Hi all
    In a colelge test we were asked to write a Program that prints nums from 1 - 100 and 100 - 1 without using loops( if, while, or). Am unable to think a good solution to it.

    any punters in this groups got solutions ?

    TIA
    ~codo

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Look up recursion.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by codomaniac
    Hi all
    In a colelge test we were asked to write a Program that prints nums from 1 - 100 and 100 - 1 without using loops( if, while, or). Am unable to think a good solution to it.

    any punters in this groups got solutions ?

    TIA
    ~codo
    if is not a loop.
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    22
    Oh yes right. Bad connection @ my place means typos while posting hastengly. Yes IF is not of LOOPing category.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Use a whole lot of copying and pasting.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    And I assume you can figure out the way for downward So I guessed you missed that question Unless you used XSquared's version

    Code:
    #include <stdio.h>
    
    void print_up(int num){
        if(num<=100){
                 printf("%d\n",num);
                 num++;
                 print_up(num);
         }
    }
    
    int main(void){
        int i=1;
        print_up(i);
        return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int print_up( int num )
    {
        return num < 101 ? printf("%d\n", num ) + print_up( num + 1 ) : 0;
    }
    Slight variation to the above...

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    TO: codomaniac. This is a tertinary opertor the same as an if statement
    Code:
    (x>22) ? printf("%d",x) : exit(0);
    BUT
    Code:
    int print_up( int num )
    {
        return num < 101 ? printf("%d\n", num ) +  print_up( num + 1 ) : 0;
    }
    How does that + sign work?? Is it the same as a semicolon or something
    Last edited by linuxdude; 05-29-2004 at 07:49 PM.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    it works just like normal addition. printf() returns the number of characters outputted. So it'll add that value to the return of print_up

  10. #10
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by linuxdude
    TO: codomaniac. This is a tertinary opertor the same as an if statement
    Code:
    (x>22) ? printf("%d",x) : exit(0);
    BUT
    Code:
    int print_up( int num )
    {
        return num < 101 ? printf("%d\n", num ) +  print_up( num + 1 ) : 0;
    }
    How does that + sign work?? Is it the same as a semicolon or something
    Quzah wants is to evaluate both printf and print_up in one statement . By returning their sum he does both, he could also have use the comma operator. Not a pretty code, but it gets the work done.
    The one who says it cannot be done should never interrupt the one who is doing it.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, it does what it's supposed to do. It adds 1, the return value of printf, to the return value of all the other function calls. Thus, you could modify it slightly, and have it take two arguments, and return to the origional caller the number of actual printed lines:
    Code:
    int print_up( int to, int num )
    {
        return num < to ? printf("%d\n", num ) + print_up( to, num + 1 ) : 0;
    }
    The comma operator wouldn't do what I had in mind when I wrote it. I wrote it to do exactly what it does for a reason. If you wanted a better syntax for it, you'd reverse the two parameters, thus it would be:
    Code:
    print_up( 1, 400 );
    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    > printf("%d\n", num ) + print_up( to, num + 1 )
    But this assumes an order of evaluation which C does not guarantee
    The compiler could easily call print_up() before printf(), then perform the addition.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why should the order matter? Either way you're getting a return value from both functions. Both return values are added together, and returned at the same time. I can't see how the order has any effect. It doesn't matter if printf or print_up is evaluated first. The end result is:

    return( valueN + valueX )

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,530
    > 1) isn't the + operator reading from left to right?
    Only the compiler parser reads it from left to right
    By the time the compiler code generator and optimiser have finished with it, who knows.
    So long as it still conforms to the rules of precedence, it should not matter which order your operands are evaluated in.
    3.4 3.5 3.8

    Any decent C book should explain order of evaluation, in particular that && and || guarantees it, and pretty much everything else doesn't.

    > what would the problem be if print_up() was called befor printf() ?
    It would then be called print_down()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    C does not guartnee the order in which functions in the same command are called.

    for example:
    given
    Code:
    int x(void);
    int y(void);
    than
    Code:
    int z = x() + y()
    could be compiled as (quasi psudo here)
    Code:
    int hidden_temp_variable = x();
    int z = hidden_temp_variable + y();
    Code:
    int hidden_temp_variable = y();
    int z = x() + hidden_temp_variable;
    Which one is used is up to the compiler to decide.

    edit: Dang I should stop queuing up messages when I'm gonna take my time on them

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-25-2006, 11:25 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21