'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
- #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);
- }
Expand|Select|Wrap|Line Numbers
- 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;
- }
- store[0]=qtnt;
- 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);
- }
- }