Post: Using arrays in loops?i need help
05-16-2017, 12:12 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); why do people write loops using arrays like this
    #include <iostream>
using namespace std;

int main(){
int arr[10];

for(int x=1;x<=9;x++){
arr[x]=20;
cout<<x<<"::"<<arr[x]<<endl;
}
}

instead of just writing it like this
    #include <iostream>
using namespace std;

int main(){
int arr;

for(int x=1;x<=9;x++){
arr=20;
cout<<x<<"::"<<arr<<endl;
}
}
05-16-2017, 12:29 AM #2
Specter
Pro Memer
Originally posted by mudville209 View Post
why do people write loops using arrays like this
    #include <iostream>
using namespace std;

int main(){
int arr[10];

for(int x=1;x<=9;x++){
arr[x]=20;
cout<<x<<"::"<<arr[x]<<endl;
}
}

instead of just writing it like this
    #include <iostream>
using namespace std;

int main(){
int arr;

for(int x=1;x<=9;x++){
arr=20;
cout<<x<<"::"<<arr<<endl;
}
}


In the first example you're creating an array of 10 elements (size of 40 bytes in memory) and setting each element to the number 20 and outputting the iteration number of the loop as well as element x's value inside of the array "arr" (also in this example you're missing element 0 as you set x = 1, array indexes are zero-based).

In the second example you're just creating a normal variable (size of 4 bytes in memory) and setting it to the number 20 nine times and outputting it 9 times. Switch;

    arr[x] = 20;

to
    arr[x] = x;


in the first example and you'll see what I mean. First example will produce an output similar to:

    
1::1
2::2
3::3
4::4
5::5
6::6
7::7
8::8
9::9

The second example will produce:
    
1::20
2::20
3::20
4::20
5::20
6::20
7::20
8::20
9::20

The following user thanked Specter for this useful post:

mudville209
05-16-2017, 12:47 AM #3
okay gotcha but does the array index always have to be zero
05-16-2017, 01:03 PM #4
Originally posted by mudville209 View Post
okay gotcha but does the array index always have to be zero


Do you understand what arrays are?

I'll try to make it as simple as I can, so it won't be technically accurate but it might help you understand what are arrays and why we use them. Sorry if my english is not the best.

As you might now, variables are like "tags" ( the name of the variable is that tag) you use to refer to bits of space allocated in memory. Variables have a type ( int, float, char, etc. )
Suppose that you are trying to achieve something which requires you to manipulate a lot of variables. You would need to find a different name for each of them and keeping track of all those variables and how you use them would be difficult. Arrays exist has a solution to this problem.
Arrays allow you to refer to a large collection of "variables", all of the same type, through one name ( the name of the array ) and an index.
Since you are working with a collection of "variables", at some point in your code you might need to work on one particular variable within this collection. So you need a way to access it.
That's what indexes are for.

Let's declare an array of integers.

    int my_array[10];


Few things about arrays:

All the elements within an array need to be of the same type. That's why it looks very much similar to when you declare a variable.
An array has a fixed size, which means it has a fixed count of elements it contains, and this size has to be defined upon the declaration of the array.

In the previous example "int" is the type of the structures contained in your collection.
The number "10" between brackets, is the size of your collection, the number of structures you want your array to contain. Notice the syntax with the brackets, this is probably what you find confusing.

When you declare the array, with its type and its size, brackets are used to establish the size of it.
Later in your code, when you refer to the array, brackets are used to access a specific element in your array.
Do not forget this.

So let's say I need to access the fourth variable in my array, to know what value lies in it.

    int a_variable = my_array[3];


That is how you do it. So has you can see the brackets are now used to refer to a particular variable in the array, and not to establish its size.
You might be wondering why is the index equal to "3", since you're trying to access the fourth element in your array, though.
That's because the first element of your array is always located at index 0.

Which means if you want to try to access the first element, you would have to do the following:

    
int my_first_element = my_array[0]; // Here you access the first element
int my_second_element = my_array[1]; // Here you acces the second element
int my_third_element = my_array[2]; // Here you access the third element
// etc...
Last edited by theDaftDev ; 05-16-2017 at 03:32 PM.

The following user thanked theDaftDev for this useful post:

mudville209

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo