Thread: problem with printing elements of a array using recursion program

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    problem with printing elements of a array using recursion program

    problem with printing elements of a array using recursion program
    Code:
    #include<stdio.h>
    void pa(int [],int,int);
    int main()
    {
    
    int a[14]={10,20,30,40};
     pa(a,0,3);
    }
    void pa(int a[],int lb,int ub)
    {
           if(lb==ub)
           printf("%d",a[lb]);
           else
           {
               printf("%d",a[lb]);
               pa(a,lb+1,ub);
           }
    }
    there is some problem with the above code.....please find where it went wrong.......
    Last edited by cooldude; 09-14-2009 at 11:21 AM.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by cooldude View Post
    problem with printing elements of a array using recursion program
    Code:
    #include<stdio.h>
    void pa(int [],int,int);
    int main()
    {
    int lb=0,ub=3;
    int a[10]={10,20,30,40};
    void pa(int a[],int lb,int ub);
    }
    void pa(int a[],int lb,int ub)
    {
           if(lb==ub)
           printf("%d",a[lb]);
           else
           {
               printf("%d",a[lb]);
               pa(a,lb+1,ub);
           }
    }
    there is some problem with the above code.....please find where it went wrong.......
    You've declared the function at two places and not called it anywhere.
    Also I dont get why you're declaring an array of 10 ints while there are only 5 in it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by cooldude View Post
    there is some problem with the above code.....please find where it went wrong.......
    oooh, its like a game!

    "void pa(int a[],int lb,int ub);" is not how you call a function, this would be an example of how to call a function:
    Code:
    #include<stdio.h>
    
    void foo (int bar)
    {
         // do something
    }
    
    int main()
    {  
        foo(5);
        return 0;
    }
    also, you have a function "int main" (correct), but you dont "return" anything, but im sure you know that when you saw the compiler warning.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by nadroj View Post
    oooh, its like a game!

    "void pa(int a[],int lb,int ub);" is not how you call a function, this would be an example of how to call a function:
    Code:
    #include<stdio.h>
    
    void foo (int bar)
    {
         // do something
    }
    
    int main()
    {  
        foo(5);
        return 0;
    }
    also, you have a function "int main" (correct), but you dont "return" anything, but im sure you know that when you saw the compiler warning.
    I guess that he knows how to call a function coz he's actually calling it recursively in the "pa" function.
    Also according to C99 return is optional in C, but yeah it's good to return 0 in the end.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nadroj View Post
    oooh, its like a game!
    LOL!

    @cooldude: absolute worst practice at programming forum: present code and then tell everyone else to find the problem! You already know there is a problem, and you have gotten error messages, etc, about it. You probably even know the line numbers. Why don't you just explain yourself PROPERLY AND INTELLIGENTLY to begin with?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by MK27 View Post
    LOL!

    @cooldude: absolute worst practice at programming forum: present code and then tell everyone else to find the problem! You already know there is a problem, and you have gotten error messages, etc, about it. You probably even know the line numbers. Why don't you just explain yourself PROPERLY AND INTELLIGENTLY to begin with?

    the above did not get any error, its compiled but i did not get correct output.....iam a beginner in C progamming....so i asked u guys to help me...
    if u r not interested to help me, then simply quit , but dont post useless messages.............hope u will understand..........

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by cooldude View Post
    the above did not get any error, its compiled but i did not get correct output.....
    So your post is completely useless unless and until you say that. As much as you may not want to hear what it said, MK27 was the only person who posted a useful post in this thread to date (inasmuch as it got you to explain further).

    So if you want output, you'll have to call the function that provides the output. Without doing so, you won't get any output.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cooldude View Post
    the above did not get any error, its compiled but i did not get correct output.....iam a beginner in C progamming....so i asked u guys to help me...
    if u r not interested to help me, then simply quit , but dont post useless messages.............hope u will understand..........
    Okay, let's get something straight about who is posting useless messages. I was just guessing that you got an error. I was wrong -- but why did I even have to guess? Let's look at all the useful information in your original post:
    there is some problem with the above code.....please find where it went wrong.......
    Now you are saying "the above did not get any error, its compiled but i did not get correct output". Better. Why did you not just say that in the first place? If the answer is you are too lazy, why do you expect others to bother helping? You could even explain EXACTLY what output you expected and EXACTLY what output you got, if you want to be really clear and get a quick, helpful reply.

    But trying to help a lazy person is very often a waste of time, because at the same time as they are too lazy to bother communicating effectively, they are also bound to be too lazy to bother paying attention to any of the advice they get.

    I am here A LOT. I answer A LOT of questions. I read pretty much every single thread in the C forum. I promise if you pay attention to what I am telling you in this post, it will be much easier for you to get advice and solve problems.

    Go back and edit your last post, or post again, with all the information you have. Unless, as nadroj says, you think this is some kind of game we are suppose to play with you.
    Last edited by MK27; 09-14-2009 at 10:48 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by MK27 View Post
    Okay, let's get something straight about who is posting useless messages. I was just guessing that you got an error. I was wrong -- but why did I even have to guess? Let's look at all the useful information in your original post:

    Now you are saying "the above did not get any error, its compiled but i did not get correct output". Better. Why did you not just say that in the first place? If the answer is you are too lazy, why do you expect others to bother helping? You could even explain EXACTLY what output you expected and EXACTLY what output you got, if you want to be really clear and get a quick, helpful reply.

    But trying to help a lazy person is very often a waste of time, because at the same time as they are too lazy to bother communicating effectively, they are also bound to be too lazy to bother paying attention to any of the advice they get.

    I am here A LOT. I answer A LOT of questions. I read pretty much every single thread in the C forum. I promise if you pay attention to what I am telling you in this post, it will be much easier for you to get advice and solve problems.

    Go back and edit your last post, or post again, with all the information you have. Unless, as nadroj says, you think this is some kind of game we are suppose to play with you.



    its not that iam lazy to do things...........
    iam new to these forums and i thought it could be sufficient to solve my problem, so i did not quote the clear and exact problem description...........
    its my mistake.........
    but dont discourage the people who are willling to learn new things by using adjectives like worst,bad etc..............
    and moreover u can advise people in a much comfortable way............
    and during our communication , if anything hurts u , then Iam sorry................

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    i modified the code...but still iam not getting any output........the code was compiled and there are no errors.....
    Code:
    #include<stdio.h>
    void pa(int [],int,int);
     main()
    {
    int a[4]={10,20,30,40};
     pa(a,0,3);
    }
    void pa(int a[],int lb,int ub)
    {
           if(lb==ub)
           printf("%d",a[lb]);
           else
           {
               printf("%d",a[lb]);
               pa(a,lb+1,ub);
           }
    }
    Last edited by cooldude; 09-14-2009 at 11:50 AM.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by cooldude View Post
    i modified the code...but still iam not getting any output........the code was compiled and there are no errors.....
    Code:
    #include<stdio.h>
    void pa(int [],int,int);
     main()
    {
    int a[4]={10,20,30,40};
     pa(a,0,3);
    }
    void pa(int a[],int lb,int ub)
    {
           if(lb==ub)
           printf("%d",a[lb]);
           else
           {
               printf("%d",a[lb]);
               pa(a,lb+1,ub);
           }
    }
    Deleting the int before main did not change anything. Main still returns int, because the compiler assumes int as the return type if none is given. Further, main must return int, to be standard C. Therefore you need to put a return 0 at the end of main. (you can also return something else to tell the OS you got an error, or perhaps some useful information.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Code:
    Hi,
    
    
    I compiled and runned your code with VC++6.0 and it went through just right! However, your code in the function void pa( int a[], int lb, int ub) is duplicated.
    
    void pa(int a[],int lb,int ub)
    {
           printf("%d",a[lb]);
           if(lb!=ub)
                pa(a,lb+1,ub);
    }
    
    
    Carle
    _____________________
    Free chat software for you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM
  5. problem with 2d array program.
    By Niloc1 in forum C Programming
    Replies: 1
    Last Post: 04-08-2002, 05:47 PM