# Building an array of ints

This is a discussion on Building an array of ints within the C++ Programming forums, part of the General Programming Boards category; I'm trying to build an array of integers, in which the number of elements of the array is determined by ...

1. ## Building an array of ints

I'm trying to build an array of integers, in which the number of elements of the array is determined by the length of a string parameter (which is an integer). The intent is to build a list starting from 1, then 4,16,64,256,1024, etc, until it becomes greater than the number read in on the string.

Code:
string num(string a) {
int list1[a.length()^2/2];
list1[0] = 1;
for (int y=1; y < 2*a.length(); y++) {
list1[y] = 4^y;
cout << "list1 contains:  " << list1[y] << endl;
}

}
The first problem is that the printout gives values of 5,6,7,0,1,2,3 before crashing, not the powers of four which I wanted it to.

Any ideas on how to tackle this?

2. ^ isn't the power operator, it's the bitwise exclusive-or operator.

3. ^ isn't a power operator, it means bitwise exclusive-OR. I don't think XOR was what you wanted, so look into the pow function from <cmath>.
Code:
#include <cmath>

std::pow(x, y); // x to the yth power
Code:
int list1[a.length()^2/2];
Keep in mind that this isn't standard C++. An array in C++ can only have a constant integral size; your size isn't constant, so the code may not work on other compilers. The standard way to do it is
Code:
int *list1 = new int[std::pow(a.length(), 2.0) / 2];
But then you need to remember to free the memory because it won't be released automatically.
Code:
delete [] list1;
Even better would be a standard vector object:
Code:
#include <vector>

std::vector<int> list1(std::pow(a.length(), 2.0) / 2);

4. Code:
int list1[a.length()^2/2];
Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with:
Code:
int list1[a.length()^0];
Which would give them:
Code:
int list1[1];
Which really wouldn't be what they wanted.

Quzah.

5. >>Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with

Since when does 2/2=0?

6. Originally Posted by quzah
Even if it were the power-of operator, division would likely get a higher precedence
^ has a higher precedance than * in math. On my TI82 calculator: 3^2/2 gives 4.5, not 3.

7. Remember the order of operations, PEMDAS?
Please Excuse My Dear Aunt Sally

Parentheses Exponents Multiplication Division Addition Subtraction

8. Remember that C++ has its own operator precedence: http://www.cppreference.com/operator_precedence.html

This means that even if you wrote your own number class and implemented ^ as the exponential operator, then Quzah's demonstration would be correct... Well, hopefully it would divide correctly, but other than that, it'd follow Quzah's demonstration.

9. if you wrote your own number class and implemented ^ as the exponential operator
Yes, *IF*. But ^ is not the exponential operator so it would be wrong. It's like overloading - as the multiplication operator and claiming that "2 times 3 plus 1" is 0 instead of 7.

10. Originally Posted by JaWiB
>>Even if it were the power-of operator, division would likely get a higher precedence, so they'd end up with

Since when does 2/2=0?