LATEST

How to use arrays in C / C++

In this tutorial, we will learn how to use array in C / C++. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

We will learn to declare, initialize, and access array elements in C++ programming with the help of examples.

In the C and C++ programming languages, an array is a variable that can store multiple values of the same data type.

For example: Suppose in a class there are 30 students. To store student information, instead of declaring 30 variables, we can use an array with 30 elements.

So, how to declare an array in C / C++? How to use it? Let's get started now !!!

What is an array in C / C++?

In the C and C++ programming languages, array is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array.

They can be used to store the collection of primitive data types such as int, float, double, char, etc of any particular type. In addition, arrays can also store derived data types such as structures, pointers, etc.

array 011 PNG

Why do we need arrays?

When we have a large number of variables of the same data type. Instead of using the usual way of storing by declaring variables, we can use an array to store.

Let's take a look at some advantages and disadvantages of arrays in C / C++:

Advantages:

  • Retrieves random elements, based on the array's indices.
  • The program looks cleaner, more optimized.
  • Easily access array elements.
  • Easy data manipulation.
  • Easily sort data.

Disadvantages:

  • The array has a fixed size.

Declare arrays in C / C++

To declare an array, define the variable type, specify the name of the array followed by [ ] and specify the number of elements it should store.

dataType arrayName[arraySize];

Here,

  • dataType: This is the data type of the array, which can be int, float,... .
  • arrayName: This is the name of the array, we will use it when calling the array.
  • arraySize: This is the size of the array. It determines how many elements an array can store.

For example: Suppose we want to declare an array of integers number[] with 6 elements.

int number[6];

Initializing arrays in C / C++

When declaring an array, we can initialize the value of the array, for example as follows:

// declare and initialize and array
int number[6] = {1, 2, 3, 5, 6, 7};

In the above code, the array number[] has 6 elements and we initialize it with the values {1, 2, 3, 5, 6, 7} respectively.

*Note: Array size is set in [ ] and array elements are set in { }.

Another method to initialize array during declaration:

// declare and initialize and array
int number[] = {1, 2, 3, 5, 6, 7};

In addition, we can initialize an element in the array. Instead of initializing the elements of the array.

int number[5];
number[0] = 1;

Adding elements to an array in C / C++

To add elements to the array, we need to know the size of the array.

Use a for loop to iterate from 0 to the size of the array. For each iteration, we perform value initialization for an element. The result after the loop ends, we get an array of elements.

For example: The program adds elements to the array.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

array 02 PNG

Access the elements of an array in C / C++

In the C and C++ languages, the elements of an array are associated with a number. That number is called the index of the element. We can access the element of the array through it.

// syntax to access array elements
array[index];

Example: Let's say we have an array of numbers [] = {1, 2, 3, 4, 5, 6}. Now to get the 3rd element in the array and then assign it to the variable N, we do the following.

int N = number[3]; // output N = 4

So how can I access all the elements in the array? See the example below.

Example: Retrieve the elements in the array.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

array 03 PNG

Examples of arrays in C / C++

Here, we have two examples with array in C/C++. These are basic examples when using array in C/C++.

Example 1: Displaying elements even numbers in arrays in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  cout<<"\nEven numbers in array: ";
   for(int i=0; i<n; i++){
      if(arr[i] % 2 == 0)
        cout<<arr[i]<<" ";
   }

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  printf("\nEven numbers in array: ");
   for(int i=0; i<n; i++){
      if(arr[i] % 2 == 0)
        printf("%d ",arr[i]);
   }

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

array 04 PNG

Example 2: Find the sum of elements in array in C / C++

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n, sum = 0;
  do{
    cout<<"Enter the size of the array: ";
    cin>>n;
    if(n <= 0) cout<<"Please enter the size of the array greater than 0 !!!\n";
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    cout<<"Enter the element "<<"[" <<i<<"]: ";
    cin>>arr[i];
  }

  cout<<"Elements in array is: ";
  for(int i = 0; i <= n - 1; i++){
    cout<<arr[i]<<" ";
  }

  for(int i=0; i<n; i++){
      sum += arr[i];
   }
   cout<<"\nSum of elements in array: "<<sum;

  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n, sum = 0;
  do{
    printf("Enter the size of the array: ");
    scanf("%d", &n);
    if(n <= 0) printf("Please enter the size of the array greater than 0 !!!\n");
  }while(n <= 0);
   
  int arr[n];
  for(int i = 0; i <= n - 1; i++){
    printf("Enter the element [%d]: ",i);
    scanf("%d", &arr[i]);
  }

  printf("Elements in array is: ");
  for(int i = 0; i <= n - 1; i++){
    printf("%d ",arr[i]);
  }

  for(int i=0; i<n; i++){
      sum += arr[i];
   }
   printf("\nSum of elements in array: %d",sum);

  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

array 05 PNG

Above are two basic examples when using array in C / C++. You can check out more examples here.

Cùng chuyên mục:

How to use strings in C / C++

How to use strings in C / C++

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top