Recursive Program

This is a discussion on Recursive Program within the C Programming forums, part of the General Programming Boards category; I don't know why the program cannot get the return value -2 when pass the NULL to the function positive(....) ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Location
    Hong Kong
    Posts
    13

    Post Recursive Program

    I don't know why the program cannot get the return value -2 when pass the NULL to the function positive(....) by hardcode, it only return 0 if the value of size is NULL... and it shows following warning, how to avoid it?
    Please help to check!! thank you very much

    Name:  temp.jpg
Views: 74
Size:  18.4 KB


    Code:
    #include <stdio.h>
    
    float positive(float array[], int size) {
      int i = 0;
      if(size == 0){
        return 0; 
      }
      else if(size < 0){
        return -1;
      }
      else if(size == NULL){
        return -2;
      }
      else{
        if(array[i++] < 0){
          return positive(&array[1], size - 1);
        }
        else{
          return array[0] + positive(&array[1], size - 1);
        }
      }
    }
    
    int countArray(int array[], int size) {
      if (size == 0)
        return 0;
      return 1 + countArray(&array[1], size - 1);
    }
    
    int main() {
      int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
      int result;
      float floatarray[] = {1.1, -2.2, 3.3, 4.4, 0.0, -6.6, 7.7, 8.8, 9.9, -10.1};
      float floatresult;
    
      result = countArray(array, 10);
      printf("%d\n", result); 
      
      floatresult = positive(floatarray, NULL);
      printf("%.2f\n", floatresult); 
      /* should return 35.2 */
    
      getchar();
      return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Use a suitable integer, rather than NULL on those lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    Hong Kong
    Posts
    13
    as the question requirements.... i have to return following for the program...
    Name:  temp01.jpg
Views: 67
Size:  34.2 KB
    any other method?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    So which of those covers you passing NULL as the SECOND parameter.
    I see the last case as passing NULL in the FIRST parameter.

    Otherwise, you should be passing int's for the second parameter, such as 10, 0, -1 etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    QFE:
    -2 if the parameter array[] is NULL
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem running recursive program!
    By gaurav_13191 in forum C Programming
    Replies: 2
    Last Post: 03-28-2012, 01:02 PM
  2. a program recursive print from 1 to 2^n
    By abbaskhan in forum C Programming
    Replies: 18
    Last Post: 01-11-2012, 07:06 AM
  3. Help with a recursive expression tree program
    By TeamRival in forum C Programming
    Replies: 6
    Last Post: 03-25-2011, 07:27 PM
  4. Tracing through a recursive program in C
    By Hybodus in forum C Programming
    Replies: 3
    Last Post: 12-18-2010, 04:48 PM
  5. Help!!this program is Recursive Function or not?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 03:40 AM

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