+ 2
Help me in understanding arrays in c++.
Can anyone help in understanding arrays and multidimensional arrays? I want to know that in which way arrays are useful. Any suggestion and also i want to know that what is the difference between arrays and multidimensional arrays. Give me a brief example and answer about the questions i asked here please!
1 Answer
+ 9
Array:
A collection or list of a single data-type.
If I create an int array of size 5, it's the same as doing:
int a, b, c, d, e;
However when it comes to printing these in a certain order it would become a mess. So an array is just a nice way of dealing with a bunch of the same data types, whether it being sorting, removing, or adding lots of data.
As for multidimensional, it gets a bit more complicated.
You may here a 2d array[][] is rows and columns, but that's kinda just an analogy to help see what it looks like, it is really an array of arrays.
array[][];
columns.
rows [0][0] [0][1]
It's really just
array [x]
array [x][0] [x][1]
The array in element [] is pointing to another array. So you have size*size more elements to deal with. However you can only grab information/data from the final array, because the array[] is just pointing to another array, not any data.
Example/
(In Java, please ignore any syntax differences to c++, it's the same concept)
int[][] arr = new int[5][4]
/*
Now i've made 5 arrays that point to other arrays of size 4.
*/
System.out.println(arr[2]);
Output: @371fwh7...
/*
This will give me the location in memory, or reference of array[2], which points to a bunch of other arrays.
*/
System.out.println(arr[2][3]);
Output: 0;
/*
This will actually give me data. I'm grabbing the arr[2] which is an array, and getting the [3] (or 4th) array that it's pointing to
*/
Hope that helps!