//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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter