Saturday, May 22, 2010

Microsoft Visual C++ question about arrays?

Hi. I'm a beginner I.T. Student, and we're dealing with C++, specifically Arrays. What is/are the major advantage/s of a two-dimensional array over a single-dimension array?





Please, I need to know.





Thanks in advanced.

Microsoft Visual C++ question about arrays?
Let's say you need a 'table' containing 25 elements. You can do this as a single dimensional array, knowing that your rows are 5 columns wide (you would have to keep this value stored somewhere if the table size was dynamic). Accessing element (1,2) would be the 5 * 1 + 2 = 7th index in this 1D array. Or you can use a 2D array, in which case you simple access it as myArray[1, 2]. The second is easier to understand as an ordered pair, whereas the first looks pretty hacky.
Reply:one important use of arrays to its ability to store multiple values of the same type. unlike a structure which can store multiple data of different type.





Note the difference: arrays = same type





a 1D array is simply like storing values in each. it only acts as a storage and is not capable of multiple storage or even operations on array.





a 2D array can be used to compare values as well.


example, you can compare the value in array[0][0] with value in array[1][0].





information retrieval:


yes, arrays can be used to store multiple inputs as stated earlier and as well can retrieve this data.





a 2D array is in general more flexible than a single array


it can also give you some kind of shape unlike the 1D array


it gives you something like a dimension (2D like seeing a flat picture of your array)
Reply:in one dimensional array, you can only do single column entries..on a two dimensional array you can almost make a table out of it..





A[1] == one dimensional array





A[1,2] == two dimensional array


No comments:

Post a Comment