Pages

Wednesday, October 4, 2017

Easy tutorial for C / C ++ - Arrays 1 (One dimensional array)

Easy tutorial for C / C ++ - Arrays 1 (One dimensional array)

array :

- It is a set of storage space which is allowed you to store data of the same type. This can make data management easier and more flexible.

- For example, suppose you want to use 100 integer data. Then you have to define 100 variables that can store integer data, and you have to assign values to each variable.

When not using arrays :
int a, b, c, ......xxx ; (Write all 100 variables)

- Using arrays can make things easier. Define only one array that handles integer data and define the number of elements in the array as 100.

When using arrays :
int a[100]; (Create only the array name, and enter 100, the number of elements)

- The basic way to define an array is as follows :

'Data type' 'array name' ['number of elements']

'Data type'     : The data type of the data to be stored in the array.
'Array name'     : The name of the array to be used.
'Number of elements'     : The number of elements in the array. Starting at 0. If n data is used, the element index is used from 0 to n-1.

- Let's look at a simple example.

Example Code

#include <iostream>
using namespace std;

int main() {
     int arr[5] ;

     arr[0] = 1 ;
     arr[1] = 2 ;
     arr[2] = 3 ;
     arr[3] = 4 ;
     arr[4] = 5 ;

     for(int i = 0; i < 5; i++){
             cout << "  arr[" << i << "] = " << arr[i] << endl;
          }
     return 0;
}

- We have defined an integer array 'arr' with 5 elements. And we assign integer values to each element.

Assigned to first element        : arr[0] = 1 ;
Assigned to second element   : arr[1] = 2 ;
Assigned to third element       : arr[2] = 3 ;
Assigned to fourth element     : arr[3] = 4 ;
Assigned to fifth element        : arr[4] = 5 ;

- The for loop is used to print the values in the array in element order.

results :

  arr[0] = 1
  arr[1] = 2
  arr[2] = 3
  arr[3] = 4
  arr[4] = 5

- You can see that the value assigned to each element of the integer array 'arr' is output.

Another way to declare array:

(1) Declare an array with the number of elements and initial value.

- At the same time as declaring an array, you can use braces {} to declare an array as shown below.

int arr[5] = {1, 2, 3, 4, 5} ;

(2) Declare an array with the number of elements and initial value. Given an initial value less than the total number of elements.

int arr[5] = {1} ;

- Set the number of elements in the array to 5, give only one initial value. And then, 0 is stored in the remaining four elements automatically. If you change the definition part of the array in the previous example, you will see the following results.

  arr[0] = 1
  arr[1] = 0
  arr[2] = 0
  arr[3] = 0
  arr[4] = 0

(3) Declare array only with initial value.

- The compiler automatically determines the number of arrays by the number of element values when the number of arrays is not given.

int arr[] = {1, 2, 3, 4, 5} ;

No comments:

Post a Comment