Organize Your Data
Arrays allow programmers to store a series of data of the same type. For example, suppose a program is to keep track of twenty numbers. Such a set of numbers would be stored in an array of integers (whole numbers) or doubles or floats (decimals). Arrays work very similarly to variables in that they must be declared and are given a type. The only difference is that each time an array is referred to, brackets indicating the index that you are referring to must be used. Unless passing in and out of a function (as in a function header), arrays cannot be referred to as a whole. Rather, the index one is referring to must be specified. Array indices for an array of size n begin at 0 and increment by 1 to n-1.

Graphical Depiction Of Arrays


my_array[5]

0 12
1 28
2 83
3 18
4 2


The above is a graphical depiction of an array of integers named my_array of size five. It would be declared by stating int my_array[5]; Doing so would tell the computer to leave room in RAM for five integers. From then on, each can be referred to as if it were an individual variable. For example, to access the number 83 one would make reference to the variable my_array[2]. Array indices can be worked with exactly as other variables. In addition, the index of an array may be a variable itself. For example, it is possible to make reference to my_array[x] where x represents some integer in your program within the boundaries of the size of my_array.


Practical Uses Of Arrays
Arrays are often used in combination with For loops. For example, one can run a for loop going from 0 to n-1, referring to an array of size n. In such a case, each iteration of the loop can work with arr[n]. For example, this is a simple process when the same operation needs to be performed on multiple values (each value in the array).

Please also note that a string (such as a word) is often depicted in C++ as an array of characters. For example, someone's name consisting of 5 letters would be declared as char name[5];.

2D Arrays
Programmers also have the option of working with two-dimensional arrays, which can be visualized as a simple spreadsheet of cells. Each cell is represented by two indices: [x] and [y]. If visualized, [x] represents the vertical "coordinate" while [y] represents the horizontal "coordinate." Note that this is the opposite of the mathematical (x,y) axis, where the horizontal is listed first.

2D arrays allow the programmer to store a virtually unlimited amount of data. In addition, by visualizing data as side-by-side, one could associate "column X" with "column Y" (just as long as the data type is the same.) Fortunately, however, the C++ language provides a much more efficient way to manage data retrieval using OOP (object oriented programming).

Organizing Your Data
Arrays are extremely important to programming as they allow an easy method for not only storing data, but for organizing it as well. For example, by placing ten numbers into an array, the programmer can easily call up the 3rd element, 5th element, or even nth element in the array. For such a reason, arrays are often used with loops.

Memory Allocation
The size of an array must be declared upon the creation of the array. This is because an array is simply a collection of elements (blocks) of memory in a computer's RAM. Upon the declaration of an array, the computer reserves X amount of space which cannot be altered within a program. For such a reason, though powerful and necessary, nonetheless, arrays are the simplest way to store a collection of data.

When arrays are declared, one can actually specify the value for each value in the array at this time. This is done in a similar way as if one was setting a value to a variable. The only difference is that each value of the array is separated by a comma, and they are all enclosed in a set of brackets. For example: int arr[3] = {3, 9, 27};

A string, a sequence of letters and symbols, is actually repersented in the C++ language as an array of characters. For this reason, one can declare a string "hello" as char word[5] = "hello"; It should be noticed that the word appears in double quotes, while single characters are always enclosed in single quotes.

Please also note that all arrays end with the null character, \0 (which is taken as a single character.) For example, the following is an example of how the word "hello" is expressed in RAM, the computer's memory:


"hello"

0 h
1 e
2 l
3 l
4 o
5 \0


Searches and Sorts

Although simple, arrays are popular in that they are very versatile. By using a sequence of loops and boolean conditions, one can perform searches and sorts on the data. Sorts include a bubble sort while searches include a linear search. Linear searches simply search for a given condition from the first element and work their way up until the element has been found. The search will then return the index of the matching value in the array.

how can i make a program using arrays toform a half matrix

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.