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

  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,336
    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
    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.

  9. #9
    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

  10. #10
    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................

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cooldude View Post
    and during our communication , if anything hurts u , then Iam sorry................
    Well, I am not trying to be your enemy of course and the only person I get angry at is Mario F., who has the good sense to stay out of the C forum But when I wrote "absolute worst practice" I did mean it.

    I am just saying, as someone who could potentially help you out, how you can get the best out of a forum, based on my experience. I am doing that because I recognize you are new. I understand that you may have "thought it could be sufficient to solve my problem" (maybe it is!); what I am telling you is that it would be better to think about how you can ask a question "in the best way", and not assume that things like desired outcome and actual outcome are irrelevant or superfluous:

    How To Ask Questions The Smart Way

    If you look under "translations", you will notice that has been translated into more than 20 languages, which is 19 more than 95% of actual programming tutorials. I wonder who thinks this is so important and why?

    Now, I compiled and ran your last posted code. Here is the output:

    10203040

    That is exactly what I would expect, so what are you complaining about? Other than the fact that main() should always be of type int and return an int value, there is nothing wrong with your code.

    I will take another guess: you are using an MS IDE, and the window closes right away?
    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

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    Quote Originally Posted by MK27 View Post
    Well, I am not trying to be your enemy of course and the only person I get angry at is Mario F., who has the good sense to stay out of the C forum But when I wrote "absolute worst practice" I did mean it.

    I am just saying, as someone who could potentially help you out, how you can get the best out of a forum, based on my experience. I am doing that because I recognize you are new. I understand that you may have "thought it could be sufficient to solve my problem" (maybe it is!); what I am telling you is that it would be better to think about how you can ask a question "in the best way", and not assume that things like desired outcome and actual outcome are irrelevant or superfluous:

    How To Ask Questions The Smart Way

    If you look under "translations", you will notice that has been translated into more than 20 languages, which is 19 more than 95% of actual programming tutorials. I wonder who thinks this is so important and why?

    Now, I compiled and ran your last posted code. Here is the output:

    10203040

    That is exactly what I would expect, so what are you complaining about? Other than the fact that main() should always be of type int and return an int value, there is nothing wrong with your code.

    I will take another guess: you are using an MS IDE, and the window closes right away?



    ya the window closes away and iam using dev c++ compiler.............

    thanks for your help ............

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

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by cooldude View Post
    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................
    Did you know that there is no such word as "iam"? They are two separate words; "I" and "am". You're certainly not lazy when it comes to adding extra dots at the end of sentences. Overzealous is perhaps what I'd call it.
    My advice to you is to try and write propper English. It will go a long way towards getting people to take you seriously. That means capital letters at the start of sentences, no extra dots, comma has a space after it but not before, the word"I" is always capital, lots of stuff like that.
    We now have a baseline of how you wrote stuff today. If you show that you're making an effort to write readable sentences next time, then more people will feel like making an effort to help, and they'll do it more politely too! It's all for your own bennefit.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by iMalc View Post
    write propper English.
    [sic] .............
    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

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