+ 1
Cant understand abt arrays ...pls help me in C++ n C
3 Answers
+ 2
arrays store elements of a specific type, for example integers, you can think of them like separated shelves, every shelf can store an element., the shelves start from zero. So the first element is in shelf 0. Usually you can store just one element in one variable, but with arrays you can 'store' many elements in one variable. Anything unclear? Please ask :)
+ 2
check out > http://www.cplusplus.com/doc/tutorial/arrays/
+ 2
When I explain arrays to people, I tell them this:
Let's say you need 5 integers to work with. So, you do this:
int x1, x2, x3, x4, x5;
That's a bit cumbersome though, isn't it? 5 is one thing, but imagine if we needed 20, or 100, or 1000. I hope you have a lot of time on your hands.
Alternatively, we can use an array:
int x[5];
Semantically, this is the same thing. But instead of declaring each integer individually, we just go ahead and declare 5 of them all at once. We can access each integer separately using x[0], x[1], x[2], x[3], and x[4]. Pretty convenient, especially if you're going to be, say, printing each one of them. You can now use a loop instead of having to write a bunch of separate print statements.
Of course, there's a lot more to arrays, but that's the basic idea. Instead of one variable, you can picture it as a collection, or literally an array, of variables.