One Dimensional Array (part 1)

Estimated reading: 7 minutes 157 Views

An array is a data structure that stores a collection of elements of the same data type. These elements are stored in contiguous memory locations, and they can be accessed using an index. Arrays provide a way to manage and manipulate a large set of data efficiently.

In C++, you can declare an array by specifying its data type and the number of elements it will hold. Here’s a basic syntax:

data_type array_name[array_size];

Arrays in C++ are zero-indexed, meaning the first element is accessed using the index 0, the second element with index 1, and so on. You can access and modify array elements using square brackets:

array_name[index] = new_value;

Here is an example:

#include <iostream>
using namespace std;

int main()
{
    int x[5];
    x[0] = 10;
    x[1] = 20;
    cout << x[0] << endl;
    x[2];
    x[3];
    x[4];

    return 0;
}

1. int x[5];

This line declares an integer array named x with a size of 5. It means that x can store five integer values. However, at this point, the values in the array are uninitialized, which means they contain random data.
2. x[0] = 10;

Here, we assign the value 10 to the first element of the array x. In C++, arrays are zero-indexed, so x[0] refers to the first element.
3. x[1] = 20;

Similarly, we assign the value 20 to the second element of the array x, which is x[1].
4. cout << x[0] << endl;

This line prints the value of the first element of the array x, which is 10, to the console. It uses the cout object from the C++ Standard Library to output the value, followed by an end-of-line character (endl) for formatting.
5. x[2];

These lines appear to be accessing the third element of the array x, but they don’t do anything. It’s essentially a “noop” operation, and the value in x[2] remains uninitialized or unchanged.
6. x[3]; and x[4];

Similar to the previous line, these lines access the fourth and fifth elements of the array x, but they also don’t perform any meaningful operation.

Output:

10

Here is another example:

#include <iostream>

using namespace std;

int main()
{
    int x[5];
    x[0] = 10;
    x[1] = 5;
    x[4] = x[0] + x[1];
    cout<< x[4] << endl;

    return 0;
}

Output:

15

Arrays only accept constant array size, Example:

#include <iostream>

using namespace std;

int main()
{
    const int t = 5;
    int x[t];

    return 0;
}

This code declares an integer array `x` with a size of 5, and the size is determined by the constant `t`, which is set to 5. The `const` keyword ensures that `t` cannot be changed later in the program. This code doesn’t perform any further operations on the array; it simply demonstrates how to declare an array with a size determined by a constant.

You can also declare the size of your array, and declare the initial values of your array:

#include <iostream>

using namespace std;

int main()
{
    int x[5] = { 1,4,8,7,2 };
    cout << x[0] << endl;

    return 0;
}

This code declares an integer array x with a size of 5 and initializes it with the values 1, 4, 8, 7, and 2. It then prints the first element of the array, which is 1, to the console. This code demonstrates how to create an array and access its elements in C++.

But what if you declared no initial values into your array?

#include <iostream>

using namespace std;

int main()
{
    int x[5];
    cout << x[3] << endl;

    return 0;
}

This code declares an integer array x with a size of 5 but does not initialize its elements. Therefore, when you attempt to print the value of the fourth element, it will display a random or garbage value. It’s important to initialize array elements before using them to avoid unpredictable behavior in your programs.

What if you declared only one initial value that equals to 0?

#include <iostream>

using namespace std;

int main()
{
    int x[5] = {0};
    cout << x[3] << endl;

    return 0;
}

This code declares an integer array x with a size of 5 and initializes all its elements to 0. It then prints the value of the fourth element of the array, which is 0, to the console. This code demonstrates how to create an array with specific initial values in C++.

You can also declare a size of array and declare some of the first initial values, for example:

#include <iostream>

using namespace std;

int main()
{
    int x[5] = {1,2,3};
    cout << x[3] << endl;

    return 0;
}

This code declares an integer array x with a size of 5 and initializes the first three elements with values, while the remaining two elements are automatically initialized to 0. It then prints the value of the fourth element, which is 0, to the console.

Or, you can declare no size of your array and only declare the initial values of your array:

#include <iostream>

using namespace std;

int main()
{
    int x[] = {1,2,3,4,5};
    cout << x[3] << endl;

    return 0;
}

This code declares an integer array x and initializes it with values using an array initialization list. It then prints the value of the fourth element of the array, which is 4, to the console. This code demonstrates how to create an array and access its elements in C++.

How does the compiler store the values on an array?

The compiler stores the values of an array in contiguous memory locations. This means that the values of the array elements are stored next to each other in memory. The compiler allocates a block of memory that is large enough to store all of the array elements, and then it stores the values of the elements in this block of memory.

The compiler keeps track of the address of the first element in the array, and then it uses this address to access the other elements in the array. For example, to access the element at index i, the compiler adds i to the address of the first element.

The specific way that the compiler stores the values of an array depends on the data type of the array elements. For example, if the array elements are integers, the compiler will store each integer in a single memory location. However, if the array elements are structs, the compiler will store each struct in a contiguous block of memory locations.

This code shows you how to read and store user input in an array in C++, it allows the user to input an integer value for the first element of an array, arr[0], and then it displays the value that was entered.

#include <iostream>

using namespace std;

int main()
{
    int arr[50];
    cout << "Enter arr[0]: " << endl;
    cin >> arr[0];
    cout << arr[0] << endl;

    return 0;
}

 

To traverse the elements of an array, you can use loops like for or while. Looping through an array allows you to perform operations on each element systematically.

Example:

#include <iostream>

using namespace std;

int main()
{
    int arr[10];
    for (size_t i = 0; i < 10; i++)
    {
        cout << "Enter arr[" << i << "]: ";
        cin >> arr[i];
    }
    for (size_t i = 0; i < 10; i++)
    {
        cout << "arr[" << i << "] = " << arr[i] << endl;
    }

    return 0;
}

This code allows the user to input 10 integer values, one for each element of the array, and then it displays the values along with their indices. It demonstrates how to read and store multiple user inputs in an array and subsequently print the contents of the array in C++.

Share this

One Dimensional Array (part 1)

Or copy link

CONTENTS
English