Update array element using One Dimensional Array in C


This program is a C program that updates an element of an array at a specific position. The program starts by declaring variables and an array called a. The user is prompted to enter the limit of the array (i.e. the number of elements) and the values of the array. The program then prints the values of the array as entered by the user.

Next, the user is prompted to enter the position of the array element that they want to update and the new value that they want to insert at that position. The program then uses a for loop to iterate through the array and checks if the current index of the loop is equal to the position entered by the user. If it is, the program replaces the value at that index with the new value entered by the user. Finally, the program prints the updated array with the updated value at the specified position.

Source Code

#include<stdio.h>
int main()
{
  int i,t,a[10],n,m,s,j=0,b[10];
  printf("\nEnter the Limit:");
  scanf("%d",&n);
  printf("\nEnter the Values:");
  for(i=0;i<n;i++)
  {
    scanf("%d",&a[i]);
  }
  printf("\nGiven values are:");
  for(i=0;i<n;i++)
  {
    printf("a[%d]=%d",i,a[i]);
  }
  printf("\nEnter the position to be update:");
  scanf("%d",&t);
  printf("\nEnter the value to be update:");
  scanf("%d",&s);
  for(i=0;i<n;i++)
  {
    if(i==t)
    {
       a[i]=s;
    }
  }
  printf("\nUpdated value is:");
  for(i=0;i<n;i++)
  {
    printf("\na[%d]=%d",i,a[i]);
  }
  return 0;
}
    
To download raw file Click Here

Output

Enter the limit:5
Enter the values:1
2
3
4
5
Given values are:
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Enter the position to be update:3
Enter the value to be update:5
Inserted value is:
a[0]=1
a[1]=2
a[2]=3
a[3]=5
a[4]=4
a[5]=5

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C