# Update an element in an Array: Program and Algorithm

• Array, Data Structures 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 02: Set `a[pos-1] = x`
• Step 03: Stop

## 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);
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= 1
a= 3
a= 5
a= 7
a= 9
The position where you wish to update the element: 2
The new element: 84
The array elements after update operation:
a= 1
a= 84
a= 5
a= 7
a= 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);
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];
return 0;

}

```

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

Output:

```The array elements before update operation are:
a= 1
a= 3
a= 5
a= 7
a= 9
The position where you wish to update the element: 3
The new element: -10
The array elements after update operation are:
a= 1
a= 3
a= -10
a= 7
a= 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.