Update an element in an Array: Program and Algorithm

Update an element in an Array

In this article, you’ll learn how to update an element of a specific position of an array in C/C++, along with that you’ll also see the algorithm to update an element in an array.

So, Let’s get started!

Prerequisite: You should have the knowledge of Traverse Operation in Array.

What is update operation in array?

The process of updating (modifying/changing) an existing element of an array at a specified index with another element is called updating an array or update operation in array.

How do you update an element in an array in C/C++?

Let’s see how you can update an element of a specific position of an array step-by-step:

Variables we are using here:

  1. a : Name of the array.
  2. size : Size of the array (i.e., total number of elements in the array)
  3. i : Loop counter or counter variable for the for loop.
  4. pos : The position where you wish to update the element.
  5. x : The updated element.

The following algorithm updates a data element at a specific position pos (position specified by the programmer) of a linear array ‘a‘.

Algorithm to Update an element in an Array:

  • Step 01: Start
  • Step 01: Set a[pos-1] = x
  • Step 01: Stop

Visual Representation:

Implementation in C:

// writing a program in C to update an element in an array

void main()
{
     int i, size, x, pos;
     int a[]={1, 3, 5, 7, 9};
     size=sizeof(a)/sizeof(a[0]);
     printf("The array elements before update operation are:");
     for(i=0;i<size;i++)
        printf("\na[%d]= %d", i, a[i]);
     printf("\nThe position where you wish to update the element: ");
     scanf("%d", &pos);
     printf("The new element: ");
     scanf("%d", &x);
     a[pos-1]=x;
     printf("The array elements after update operation:");
     for(i=0;i<size;i++)
        printf("\na[%d]= %d", i, a[i]);
}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before update operation are:
a[0]= 1
a[1]= 3
a[2]= 5
a[3]= 7
a[4]= 9
The position where you wish to update the element: 2
The new element: 84
The array elements after update operation:
a[0]= 1
a[1]= 84
a[2]= 5
a[3]= 7
a[4]= 9


Implementation in C++:

// writing a program in C++ to update an element in an array

#include <iostream>
using namespace std;
int main()
{
     int i, size, x, pos;
     int a[]={1, 3, 5, 7, 9};
     size=sizeof(a)/sizeof(a[0]);
     cout <<  "The array elements before update operation are:" ;
     for(i=0;i<size;i++)
        cout << "\n" << "a[" << i << "]= " << a[i];
     cout << "\nThe position where you wish to update the element: ";
     cin >> pos;
     cout << "The new element: ";
     cin >> x;
     a[pos-1]=x;
     cout << "The array elements after update operation are:";
     for(i=0;i<size;i++)
        cout << "\n" << "a[" << i << "]= " << a[i];
}

If you compile and run the above program, it will produce the following result:

Output:

The array elements before update operation are:
a[0]= 1
a[1]= 3
a[2]= 5
a[3]= 7
a[4]= 9
The position where you wish to update the element: 3
The new element: -10
The array elements after update operation are:
a[0]= 1
a[1]= 3
a[2]= -10
a[3]= 7
a[4]= 9


Conclusion:

In this article, you learned how to update an element of a specific position of an array in C/C++, you also became familiar with the algorithm to update an element in an array.

So,

I really hope that you liked my article and got a lot of value from it.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.


Share this Post

Leave a Reply

Your email address will not be published.