Skip to main content

Arrays

Iterating & Updating Arrays

Iterating Arrays

In the last post, we learned what arrays are and how we can create, initialize and access an array. For accessing array elements we printed every element of array one by one manually.

Something like this!
#include <iostream>
using namespace std;
 
int main() {
 
    // create an array named arr of size 5
    int arr[5] = {1, 2, 3, 4, 5};
 
    // access first element
    cout << arr[0] << endl;
 
    // access second element
    cout << arr[1] << endl;
 
    // access third element
    cout << arr[2] << endl;
 
    // access fourth element
    cout << arr[3] << endl;
 
    // access fifth element
    cout << arr[4];
  
    return 0;
    }

Now suppose the size of array is 100 and I need to print all the elements of array. No way, I am going to access and print all elements from the first to the hundredth position. What's the solution then? LOOPS!

Observe that to access any element at position i, we print the expression arr[i] and this i ranges from 0 to n -1 (indices of array arr) where n is the size of array. Hence we can run a loop on i from 0 to n-1 and print the array element at the corresponding index i.

Example

Let's solve the same problem using a for loop.

#include <iostream>
using namespace std;
 
int main() {
 
    // create an array
    int numbers[5] = {1, 2, 3, 4, 5};
 
    // access array using loop
    for (int index = 0; index < 5; ++index) {
        cout << numbers[index] << endl;
    }
 
    return 0;
}

Output

1
2
3
4
5

It's clearly visible that accessing elements via loops is simpler and more efficient for any array size.


Quiz 1

What is the output of the following code?

#include <iostream>
using namespace std;

int main() {

    int arr[5] = {5, 78, 24, 1, 68};

    cout << arr[4] << " " << arr[1] << " " << arr[0];

    return 0;
}

Quiz 2

What is the index number for element 97 in the following array?

int array[4] = {102, 63, 97, 49};

Updating Array elements

Suppose I create an array as

int arr[5] = {11, 9, 2, 7, 5};

But now I want to change the element at index 3 (7) to some other value say 16. In other words, I need to update my array arr to {11, 9, 2, 16, 5} . Should I create a new array? No way!

So let's see how can we update the same array arr according to our requirements.

#include <iostream>
using namespace std;
 
int main() {
 
    // create an array
    int arr[5] = {11, 9, 2, 7, 5};
 
    // Change the value of 4th element (3rd index)
    arr[3] = 16;
 
    // access array using loop
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << endl;
    }
 
    return 0;
}

Output

11
9
2
16
5

Here, arr[3] = 16; changes the value of the fourth element to 16.


Quiz 3

What is the output of the following code?

#include <iostream>
using namespace std;

int main() {

    int arr[4] = {7, 5, 2, 11};
    cout << arr[3] << ", ";

    arr[3] = 8;
    cout << arr[3];

    return 0;
}

Quiz 4

We are given an array arr as:

int arr[4] = {7, 9, 1, 3};

How can we change the first element of this array to 200?


Problem

Now let's solve a problem and apply the concepts that we covered till now!

Problem Statement

  • Create a double type array named arr with values: 2.7, 4.3, 12.8, 5.4
  • Print the array values in separate lines
  • Update the 2nd and 4th element to 3.7 and 6.6 respectively
  • Print the array values in separate lines

Try yourself

Solution

Code
#include <iostream>
using namespace std;

int main() {
    // Create a double-type array named arr with initial values
    double arr[] = {2.7, 4.3, 12.8, 5.4};

    // Print the original array values in separate lines
    cout << "Original array values:" << endl;
    for (int i = 0; i < 4; i++) {
        cout << arr[i] << endl;
    }

    // Update the 2nd and 4th elements of the array
    arr[1] = 3.7;
    arr[3] = 6.6;

    // Print the modified array values in separate lines
    cout << "\nArray values after updates:" << endl;
    for (int i = 0; i < 4; i++) {
        cout << arr[i] << endl;
    }

    return 0;
}

I hope you learned and enjoyed all the concepts of arrays done till now. We'll meet with a few more in the upcoming articles.

Happy learning😊