-2

What is the role of "x" in the array's brackets and the loop?

(The code below is an example that was used to demonstrate some logic in C#)

#include <iostream>
#include <string>
using namespace std;

int main() {
    int myArr[5];
    //Why is 'x' present in "myArr[x] and what is the use of for loop
    for(int x=0; x<5; x++) {
        myArr[x] = 42;
        
        cout << x << ": " << myArr[x] << endl;
    }
}
Naitik
  • 55
  • 1
  • 8
  • where did you get this code from? – 463035818_is_not_an_ai Sep 29 '20 at 13:59
  • you wrote the code? Why did you put the loop there? Compile and run the code, then remove the loop, compile and run again, notice any difference? – 463035818_is_not_an_ai Sep 29 '20 at 14:02
  • Using `myArr` in this case looks redundant and the printing can simply be written as `cout << x << ": 42\n";` – MikeCAT Sep 29 '20 at 14:02
  • 3
    May I suggest getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? This is absolutely basic use of array and if your learning source doesn't explain it well, you need something better. – Yksisarvinen Sep 29 '20 at 14:03

3 Answers3

1

Your code is (almost) equivalent to:

#include <iostream>

int main() {
    for(int x=0; x<5; x++) {
        std::cout << x << ": " << 42 << std::endl;
    }
}

The output is

0: 42
1: 42
2: 42
3: 42
4: 42

And your code produces same output.

Why the author of the code uses an array, only the author of the code can know.

why is 'x' present in myArr[x]

Because x is used as index into the array myArr.

what is the role of for loop in this code

The role of the for loop is to execute the body of the loop 5 times. x has the value n in the n-th iteration.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • So, instead of writing "myArr[0] = 42;"--"myArr[1] = 42;"...and so on--we can write "myArr[x]"(with a for loop)...Right? OR is it necessary to write a for loop? – Naitik Sep 30 '20 at 17:23
  • @NaitikKumarSrivastava i have no clue what the code is supposed to do, I just considered its output. Actually compilers do similar, they are allowed to do whatever to your code as long as the observable behavior is the same. There is no reason to store the numbers in an array. It is also not necessary to use a loop, but as long as it is unclear what is the code supposed to do asking whether a loop or array is needed doesn't bring much insight – 463035818_is_not_an_ai Sep 30 '20 at 17:51
  • @NaitikKumarSrivastava in different words: What is the code supposed to do? Why did you use an array and a loop in the code? – 463035818_is_not_an_ai Sep 30 '20 at 18:01
  • (1) The code is giving the same value to a number of elements in the arrary(instead of writing it manually) (2) The loop is used for the same(the first point) and the array is here for taking a number of identical values from the loop and use them for its elements (I know I didn't frame a right sentence) – – Naitik Oct 01 '20 at 02:49
  • @ idclev 463035818 I understood the use of the "For loop" but I still don't understand why is 'x' present in "myArr[x] = 42" <~~ I didn't understand why is this 'x' present here in those brackets – Naitik Oct 01 '20 at 02:57
0

Up Edit: I strictly recomend you to get C++ book, and read it. Or watch tutorials of C++ coding. Here is not to good way of learning new language

the build of for loop like this: for ( init; condition; increment )

At first part, you need to initialize variable, name doesen't important. In your case, you named 'x' you can name whatever you want. And you define starting point of x.

In your case, Your x is integer, and x starts from 0

In condition part, you declare the condition, in your case, when x<5, loop is continuing (in true values)

And the last part of for loop, you declare the incriment to end loop somewhere. x++ means x = x+1

So in for loop argument is like this "Start from 0, count one by one untill x reachs 5" and untill reachs 5, do something (do what is in brackets {} )

Array is array of datas, and myArr[x] means, "myArr's x. Data"

And because of for loop, it'll go from zero to fourth data. In array

And all of data will be equal 42.

Like, first data of myArr[x] (don't forget x is an integer) (myArr[0]) will equal 42.

The second will equal 42 and so on. For loop decides how long x variable will go. But, Array must be declared that size due to prevent out of range errors.

  • Ok, I understood why **For Loop** is present here. Can you explain me why is **x** present in **"myArr[x]"** with the help of an argument like you did for the **For Loop** (in an another answer for the same question)?(So in for loop argument is like this "Start from 0, count one by one untill x reachs 5" and untill reachs 5, do something (do what is in brackets {} ) ) – Naitik Sep 30 '20 at 17:21
  • @Naitik Kumar Srivastava the guy who wrote the code wanted to store datas in myArr[]. He can just do idclev's answer. But he did in this way. And why he store data in Array is, if you want to use those data in future, you can call array with spesific data cell to reach data easily. It isn't revoke until scope is out. – Ahmet Yusuf Yatkın Sep 30 '20 at 18:54
0

(Scroll down for a summary of everything I just said) The array ‘myArr’ has 5 integer elements, each of which can be accessed through their index. For example, to access the first element in myArr, we can use: myArr[0] (Note that indices start from 0, so the first element in any array is at index 0)

Keeping this “index-property” of arrays in mind, we can implement “for” loops with arrays. For loops work well with arrays because they sequentially go through a certain data set, and arrays are essentially a row-wise collection of elements of the same data type. So using a for loop, we can go through each element one by one (and perform whatever processes we need to).

In the for-loop, the variable “x” is used to go through the array. The initial value of 0 for x will make myArr[x] equal to myArr[0], which is the first element of myArr. In the second iteration of the for-loop the same code is run, however, this time the value of x is 1, and thus instead of myArr[0], myArr[1] is used; i.e: the second element is referenced.

In summary, the for loop just iterates over all the elements in the array and the local variable x is just used as a reference to the index of the element currently being used in the loop.

I hope this answers your question :)

ObedA.
  • 1
  • So, instead of writing "myArr[0] = 42;"--"myArr[1] = 42;"...and so on--we can write "myArr[x]"(with a for loop)...Right? OR is it necessary to write a for loop? – Naitik Sep 30 '20 at 17:15
  • @NaitikKumarSrivastava put simply, you should use a for loop if you need to access the elements in the array one by one (if thats what you were asking?) – ObedA. Sep 30 '20 at 18:59
  • I don't understand the use of 'x' in the statement "myArr[x] = 42" just see the code above and you will come to know what I didn't understand. – Naitik Oct 01 '20 at 02:55
  • @NaitikKumarSrivastava Looking at the code, the for loop is supposed to set the value stored in each element to 42. That is just like writing: ` myArr[0] = 42; myArr[1] = 42; myArr[2] = 42; myArr[3] = 42; myArr[4] = 42; `. Now you might have noticed that this is redundant and might get pretty hectic if the array was bigger than just 5 elements (say 100 elements; you'd have to write the same statement a 100 times!) So in order to "automate" this procedure, the for loop is used. – ObedA. Oct 02 '20 at 09:20
  • The ` myArr[x] ` part just acts as a generic way to reference each element in the array. Varying the values of **x** from 0 to 4 will mean substituting the values from 0 to 4 in myArr[x]. Therefore, using the for loop, we can easily perform the task of setting the values of all the elements in myArr to 42. I hope this clears your confusion :) – ObedA. Oct 02 '20 at 09:23