0

I don't understand this, the part where it says, "myArray[x] = 42;" Since when did x even come into myArray? Atleast someone explain me how this script even works?

{ int = myArray[5];
for (int x = 0; x < 5; x++) {
myArray[x] = 42;

cout << x << ":" << myArray[x] << endl;


}
}
CollinD
  • 7,304
  • 2
  • 22
  • 45
Akuhyo
  • 23
  • 6
  • 3
    Here is [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). –  Feb 10 '17 at 02:06
  • I don't need a book, I just need to see how the "myArray[x]" even works? I just dont understand that exact part. – Akuhyo Feb 10 '17 at 02:07
  • 1
    Well your code doesn't compile so I think you may need a tutorial. – CollinD Feb 10 '17 at 02:08
  • 1
    Who is teaching you C++? You should have a good book. A book will explain how accessing arrays works. – Greg Kikola Feb 10 '17 at 02:09
  • Do you know what `x` is? Do you know what `int x` means? Do you understand that `x` is a variable, and that a variable has a value? Do you know what a `for` loop is and what values that particular loop would be giving to `x`? If you understand those things, then hopefully that should lead to you towards the idea that `myArray[x]` uses the current value of `x` to index into the array. – TheUndeadFish Feb 10 '17 at 03:10

2 Answers2

1

C++ arrays are random-access containers. That means that if you want the ith element of the array, you can ask for it (and get it in constant time).

The operator used for array element access is the brackets ([]) operator. It takes a parameter of type std::size_t, which is a typedef to some unsigned integer type that's big enough to index all the memory addresses of your computer's RAM. Thus, if I have an array arr, and I index into it by writing arr[2], I'm asking for the second element of the array.

In the following example, an array is created, and each element is assigned a value equal to the square of its index.

#include <cstdlib>
#include <iostream>

int main() {

    constexpr std::size_t length = 10;
    int arr[length];

    for (std::size_t i = 0; i < length; ++i) {
        arr[i] = i * i;
        std::cout << "Element " << i << " of arr is " << arr[i] << "." << std::endl;
    }

}

Because operator[] returns a reference (in this case an int&), the expression arr[i] can be used for both reading and writing the value.

Sam Marinelli
  • 999
  • 1
  • 6
  • 16
0

I'll be specific here, you need to read on basic programming in c++, because you are questioning the fundamental ways the for-loop in c++ works.

I will start by formatting your code so that is is easier to read:

{ 
    int = myArray[5];
    for (int x = 0; x < 5; x++)
    {
        myArray[x] = 42;
        cout << x << ":" << myArray[x] << endl;
    }
}

Let me point out some errors in your code. Firstly, when you declare a variable in c++, you must also declare its data type along with it. So the following example:

var = 2;

Is invalid, because the data type of the variable var is not specified. The compiler cannot tell what the data type of a variable is by simply looking at the value passed to it in c++. Therefore, we need to add the data type just before the name of the variable when DCLARING IT ONLY. So in the above example:

int var = 2;

Is a valid variable declaration because we have specified that this variable will only store an int value.

Now the problem in your code is that you are trying to do:

    int = myArray[5];

This makes no sense. First problem is that you are trying to assign a data type a value as if it were a variable. Second problem is that you never declared myArray before. From your cod, I'm assuming that you intended to do:

    int myArray[5];

This makes sense because your for-loop also runs 5 times. In this line, you have declared an array called myArray that stores 5 values of type int.

Now moving on to the for-loop. Lets dissect the header statement of the for-loop:

for (int x = 0; x < 5; x++)

The first statement right after the opening semicolon int x = 0; simply declares a variable ONCE for the entire run of the for loop. This variable x is local to the for loop and cannot ba accessed outside of it. In c++, we refer to it as a counter variable as it stores the number of times we have gone looped through a for-loop.

The next statement x < 5; is a condition for the for-loop. While this condition is true, the for-loop will run; In your case, while x is less than 5, it will run through the loop. As soon as this condition become false; in this code, if x becomes equal to or greater than 5, then the condition is falsified, and the loop is terminated. This way, we ensure that the loop runs through our code only the number of times we want it to.

The final statement x++; simply means that x is incremented at every point. If it is not incremented, then the value of x will never change, and the condition of the loop will never be falsified, making it an infinite loop. Therefore, this statement is crucial.

Now while we are inside the for-loop, we can use the variable x. This is particularily useful in your case as you have an array, and you are trying to fill it up with values. Instead of doing array[a]=n, where a ranges from 0 up till the size of the array minus 1, and n is a random number, we can use a for loop to initialize our values.

In the for loop, how do we do that? We use the counter x, that increments itself after every loop run, to serve as the "a" in array[a]. That's why we use myArray[x] = 42;, simply because x increments itself by 1 after every loop and this number can be used as the address (sorry for my poor choice of words here) of the array. Why it allocates the number 42 is random; it could allocate any number.

If you still need some clarification, or if I made a mistake in my post, please let me know in the comments box.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31