2

I want to create an array and raise 2 to every element in that array and store it as new array arr2. Here is my code

#include <iostream>

using namespace std;

int main(){
  int arr1[7] = {1, 2, 3, 4, 5, 6, 7};

  auto arr2 = 2 ** arr1;

  cout << arr2 << endl;
}

But, it prints only the first element, it does not print the whole array. Why? So, basicaly, what I did here is I created arr1 with elements {1,2,3,4,5,6,7} and then I want arr2 to be

  • [2, 4, 8, 16, 32, 64, 128]

but for some reason it prints only the first element of array, it prints 2, but I want it to print all elements. Notice that 2 ** arr1 is the line where I am raising 2 to power (using exponentiation operator, i think it is how you call it if I'm not wrong) and then it should store array at array2.

What is wrong and why does it print only the first element instead all the elements?

kmdreko
  • 42,554
  • 6
  • 57
  • 106

3 Answers3

7

** is not an exponentation operator. C++ is not Fortran.

You have multiplied 2 by the first element of arr: your statement is equivalent to int arr2 = 2 * arr1[0];. What you have entered is perfectly legal C++ (consisting of multiplication and pointer dereference), and the use of auto is adding to the obfuscation.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

This statement

auto arr2 = 2 ** arr1;

is equivalent to

auto arr2 = 2 * *arr1;

Array designators in expressions are converted (with rare exceptions) to pointers to their first elements.

So the above statement can be rewritten like

auto arr2 = 2 * *( &arr1[0] );

As the first element of the array arr1 is equal to 1 then you have

auto arr2 = 2 * 1;

The expression 2 * 1 has the type int.

So as result the statement can be just rewritten like

int arr2 = 2;

There is no such operator as ** in C++. Instead you have to use standard function pow.

You can do the task either by writing an appropriate loop manually or using for example the standard algorithm std::transform.

Here is a demonstrative program

#include <iostream>
#include <cmath>
#include <algorithm>
#include <iterator>

int main()
{
    int arr1[] = { 1, 2, 3, 4, 5, 6, 7 };
    int arr2[sizeof( arr1 ) / sizeof( *arr1 )];

    std::transform(std::begin(arr1), std::end(arr1),
        std::begin(arr2),
        [](int x) { return pow( 2, x ); });

    for (int x : arr1) std::cout << x << ' ';
    std::cout << std::endl;
    for (int x : arr2) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

Its output is

1 2 3 4 5 6 7 
2 4 8 16 32 64 128 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The simplest way to do what you want can be something like this.

#include<iostream>
#include<math.h>
using namespace std;
int main(){
      int arr1[7] = {1,2,3,4,5,6,7}, arr2[7];
      for(int i = 0; i<7; i++){
          arr2[i] = pow(2, arr1[i]);    //arr2 will be created.
          cout<<arr2[i]<<" ";           //Show arr2.
          }
}
  • yes thank you, other answers already explained very good, but your answer is also valid, so thank you. –  Dec 28 '17 at 07:14