Thread: where is my mistake in undersanding the order of this code..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    where is my mistake in undersanding the order of this code..

    when i compile it i get perfectly well output
    without any endless recursion

    but when i trace this code:
    Code:
    #include <stdio.h>
    int a(int n,int count){
          int i;
          for(i=0;i<n;i++)
               count =a(i,count);           <<<<<<<<<<the problematic line
               return count+1;
    }
    
    int b(int n,int count) {
      int i;
      count =a(n,count);
         for(i=0;i<n;i++)
         count =b(i,count);
         return count;
      }
    
      int main (){
        int i;
        for (i=0;i<4;i++)
          printf("%d",a(i,0));
          printf("\n%d\n",a(i,10));
    
          for (i=0;i<4;i++)
            printf("%d",b(i,0));
            printf("\n%d\n",b(i,10));
    return 0;
      }

    Code:
    Main:
    for (i=0;i<4;i++)             i=0
    printf("%d",a(0,0));
    
    a:  
    
     n=0 count=0
    int a(0,0){
    for(i=0;i<n;i++)
        count =a(i,count);  a=0 count=0
    it will always call a again and again there is no end to this recurtion

    ???????

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    yes there is. Take a look at n and how it's used. Especially with respect to i (look at the relationship between n and i in a() for example).

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    ahhhh it will not get into the for loop
    thanks

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the best way to solve this question
    is there a way to predict the final out put
    or should i go line by line
    which by the recursive loops it will take me all the time from the test

    is there any less time consuming method ??

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course there's a better method. Breakpoints.
    A good debugger can allow you to put a breakpoint which will break when a certain condition is true. I know Visual Studio has one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i meant when i only have a pen and paper and 3 hours to solve 6 questions

    and this task is only third of a question

    how to act?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use your brain, simply put.
    Walk through the code if necessary. Write notes on your paper. Figure out how the code will act.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is there some manual for solving this kind of stuff

    because if i will write down every line each time it executes
    it takes to much time

    is there any manual???

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, there is no manual.
    There is only your abilities to analyze the flow of code.
    Which, quite frankly, is a very, very important ability.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by transgalactic2 View Post
    is there any manual???
    No manual, just practice. With time it'll become easier and easier to follow the flow of code. So just make sure you practice and you should be fine.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Replies: 6
    Last Post: 09-07-2004, 11:06 PM
  4. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM