Arrays in C ++
Hi friends, The subject of this article is Arrays. I’m going to try to explain what we need to know about arrays.
Introduction to Arrays
Array can be called the cluster where the same type of data is collected. int, float, double, char, such as array can do.
Declaring Arrays
int score [5]; // We have defined an array of type int with a size of 5. And we called our score score.
score [0], score [1], score [2], score [3], score [4] // elements of our series
Since the index starts from 0, one of the dimensions is missing.
Accessing Arrays
cout << score [3]; // this way, we can reach the index 3 of the array and write to the screen.
score [n + 1] = 67; // we can assign any value we want to any index of the directory.
// sample code snippet3
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main () { int i, score [5], max; cout << "Enter 5 scores: \ n"; cin >> score [0]; max = score [0]; for (i = 1; i <= 5; i ++) { cin >> score [i]; // received 5 points from the user if (score [i]> max) max = score [i]; // the largest of the 5 numbers entered } cout << "The highest score is" << endl; return 0; } |
We can do many operations with arrays. Taking the value from the user, filling in the bi directory, finding the smallest, largest element of the array, writing all the elements of the array to the screen etc. many operations can be performed.
Defined Constant as Array Size
It is always more advantageous to define the size of an array as constant.
Increases readability. (readability)
increases productivity. (Versatility)
provides sustainability. (maintainability)
// sample identification
const int NUMBER_OF_STUDENTS = 50;
int score [NUMBER_OF_STUDENTS];
Ranged-Based For Loop
With C + + 11 version, it has become easier to do loops for a given range of given array.
2 3 4 5 |
for (datatype varname: array) // format as follows { ......... } |
2 3 4 5 6 7 8 9 10 11 12 |
// sample usage int arr [] = {10, 20, 30, 40}; // given array / * to print individual elements until the array is terminated in this for loop * / for (int x: arr) cout << x << ""; cout << endl; / * When the elements in the array are finished, the cycle ends, the program resumes. * / Our output is as follows: 10 20 30 40 |
Initializing Arrays
When defining the Array we have specified that it should have a type, name and size. When initializing, using {}, we assign the first value to the indexes.
Example use: int children [3] = {2, 12, 1};
// the above operation gives us a value of 2, 2, 1, 2, 1, 2, 3, 1, 3, 3
children [0] = 2;
children [1] = 12;
children [2] = 1;
Auto-Initializing Arrays
int b [] = {5, 12, 11}; // where we initialize with 3 values, the size of the array is automatically 3.
Arrays in Functions (Using Arrays in Functions)
- The arrays can be sent as a parameter to the functions.
- Functions can return an array.
- The functions can use any index of the array as a parameter. -> myFunction (myArray [3]);
- Array type and size can be sent to the function with different parameters. -> void fillUp (int x [], int size);
- The size of the array is not required where the function is called, without brackets, it is called by its name only. -> fillUp (score, sizeOfScore);
- If we don’t want to make a change to the array we send as a parameter, we can send it as a constant parameter. Thus, we guarantee that no changes will be made to the array. -> double average (const int a [], int size);
- Series; We can easily use it when programming for sorting, searching.
Multidimensional Arrays
char book [24] [120]; // two-dimensional array
book [0] [0], book [0] [1] [..book [0] [119]
…………………………………………………………………………………………….
book [23] [0], ………………………………………………… book [23] [119]
2 3 4 5 6 7 8 9 10 11 |
// Implementing the Sample Function for Sending Two-Dimensional Array as a Parameter void DisplayPage(const char p[][100], int sizeDimension1) { for (int index1=0; index1<sizeDimension1; index1++) { for (int index2=0; index2 < 100; index2++) cout << p[index1][index2]; cout << endl; } } |
Finally, it should be noted that arrays of arrays start at 0. If the size of the array is exceeded, the program jumps to a point that we do not want and can receive a segmentation fault. Although it does not cause a visible error, it can also lead to incorrect operations.
if you have question do not forget to write from the chat button next to it or from the comment
I like this web site its a master peace ! Glad I observed this on google .