This is how I fill the array, but unfortunately I can’t figure out how to insert a number into it without knocking down the order (so that after inserting the number into the array, the order of numbers would not get lost)

#include <stdio.h> int main(void) { //Creating array int a[N]; int i, j, temp; printf("Enter size of array\n"); scanf("%d", &N); printf("Enter number to create array\n"); for (i = 0; i < N; i++) scanf("%d", &a[i]); //Sorting the array ascending descending for (i = 1; i < N; i++) { temp = a[i]; for (j = i - 1; j >= 0; j--) if (temp > a[j]) { a[j + 1] = a[j]; a[j] = temp; } } //Output of sorted array for (i = 0; i < N; i++) printf("%d\n", a[i]); return 0; } 

    1 answer 1

    First you need to find the position in which we will insert the new value. Since the array is ordered, it is better to use the binary search algorithm.

    After the position for insertion is found, it is necessary to shift all elements, starting from the position found one element "to the right".

    Now everything is ready to write the new value to the position found.

    • Right , you can add man memmove . - 0andriy