Why is this the output?......

This is a discussion on Why is this the output?...... within the C++ Programming forums, part of the General Programming Boards category; Ok, I have 2 array programs here which are examples from the deitel C++ book. I was studying the examples ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    Why is this the output?......

    Ok, I have 2 array programs here which are examples from the deitel C++ book. I was studying the examples and I just dont know why they produce their certain output. I am lost and stressed. If anyone can tell me why the output is such for these 2 programs, I'd really appreciate it, thanks.

    // Ex. 4.21: ex04_21.cpp
    // What does this program do?
    #include <iostream>

    using namespace std;

    void someFunction( int [], int, int ); // function prototype

    int main()
    {
    const int arraySize = 10;
    int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    cout << "The values in the array are:" << endl;
    someFunction( a, 0, arraySize );
    cout << endl;

    return 0; // indicates successful termination

    } // end main

    // What does this function do?
    void someFunction( int b[], int current, int size )
    {
    if ( current < size ) {
    someFunction( b, current + 1, size );
    cout << b[ current ] << " ";
    }

    Why does this code produce the array contents going from 10 to 1 and not the other way around, and why would the array even be initialized to 10 in the first place, isnt the correct manner supposed to be from 0-9?


    Here is the other program:

    #include <iostream>

    using namespace std;

    int whatIsThis( int [], int ); // function prototype

    int main()
    {
    const int arraySize = 10;
    int a[ arraySize ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    int result = whatIsThis( a, arraySize ); //passing in array and size

    cout << "Result is " << result << endl;

    return 0; // indicates successful termination

    } // end main

    // What does this function do?
    int whatIsThis( int b[], int size )
    {
    // base case
    if ( size == 1 )
    return b[ 0 ];

    // recursive step
    else
    return b[ size - 1 ] + whatIsThis( b, size - 1 );

    } // end function whatIsThis


    Hmmm the return b[size-1] + whatIsThis(b,size-1) statement baffles me. How does that statement make all the elements in the array add to each other recursively?


    *sighs* any help would be highly appreciated. Thanks.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Oi, i forgot to write down the outputs.

    For the first one the output is:

    Result is 55



    For the second program the output is:

    The values in the array are:
    10 9 8 7 6 5 4 3 2 1


    thanks.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Please use the code tags
    see the "read before posting" thread for details
    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.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    somefunction() is recursive because it calls itself over and over again until current == size. Since the output statement comes after the recursive call, there will be no output until current == size. When current == size the if conditional is false, so the body of the if statement isn't entered, meaning the recursive call isn't seen, meaning the current instance of somefunction will return void without printing anything to the screen an effectively handing the control back to the instance of somefuntion() where current == 9. That instance of somefuntion() will then print b[9], which is 10, to the screen. That will complete the if statement in this instance of somefunction() so it returns void, handing the control back to the instance where current == 8, which prints b[8], which is 9, to the screen, etc. The end result is printing the values in the array called b, which is the same as the array called a, backward. To print the values in ascending order by index, place the cout statement before the recursive call in the body of somefunction().

    whatisthis() is also a recursive function, but this time a base case is identified, whereas in somefunction() no base case was used. the last line of whatisthis() contains a recursive call to itself. That means no instance of whatisthis() will actually return anything until size == 1, which is the base case. The base case doesn't contain a recursive call so it just returns b[0] which is 1 to the instance of whatisthis() where size == 2, which can then return b[1] + 1, which is the same as returning 2 + 1 or 3 to the instance of whatisthis() where size == 3 which will return the value of 6 to the instance of whatisthis() where size == 4, etc. The last return from all the instances of whatisthis() is assigned to the variable called result which is then displayed to the screen as 55.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    Look at this:

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    void rekursion( int n )
    {
        if( n>1 )
        {
          rekursion( n-1 ); 
        }
        cout << "Rekursion: " << n << endl;
    }
    
    int main()
    {
        for( int i=0; i<10; ++i )
        {
          cout << "Iteration: " << i+1 << endl;
        }
        cout << endl;
        rekursion(10);
        
        getch();
    }
    and change the function to:

    Code:
    void rekursion( int n )
    {
      cout << "Rekursion: " << n << endl;
      if( n>1 )
      {
          rekursion( n-1 ); 
      }
    }

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Thank you both very much for your help. I think I have it now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 03:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM

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