C++ Array

In this tutorial, we will practice using arrays. We will learn how to declare, initialize, and access array items in C++ programming with the help of examples.

An array is a variable in C++ that can hold multiple instances of a single type of value. For instance,

Let’s say there are 12 students in the class, and we need to store their grades altogether. We can create an array rather than 12 separate variables:

double grade[12];

In this case, the grade is an array with a 12-element double-type limit.

In C++, an array’s size and type cannot be modified after it has been declared.

C++ Array Declaration

dataType arrayName[arraySize];

For example,

int x[20];

Here,

  • int – a type of element to be stored
  • x – name of the array
  • 20 – the size of the array

Access Elements for Arrays in C++

In C++, each element in an array has a unique number. An array index is the term used to describe the value. These indices allow us to access an array’s elements.

// syntax to access array elements
array[index];
array
Elements of an array in C++

A Few Things to Bear in Mind

  • The array’s indexes begin at 0. Meaning that the first item stored at index 0 is x[0].
  • The last element of an n-element array is stored at index (n-1). The last component in this example is x[5].
  • An array’s elements have sequential addresses. Consider the scenario where x[0starting ]’s address is 2120.

The next element, x[1], will then have an address of 2124, then x[2], 2128, and so on.

In this instance, the size of each element has increased by four times. This is due to the fact that int has a 4-byte size.

Array initialization in C++

An array can be initialized during declaration in C++. For instance,

// declare and initialize an array
int x[6] = {9, 120, 28, 37, 39, 35};
array
C++ Array elements and their data
// declare and initialize an array
int x[] = {9, 120, 28, 37, 39, 35};
 35};

The array’s size is not indicated in this sentence. In these circumstances, the compiler determines the size automatically.

How do you add and print elements to an array?

int mark[5] = {24, 210, 28,317,29}

// change 4th element to 9
mark[3] = 4;

// take input from the user
// store the value at third position
cin >> mark[2];

// take input from the user
// insert at ith position
cin >> mark[i-1];

// print first element of the array
cout << mark[0];

// print ith element of the array
cout >> mark[i-1];

Example 1: Displaying Array Elements

#include <iostream>
using namespace std;

int main() {
//geektocode.com
  int numbers[5] = {22, 33, 221, 11, 77};

  cout << "The numbers are: ";

  //  Printing array elements
  // using range based for loop
  for (const int &n : numbers) {
    cout << n << "  ";
  }

  cout << "\nThe numbers are: ";

  //  Printing array elements
  // using traditional for loop
  for (int i = 0; i < 5; ++i) {
    cout << numbers[i] << "  ";
  }

  return 0;
}
OUTPUT:
The numbers are: 22  33  221  11  77  
The numbers are: 22  33  221  11  77  

Note that instead of using int n as the range declaration in our range-based loop, we used the code const int &n. The const int &n, however, is more favored because

  • When using int n, each iteration just involves copying the elements of the array to the variable n. This uses a lot of memory.

However, &n accesses the data of the array elements directly from memory rather than by copying it to a new variable. This is efficient with memory.

  • The array items are not being changed; we are just printing them. So that we don’t unintentionally alter the array’s values, we utilize const.

Example 3: Using a for loop, display the sum and average of an array’s elements

#include <iostream>
using namespace std;

int main() {
    //geektocode.com
  // initialize an array without specifying size
  double numbers[] = {3, 53, 36, 212, 235, 127};

  double sum = 0;
  double count = 0;
  double average;

  cout << "The numbers are: ";

  //  print array elements
  // use of range-based for loop
  for (const double &n : numbers) {
    cout << n << "  ";

    //  calculate the sum
    sum += n;

    // count the no. of array elements
    ++count;
  }

  // print the sum
  cout << "\nTheir Sum = " << sum << endl;

  // find the average
  average = sum / count;
  cout << "Their Average = " << average << endl;

  return 0;
}
OUTPUT:
The numbers are: 3  53  36  212  235  127  
Their Sum = 666
Their Average = 111

Leave a Reply

Your email address will not be published. Required fields are marked *