Can Someone tell me why this program is not doubling the numbers 64 times ?

it dos not have to be array or a loop:

Think about an array that has 64 elements , write a code to insert on the first element the number 1, then double this number  1*2  =2 and store it as the second element , double the second element (2 *2 = 4) and store it as the  third element, and continue this up to 63 times (n -1) and print the final product.
For example:  9,223,372,036,854,775,808

thanks for all the help


this is what I got so far
what am I doing wrong
is there a easier way then what I am doing please tell me thanks

I don't know why I keep getting zeros near the end can someone check



----------------------------------------------------------------------


#include <iostream>

using namespace std;

1 void doubleThis(int num, int until) {
2 if (until == 0) {
3 return;
4 }
5 num = num * 2;
6 cout << num << endl;
7 doubleThis(num, until-1);
8 }
9
10
11 int main() {
12 int x;
13 cout << "Enter the the number to be doubled for next 5 times " << endl;
14 cin >> x;
15 cout << "Entered number is " << x << endl;
16 doubleThis(x, 5);
17
18 return 0;
19 }
123
Last edited on
https://en.wikipedia.org/wiki/Wheat_and_chessboard_problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cmath>
using namespace std;

const size_t SIZE = 63;

template <typename T>
void wheatChess(T wheatChess_array[], size_t n)
{
    for(size_t i = 0; i < n; i++)
    {
        wheatChess_array[i] = pow(2,i);
        cout<<i+1<<": "<<wheatChess_array[i]<<"\n";
    }
}
int main ()
{
    unsigned long long wheatChess_array[SIZE];
    wheatChess(wheatChess_array, SIZE);
}
Integers only take up a certain small number of bits. On my system, for instance, int is 32 bits, which gives it a range of roughly +/- 2147 million. Chances are this is the same for you, unless you're using an out-of-date system or compiler, or are not working on a common desktop or laptop.

Note that 2^63 requires at least 63 bits to store. So 32 bits is not going to cut it. Chances are that you're experiencing signed integer overflow, which is undefined behavior. (Unsigned integer overflow wraps to zero, and is well-defined, so we'll prefer unsigned in cases where overflow is possible.)

You need any integer that is at least 64 bits in size; ask for it.
uint_least64_t
Last edited on
I think I had better take cover!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
using namespace std;


const int MAXSIZE = 100;


void doubleIt( int *a, int &last )
{
   int i = 0;
   int carry = 0;

   while ( i <= last )
   {
      a[i] = 2 * a[i] + carry;
      carry = a[i] / 10;
      a[i] = a[i] - 10 * carry;
      i++;
   }
   if ( carry > 0 )
   {
      last++;
      a[last] = carry;
   }
}


int main()
{
   int a[MAXSIZE] = { 0 };
   int last = 0;
   
   a[0] = 1;
   for ( int n = 1; n <= 128 ; n++ )
   {
      doubleIt( a, last );
      cout << "2 to the power of " << n << " is ";
      for ( int i = last; i >= 0; i-- ) cout << a[i];
      cout << endl;
   }
}
Gunner funner your code that you did is almost what I was looking for
is there a way I can promp the user to type a number and then that number is doubled 64 times and at the end print the final number which is 4611686018427387904
I just the user to type a number then is doubled just like you did
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

template <typename T>
void wheatChess(T* wheatChess_array)
{
    cout<<"Enter array size: \n";
    size_t len;
    cin>>len;
    wheatChess_array = new T[len];
    for(size_t i = 0; i < len; i++)
    {
        wheatChess_array[i] = pow(2,i+1);
        cout<<i+1<<": "<<wheatChess_array[i]<<"\n";
    }
    delete wheatChess_array;
}
int main ()
{
    unsigned long long* wheatChess_array;
    wheatChess(wheatChess_array);
}
well when I type 1 it only doubles it one time
1 to 2
how can I make it do that 64 time ? just like your last code

like how can I prompt the user to type 1 and then it doubles automatically just like this I been tring to do this but I haven't got it just yet

1
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963968
72057594037927936
144115188075855872
288230376151711744
576460752303423488
1152921504606846976
2305843009213693952
4611686018427387904
Last edited on
is there a way to do it just with loops or if statements ? to make it more simple code
like if the user types 1 it will double the number by2 64 times
take a look at lastchance's solution - it is very flexible and can do both things that you're asking for:
like if the user types 1 it will double the number by2 64 times
is there a way I can promp the user to type a number and then that number is doubled 64 times
> For example: 9,223,372,036,854,775,808

9,223,372,036,854,775,808 is more than the maximum value that the type int can represent (on this particular implementation)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <limits>

void doubleThis( int num, int until ) {

    static constexpr int maxval = std::numeric_limits<int>::max() ;
    static constexpr int ubound = maxval / 2 ;
    static constexpr int minval = std::numeric_limits<int>::min() ;
    static constexpr int lbound = minval / 2 ;

    if( until > 0 )
    {
        if( num >= lbound && num <= ubound ) {

            num = num * 2;
            std::cout << num << '\n' ;
            doubleThis( num, until-1 );
        }

        else {
            
            std::cout << "*** doubling " << num << " would engender undefined behaviour\n"
                         "(signed integer overflow; " ;
            if( num > 0 ) std::cout << "INT_MAX is: " << maxval << ")\n\n" ;
            else std::cout << "INT_MIN is: " << minval << ")\n\n" ;
        }
    }
}

int main() {
    
    int x;
    std::cout << "Enter the the number to be doubled for next 63 times\n" ;
    std::cin >> x;

    std::cout << "entered number is " << x << '\n' ;
    doubleThis( x, 63 );

    std::cout << "\nnegative of entered number is " << -x << '\n' ;
    doubleThis( -x, 63 );
}

http://coliru.stacked-crooked.com/a/0daebf99a1fb75e4

Use unsigned long long (it has a width of at least 64 bits); the upper limit is higher
http://coliru.stacked-crooked.com/a/ea161d5cb4e5b2ad
http://coliru.stacked-crooked.com/a/e11c9512a46ae1ec
well lastchance's solution dosent make the user type 1 then doubles
it just dos it
and I don't thing the numers are correct in the code
Last edited on
jLBorges I coudnlt rub your code it says there is an error
OP: you'd have to do a bit of work yourself and adapt lastchance's code to your specific needs, don't expect everything on a plate. That program has all you need
> it says there is an error

What is the complete text of the error message?
Topic archived. No new replies allowed.