i solved this recursion, is it correct?

This is a discussion on i solved this recursion, is it correct? within the C Programming forums, part of the General Programming Boards category; Write a recursive function that searches an array of integers of size N and returns the number of integers equal ...

  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
    21,394
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,394
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,394
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05:05 PM
  5. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 07:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21