- #1
realism877
- 80
- 0
I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.
realism877 said:I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.
int arrayLength = 30;
int myArray[arrayLength ]; // an array with 30 "slots", pretend we already put the data in
int total = 0;
for(int i = 0; i < arrayLength; i++)
{
total = total + myArray[i];
}
average = total / arrayLength;
int height1, height2, height3, height4, height5, height6 ... height30; // 30 variables!
total = height1 + height2 + height3 + height4 ... + height 30; // !
average = total / 30;
Using an array, you can store multiple values of the same time, using essentially a single name. The individual items in the array can be distinguished by an index.realism877 said:I know what an array is and what it does. I'm just having a hard time getting the concpet on how it is useful.
Assuming that we're talking about programming languages that store strings as arrays of characters. That would include C, C++, and several other languages.Jaynte said:A text string is also a good example.
array[0] == 'A'Jaynte said:The above text i just wrote is an array with characters where array[0] = A, array[1] = "space", array[2] = t. and so on.
An array is a data structure used to store a collection of elements of the same data type. It allows for easy access and manipulation of the data by indexing each element in the array.
Arrays are used to store and organize large amounts of data in a structured and efficient manner. They also allow for easy access and manipulation of the data, making it useful for tasks such as sorting and searching.
The purpose of an array is to provide a way to store and access a collection of data in a sequential manner. It allows for efficient data retrieval and manipulation, making it a useful tool for solving various programming problems.
Arrays offer several benefits, including efficient data storage and retrieval, easy access to specific elements, and the ability to perform common operations such as sorting and searching. They also allow for code reusability, as the same array structure can be used for different sets of data.
In most programming languages, arrays can only hold elements of the same data type. However, some languages allow for arrays to hold different data types by using a data structure called a "variant array" or "heterogeneous array". These types of arrays are less efficient and are not commonly used.