Thread: recursive functions with arrays as parameters

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    4

    Talking recursive functions with arrays as parameters

    I wrote a fuction in C with the prototype
    'void raisePowerOf2(int array[],int pow);'

    If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)

    But it seems that this function doesn't give the exact answer

    And I tried this from java also,with the same implementation.It produced the answer precisely .

    I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly .
    Can you please help me......


    This is the code in c
    Code:
    #include<stdio.h>
     void raisePowerOf2(int array[],int pow);
     int main(){
         int a[2]={1,-1};
         raisePowerOf2(a,5);
     
         return 0;
     }
     
      void raisePowerOf2(int array[],int pow){
         int i, qtnt=0;
         for(i=0;;i++){
         if(array[i]==-1)
         break;
             }
         const int len=i+1;
         int store[len+1];
     
         for(i=len-2;i>=0;i--){
         store[i+1]=(array[i]*2)%10+qtnt;
         qtnt=(array[i]*2)/10;
     
         }
         store[len]=-1;
     
         if(pow==1){
         for( i=0;i<len;i++)
             printf("%i",store[i]);
         }
     
         if(pow>1)
             raisePowerOf2(store,pow-1);
     
     }
    This is the code in java....
    Code:
    public class NewClass4 {
     
         void raisePowerOf2(int array[],int pow){
         final int len=array.length;
         int store[]=new int[len+1];
         int qtnt=0;
         for(int i=len-1;i>=0;i--){
         store[i+1]=(array[i]*2)%10+qtnt;
         qtnt=(array[i]*2)/10;
     
         }
     
         if(pow==1){
         for(int i=0;i<len+1;i++)
             System.out.print(store[i]);
         }
     
         if(pow>1)
             raisePowerOf2(store,pow-1);
     
     }
     
     public static void main(String s[]){
      int a[]={1};
        new NewClass4().raisePowerOf2(a,5);
     }    
     
     
     }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're probably getting an integer overflow. A java int type is 32 bits, and overflows are guaranteed to wrap. Neither property is guaranteed in C.

    Or you're mucking up the computation of array lengths. Your coding is less than clear and - to say the least - the technique is not exactly a thing of beauty. When you don't code clearly, you increase the chances of making a stupid error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    How does that even compile? (maybe it does I haven't tried, but it doesn't look like it would)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    The first step would be some sane indentation.
    Code:
    #include<stdio.h>
    void raisePowerOf2(int array[], int pow);
    int main()
    {
      int a[2] = { 1, -1 };
      raisePowerOf2(a, 5);
    
      return 0;
    }
    
    void raisePowerOf2(int array[], int pow)
    {
      int i, qtnt = 0;
      for (i = 0;; i++) {
        if (array[i] == -1)
          break;
      }
      const int len = i + 1;
      int store[len + 1];
    
      for (i = len - 2; i >= 0; i--) {
        store[i + 1] = (array[i] * 2) % 10 + qtnt;
        qtnt = (array[i] * 2) / 10;
    
      }
      store[len] = -1;
    
      if (pow == 1) {
        for (i = 0; i < len; i++)
          printf("%i", store[i]);
      }
    
      if (pow > 1)
        raisePowerOf2(store, pow - 1);
    
    }
    
    public class NewClass4 {
      void raisePowerOf2(int array[], int pow) {
        final int len = array.length;
        int store[] = new int[len + 1];
        int qtnt = 0;
        for (int i = len - 1; i >= 0; i--) {
          store[i + 1] = (array[i] * 2) % 10 + qtnt;
          qtnt = (array[i] * 2) / 10;
        } 
        if (pow == 1) {
          for (int i = 0; i < len + 1; i++)
            System.out.print(store[i]);
        }
        if (pow > 1)
          raisePowerOf2(store, pow - 1);
      }
    
      public static void main(String s[]) {
        int a[] = { 1 };
        new NewClass4().raisePowerOf2(a, 5);
      }
    }
    The thing that strikes me as being odd is that in the C code, the for loop starts at len-2
    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.

  5. #5
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    no further comments

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by Salem View Post
    The thing that strikes me as being odd is that in the C code, the for loop starts at len-2

    I think there is no problem with len-2,there I wrote 2 to exclude the last element in each array which is -1.
    Last edited by nilushika; 09-21-2013 at 09:03 AM.

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by SirPrattlepod View Post
    no further comments


    Definitely needed further comments....

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I have to use malloc() and free() to work with microsoft compiler. Each recursion adds a leading zero to store, even though it's not needed most of the time. After the for loop with % and /, qtnt will always be zero since store is longer than it needs to be.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void raisePowerOf2(int array[], int pow);
    int main()
    {
        int a[2] = {1,-1};
        raisePowerOf2(a, 32);
        return 0;
    }
     
    void raisePowerOf2(int array[], int pow){
        int i, len, *store, qtnt=0;
        for(i=0; array[i] != -1 ; i++);
        len = i;
        store = malloc((len+2)*sizeof(*store));
        for(i = len-1; i >= 0; i--){
            store[i+1]=(array[i]*2)%10+qtnt;
            qtnt=(array[i]*2)/10;
        }
        store[0] = qtnt;
        store[len+1] = -1;
        if(pow > 1)
            raisePowerOf2(store, pow-1);
        else{
            for(i=0; i <= len; i++)
                printf("%d",store[i]);
        }
        free(store);
    }
    This version uses malloc() and realloc() to resize an allocated array only as needed:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    void raisePowerOf2(int **pa, int pow);
    int main()
    {
        int * a = malloc(2 * sizeof(*a));
        a[0] = 1;
        a[1] = -1;
        raisePowerOf2(&a, 32);
        free(a);
        return 0;
    }
     
    void raisePowerOf2(int **pa, int pow){
        int *a = *pa;
        int i, len, a2, qtnt = 0;
        for(i = 0; a[i] != -1; i++);
        len = i;
        for(i = len-1; i >= 0; i--){
            a2 = a[i]*2;
            a[i]=a2 % 10 + qtnt;
            qtnt=a2 / 10;
        }
        if(qtnt){
            len += 1;
            *pa = a = realloc(a, (len+1) * sizeof(*a));
            for(i = len; i > 0; i--)
                a[i] = a[i-1];
            a[0] = qtnt;
        }
        if(pow > 1)
            raisePowerOf2(pa, pow-1);
        else{
            for(i = 0; i < len; i++)
                printf("%d", a[i]);
        }
    }
    Last edited by rcgldr; 09-21-2013 at 05:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL and functions parameters...
    By BrownB in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2006, 01:31 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. functions passing parameters
    By volk in forum C Programming
    Replies: 9
    Last Post: 01-02-2003, 05:49 PM
  4. please help : arrays/parameters
    By Jax in forum C Programming
    Replies: 9
    Last Post: 11-14-2001, 01:14 PM

Tags for this Thread