Thread: i solved this recursion, is it correct?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    i solved this recursion, is it correct?

    Write a recursive function that searches an array of integers of size N and returns the number of integers equal to a target value

    so all i did was this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int search(int n,int x[],int size,int count[0]) {
    
           if (size == -1) {
    
                 return count[0];
    
            }
    
           else {
    
          if (x[size] == n){
              count[0]++;
             }
    
           search(n,x,size-1,count);
    
           }
    }
    
    int main(void){
    
            int count=0;
    
            int x[10]= {1,2,3,2,9,3,10,2,2,2};
    
            search(2,x,sizeof(x)/sizeof(int) - 1,&count);
    
            printf("%d\n",count);
    
            system("pause");
    
    return 0;
    
    }
    it gets me the correct result for different values, but am I correct? I'm not using any loops to get my result, just an if statement to increase the count

    Thanks in advance

  2. #2
    C/Linux Programmer
    Join Date
    Dec 2009
    Posts
    17
    Hey, your recursive call coupled with if and increment is serving as loop only!

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by technam View Post
    Hey, your recursive call coupled with if and increment is serving as loop only!
    yea i know what i meant is that i'm not using any for/while/do while loops inside the function to get the desired result

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to name your function count rather than search. I do not understand why you have the count parameter. It seems to me that you just need one parameter for the pointer to the first element of the array, another parameter for the size of the array (or subarray), and yet another parameter for the target value. You would then make full use of the return value.

    I note that your indentation can be improved, but you did give it a try and posted in code tags
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by laserlight View Post
    You might want to name your function count rather than search. I do not understand why you have the count parameter. It seems to me that you just need one parameter for the pointer to the first element of the array, another parameter for the size of the array (or subarray), and yet another parameter for the target value. You would then make full use of the return value.

    I note that your indentation can be improved, but you did give it a try and posted in code tags
    hi thanks for the reply

    but how can i do this without using the variable count? I used it to count the number of times the target appears in the specified array

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackhasf
    but how can i do this without using the variable count? I used it to count the number of times the target appears in the specified array
    Why not return the count of the number of times the target appears in the specified array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by laserlight View Post
    Why not return the count of the number of times the target appears in the specified array?
    i think i return it

    i have return count[0];

    do you mean anything else?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackhasf
    i think i return it

    i have return count[0];
    That is true, but not all control paths return a value (what does your function return if the size is not equal to -1?). Basically, you are using the count parameter as an out parameter (or more precisely, as an in/out parameter). But what I am saying is that you do not need it at all.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help in recursion program
    By Bargi in forum C Programming
    Replies: 7
    Last Post: 03-17-2009, 12:35 PM
  2. Recursion
    By arctic_blizzard in forum C Programming
    Replies: 9
    Last Post: 10-26-2008, 04:37 PM
  3. recursion...recursion...
    By Chaplin27 in forum C++ Programming
    Replies: 15
    Last Post: 10-06-2004, 12:54 PM
  4. oooh how much fun recursion is...
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-05-2004, 06:05 PM
  5. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:52 PM