First, as a basic recommendation, read about the topic of pointer arithmetics for educational reasons.
Now, let's give you some code you can work with.
A raw array is somewhat unwieldy to use. There are situations in which you want to do that, but not in this one. What you want to do here is to use STL containers, such as std::vector
. This one behaves like an array but also knows it's size and does some other nice things for you. If you use C++11 or higher, you can initialize those with a list, just like you did with the array:
std::vector<int> numbers = { 3, 5, 6 };
The next thing, there is no native printing for arrays or similiar types. A good thing to do here would to write a function that does that:
void print(const vector<int>& vec)
for(size_t i = 0; i < vec.size(); i++)
{
cout << vec[i] << " ";
}
cout << endl;
}
As you see, this works pretty well because vector
has the method size()
. We don't have to use a 3 in our code (a "magic number") and we have written a piece that can be reused in the future (our function).
Not the actual answer to your question, but I thought that this is something that helps you in the future. In general, take a good look at the things that the STL provides, those are your basic tools.