Introduction to Arrays

Beginner

Introduction

An array is a collection of items stored in contiguous memory locations. Each element in an array can be accessed by using its index value. In this lab, we will learn how to create and manipulate arrays in C++.

Include Required Libraries & Define Namespace

We will create a new file named main.cpp in the ~/project directory using the following command:

touch ~/project/main.cpp

We begin by including the necessary libraries and defining the std namespace using the using namespace std; statement.

#include <iostream>
using namespace std

Declare and Initialize the Array

In this step, we will declare an integer type array and initialize it with some values.

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};
}

Accessing Array Elements

In this step, we will access the elements of the array using their indices. Here, we will print the first element of the array.

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};
    cout << "The first element of the array is: " << arr[0] << endl;
}

Modifying Array Elements

In this step, we will modify an element in the array by assigning a new value to it. Here, we will replace the second element of the array with a new value of 5.

int main()
{
    int arr[5] = {8, 2, 3, 7, 1};

    // modifying second element of array
    arr[1] = 5;

    cout << "The modified array is: ";
    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

Taking Array Input from User

In this step, we will take array elements as input from the user. First, we will ask the user for the number of elements they want to enter. Then, we will create an array of that size and populate it with the user's input.

int main()
{
    int n;

    cout << "Enter the number of elements: ";
    cin >> n;

    int arr[n];

    cout << "Enter " << n << " elements: ";
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }

    cout << "The elements of the array are: ";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

Sorting the Array

In this step, we will sort the array in ascending order using the Bubble Sort algorithm. This algorithm works by repeatedly swapping adjacent elements if they are in the wrong order.

int main()
{
    int arr[] = { 5, 7, 4, 6, 2 };

    int n = sizeof(arr) / sizeof(arr[0]);

    // Bubble Sort Algorithm
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    cout << "The sorted array is: ";
    for (int i = 0; i < n; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

To run the code in the terminal:

g++ main.cpp -o main
./main

Summary

This lab has covered the basics of working with arrays in C++. We learned how to create and initialize an array, access and modify its elements, take user input, and sort the array using a simple Bubble Sort algorithm. With this knowledge, you should be able to create and manipulate arrays in C++ effectively.

Other Tutorials you may like