Knowee
Questions
Features
Study Tools

write c program to insert the values at the specific index in the array without using functions

Question

write c program to insert the values at the specific index in the array without using functions

🧐 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>

int main() {
    int array[100], position, i, n, value;

    printf("Enter number of elements in array\n");
    scanf("%d", &n);

    printf("Enter %d elements\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &array[i]);
    }

    printf("Enter the location where you wish to insert an element\n");
    scanf("%d", &position);

    printf("Enter the value to insert\n");
    scanf("%d", &value);

    for (i = n - 1; i >= position - 1; i--) {
        array[i+1] = array[i];
    }

    array[position-1] = value;

    printf("Resultant array is\n");
    for (i = 0; i <= n; i++) {
        printf("%d\n", array[i]);
    }

    return 0;
}

This program first asks for the number of elements in the array, then the elements themselves. It then asks for the position at which to insert a new element and the value of the new element. It shifts all elements from that position to the right, then inserts the new element. Finally, it prints the new array.

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

How do you initialize a 2D array in C?

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

C program to sort an array in ascending order

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

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.