Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Programming function: insert a number into an integer array whose element values are stored in ascending order, so that the inserted array elements are still in order.
Programming function: insert a number into an integer array whose element values are stored in ascending order, so that the inserted array elements are still in order.
Yi yi ...

# include & ltstdio.h & gt

# Define N 5

void main()

{ int a[N+ 1],x,I,k;

//Wait for the input array

for(I = 0; I & ltn;; i++)

scanf("%d ",& ampa[I]);

//Wait for the value to be inserted.

scanf("%d ",& ampx);

//Find the coordinate position of the numerical value to be inserted (starting from 0, the subscript is searched incrementally to meet the value of the array.

for(I = 0; a[I]& lt; X & I & ltn;; i++)

;

//From the k-th value to the n-th value, the assignment is made in the reverse order, which is actually to move back one bit.

for(k = N; K> me; k -)

a[k]= a[k- 1];

//Place the inserted value at the subscript position found before.

a[I]= x;

//Print the results

for(I = 0; I < = n; i++)

printf("%5d ",a[I]);

putchar(' \ n ');

}