replace element in array
Tue Apr 09 2024 02:16:44 GMT+0000 (Coordinated Universal Time)
Saved by
@wayneinvein
//replace any element of an array with the provided element(prevoius element will be deleted)
#include <stdio.h>
int main() {
int size, element, pos;
// Input the size of the array
printf("Enter size of array: ");
scanf("%d", &size);
int arr[size];
// Input array elements
printf("Enter elements of array:\n");
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the number to be inserted
printf("Enter the number to insert: ");
scanf("%d", &element);
// get the position from user and authenticate it
printf("enter the position to insert (0 to %d): ", size - 1);
scanf("%d",&pos);
if(pos < 0 || pos >= size){
printf("\nInvalid Position");
return 1;
}
arr[pos] = element;
// Print the updated array
printf("Array after insertion:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
content_copyCOPY
Comments