Value of an Array

I'm doing a practice worksheet and I'm asked what the value of each of the array elements listed below. This may seem like a stupid question but how do you find the value of array elements.

myArray[0]=?
myArray[5]=?
myArray[9]=?
myArray[10]=?
closed account (48T7M4Gy)
I don't see how you can answer the question. Its like I am shown a closed pot and someone asks what's in the pot? The only answer is "whatever was put into it' which isn't very satisfying especially when that's the answer for all of them.

If the question mark was written '?' then the story would be completely different.
:)
For example, If you have myArray declared,

1
2
    const int numOfElements{ 10 };
    int myArray[numOfElements] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };


Then,
1
2
3
std::cout << myArray[0] << endl; // Output is 1
std::cout << myArray[7] << endl; // Output is 8
std::cout << myArray[10] << endl; // Output will result undefined behavior (run-time error) 


The array element count starts at zero.

But if you don't have the variable myArray declared, then who knows???

I probably should have put down the answer for the previous question. It asked me to create an array of type int named myArray and initialize it with the following list: 0,2,3,4,5,6 heres what I put.
int myArray= new int[]{0,2,3,4,5,6};
Last edited on
closed account (48T7M4Gy)
That's better Serge1005

myArray[0]=0
myArray[5]=6
myArray[9]= undefined/junk
myArray[10]= undefined/junk

Consider int myArray[]= {0,2,3,4,5,6}; see what chicofeo has above for the right syntax and no problem with the array index being out-of-range
Last edited on
Really appreciate the help! Will need it for my finals this week
Just a quick question what did you mean about what you said in the end.
closed account (48T7M4Gy)
Who/what? :)
The most important thing is remember to count starting from 0.
closed account (48T7M4Gy)
The first element in an array is the zero-th element i.e. myArray[0]
Topic archived. No new replies allowed.