0
What's an array?
describe it
4 Antworten
+ 5
In short :
- It stores datas, like a variable, but in a varying amount. If you studied 'sets' in math, arrays are something like that.
+ 2
it's already well described in other questions and the tutorials themselves. Use the search functionality in the Q&A section of the app. Another resource: don't forget that google (or whatever your search engine) is your friend and can lead you to excellent readings and videos.
+ 1
An array in c++ helps us to be able to store various 'values' in a single variable. It makes it easier to manipulate values that are related.
You may be wondering why you need an array, right?. Well let me explain.
For instance if you want to store a set of values such as numbers from 1 to 5. If you don't use an array, you can do it by declaring five different variables.
That is,
int a=1;
int b=2;
int c=3;
int d=4;
int e=5;
But the problem with declaring it like that is that the variables a-e are stored differently and so it is difficult or impossible to do some types of manipulations with them like that. Also, to output them, you have to call each variable differently.
That is;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
cout<<e<<endl;
Now this is a stressful way to write code.
But declaring an array makes it much easier.
Eg.
int numbers [5]={1,2,3,4,5}; //this shows that the array 'numbers' which is meant to hold five variables holds numbers from 1-5//
for (int x=0; x<5;x++){
cout<<numbers[x]<<endl;} //this line means that the '[x]' in 'numbers[x]' will keep increasing as it loops until the five numbers are output. Remember that in the for statement, I made x=0. So this means that numbers[0]=1,numbers[1]=2,numbers [2]=3,numbers[3]=4,numbers[4]=5. The code stop looping when x is 4 because the 'for' statement tells it to stop at 'x<5'. Therefore
//output
1
2
3
4
5
//
hope this helped
0
you can see it as a storage with a fixed size.