473,471 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

recursive functions with arrays as parameters

20 New Member
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
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void raisePowerOf2(int array[],int pow);
  3. int main(){
  4.     int a[2]={1,-1};
  5.     raisePowerOf2(a,5);
  6.  
  7.     return 0;
  8. }
  9.  
  10.  void raisePowerOf2(int array[],int pow){
  11.     int i, qtnt=0;
  12.     for(i=0;;i++){
  13.     if(array[i]==-1)
  14.     break;
  15.         }
  16.     const int len=i+1;
  17.     int store[len+1];
  18.  
  19.     for(i=len-2;i>=0;i--){
  20.     store[i+1]=(array[i]*2)%10+qtnt;
  21.     qtnt=(array[i]*2)/10;
  22.  
  23.     }
  24.     store[len]=-1;
  25.  
  26.     if(pow==1){
  27.     for( i=0;i<len;i++)
  28.         printf("%i",store[i]);
  29.     }
  30.  
  31.     if(pow>1)
  32.         raisePowerOf2(store,pow-1);
  33.  
  34. }
This is the code in java...
Expand|Select|Wrap|Line Numbers
  1. public class NewClass4 {
  2.  
  3.     void raisePowerOf2(int array[],int pow){
  4.     final int len=array.length;
  5.     int store[]=new int[len+1];
  6.     int qtnt=0;
  7.     for(int i=len-1;i>=0;i--){
  8.     store[i+1]=(array[i]*2)%10+qtnt;
  9.     qtnt=(array[i]*2)/10;
  10.  
  11.     }
  12. store[0]=qtnt;
  13.  
  14.     if(pow==1){
  15.     for(int i=0;i<len+1;i++)
  16.         System.out.print(store[i]);
  17.     }
  18.  
  19.     if(pow>1)
  20.         raisePowerOf2(store,pow-1);
  21.  
  22. }
  23.  
  24. public static void main(String s[]){
  25.  int a[]={1};
  26.    new NewClass4().raisePowerOf2(a,5);
  27. }    
  28.  
  29.  
  30. }
Sep 21 '13 #1

✓ answered by Nepomuk

You never set store[0] to a value, so it takes a random value from memory. Just add
Expand|Select|Wrap|Line Numbers
  1. store[0]=0;
after line 5 and it should work.

5 1620
weaknessforcats
9,208 Recognized Expert Moderator Expert
Which C are you using?
This code:

Expand|Select|Wrap|Line Numbers
  1. const int len=i+1;
  2. int store[len+1];
  3.  
won't compile as either C or C++ with my compiler.
Sep 21 '13 #2
nilushika
20 New Member
i compiled this with c-free5.0.it compiles fine.i'd like to know what the error is,please.
Sep 21 '13 #3
Nepomuk
3,112 Recognized Expert Specialist
You never set store[0] to a value, so it takes a random value from memory. Just add
Expand|Select|Wrap|Line Numbers
  1. store[0]=0;
after line 5 and it should work.
Sep 21 '13 #4
nilushika
20 New Member
Thank you Nepomuk, that's the point!
I made line 25 as ' store[0]=qtnt;'I have forgot it.
Now it works ....
thnks for the all.
Sep 21 '13 #5
nilushika
20 New Member
Thank for all.....
Sep 21 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: BoonHead, The Lost Philosopher | last post by:
Does anyone know how to implement internal interface documentation for functions and parameters? I found a similair thing for proerties. For properties I found something like:...
64
by: dmattis | last post by:
I am trying to write a recursive version of Power(x,n) that works by breaking n down into halves(where half of n=n/2), squaring Power(x,n/2), and multiplying by x again if n was odd, and to find a...
7
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? ...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
6
by: jeniffer | last post by:
Please give an example of 2 simple mutually recursive functions in C .
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
5
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.