Knowee
Questions
Features
Study Tools

Write C program to insert the values at the specific index in the array.

Question

Write C program to insert the values at the specific index in the array.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that inserts a value at a specific index in an array:

#include <stdio.h>

void insert(int arr[], int n, int index, int value) {
    // shift elements to the right of the index
    for (int i = n; i > index; i--) {
        arr[i] = arr[i - 1];
    }

    // insert the value at the given index
    arr[index] = value;
}

int main() {
    int arr[100] = {1, 2, 3, 4, 5};
    int n = 5;
    int index = 2;
    int value = 10;

    insert(arr, n, index, value);

    // print the array
    for (int i = 0; i <= n; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

This program defines a function insert that takes an array, the current size of the array, an index, and a value. It shifts all elements to the right of the index one position to the right, and then inserts the value at the given index. In the main function, we create an array, specify the index and the value, call the insert function, and then print the array to verify that the value has been inserted correctly.

This problem has been solved

Similar Questions

Write a program in C to insert New value in the array at a given positionNote: First read array size then elements and then position and its valueFor example:Test Input Result151 2 3 4 53 8New Array is 1 2 8 3 4 5

Create an array of integers, then create an array of pointers pointing to each element in the integer array. Print the values using both arrays. Write the code in c

How do you initialize a 2D array in C?

How do you declare an array of integers in C?*1 pointint arr[10];array int[10];[10]int array;int array 10;

Consider an array arr of size 5, initially filled with elements [2, 4, 6, 8, 10]. You want to insert the element 5 at position 2 in the array. What would be the array after the insertion?

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.