In C++, sometimes storing data in variables are not reliable, especially if the number of inputs is not consistent or too big (you cannot make 1.000.000 variables fast enough, right?). In that case, storing numbers in an array is a good choice. Let's see how we can use arrays. There are two types of arrays: array, and matrix. Vectors are not considered in this article.
Array This is the simplest form of array. It stores data in 'blocks'. The indexes are numbered from a[0], a[1], a[2], a[3], ...
Declaring an array
Declaring an array is really simple. The following line of code will show you how to declare it:
type of data + name of array[size of array];
For example: "double a[5]" is an array with the name 'a', storing numbers of type with size 6 (a[0], a[1], a[2], a[3], a[4], a[5])
Note: Don't make an array too big, it can overflow the code and cause it to terminate.
Input/Output numbers into an array
Just use cin and cout. In case you have different test cases, use the 'for' loop:
For example: for (long long i = 0; i < 5; i++){ cin >> a[i]; } is the code required to register values to a[0], a[1], a[2], a[3], and a[4]. You can sometimes use 'for (long long i = 1; i <= 5; i++)'; but the compiler here will say 'out of bounds' (not recommended).
Outputing indexes in an array is easy too. Just use cout and the for loop and done!
Miscalleneous
If you put a new value to an index, it will overwrite it.
Matrix Matrices are pretty similar to arrays, but it is two-sided.
Declaring a matrix
A matrix is basically a m x n board of data. Declaring it is very ez.
To declare an array with m rows and n columns, use the following code:
type of data + name of array[m][n]
For example: double k[9][10] is a matrix with 9 rows and 10 columns
Calculations and input/output in a matrix is done by using two for loops inside each other. After that, just do it like an array. Remember: to call out a cell at the ith row and the jth column, use a[i][j].
Hope you enjoyed the article!