Help explaining output

This is a discussion on Help explaining output within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> void a(int num); void b(int num); int main() { a(6); return 0; } void a(int num) { ...

  1. #1
    Registered User alpine's Avatar
    Join Date
    Oct 2010
    Posts
    6

    Help explaining output

    Code:
    #include <stdio.h>
    
    void a(int num);
    void b(int num);
    
    int main() {
         a(6);
         return 0;
    }
    
    void a(int num) {
         int index;
         for (index=0; index<num; index++)
              b(index);
    }
    void b(int num) {
         int index=0;
         for (index=0; index<num; index++)
              printf("%d", index);
    }
    Can someone explain to me why the out put of this is '001012012301234'? My professor didn't do a very good job -_-

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Isn't it obvious? For integer N > 0:
    a(N) prints: 001:012:0123:01234:...:012...(N-3)(N-2)
    Devoted my life to programming...

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's a glorified nested loop, and adding newlines helps see where the breaks are:
    Code:
    int index, index2;
    
    for (index = 0; index < num; index++) {
        for (index2 = 0; index2 < index; index2++)
            printf("%d", index2);
    
        printf("\n");
    }
    For each iteration of the outer loop, you print all numbers from 0 to the current index of the outer loop:
    Code:
    0
    01
    012
    0123
    01234
    ...
    >Isn't it obvious?
    As amazing as it may seem to you, most things are not obvious when one is a beginner.
    My best code is written with the delete key.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Sorry, wrong answer. You ask why. Well it is because the bigger integer passed to b() is N-1 and the bigger integer printed by b() is N-2.
    Devoted my life to programming...

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Quote Originally Posted by Prelude View Post
    >Isn't it obvious?
    As amazing as it may seem to you, most things are not obvious when one is a beginner.
    I know, i've been a beginner before... Sorry, it just came so naturally to me that i forgot how confusing things can be when a newbie.
    Devoted my life to programming...

  6. #6
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    That code is very confusing. Maybe you should rename your variables. You have two different variables named index and num. What's up with that?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alpine View Post
    Code:
    #include <stdio.h>
    
    void a(int num);
    void b(int num);
    
    int main() {
         a(6);
         return 0;
    }
    
    void a(int num) {
         int index;
         for (index=0; index<num; index++)
              b(index);
    }
    void b(int num) {
         int index=0;
         for (index=0; index<num; index++)
              printf("%d", index);
    }
    Can someone explain to me why the out put of this is '001012012301234'? My professor didn't do a very good job -_-
    Your best bet is to become a code tracer... follow the values of the variables through each iteration of the loops in each function... you'll figure it out.

    example:
    pass 1... a (6) -> a num = 6 -> a index = 0 -> b index = 0 -> print 0
    pass 2... a index = 0 -> b num = 0 -> b index = 1 -> return
    pass 3 ... a index = 1 -> b num = 1 -> b index = 0 -> print 0
    pass4 ... a index = 1 -> b num = 1 -> b index = 1 -> print 1

    and so on...

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