Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Define a one-dimensional integer array, input 5 numbers, sort from largest to smallest, and output the second largest value?
Define a one-dimensional integer array, input 5 numbers, sort from largest to smallest, and output the second largest value?
There are many common sorting methods, such as bubbling method, that is, n- 1 cycles are carried out, and each cycle is to detect who is bigger between the current element and the next element. If the next element is larger, the two elements are interchanged to achieve sorting.

The program and results are shown in the figure. Please note that scanf_s is replaced by scanf. I'm here because my c++ environment has no other settings.

void sort(int a[],int n)

{

Internal temperature;

for(int I = 0; I<n-1; i++)

for(int j = 0; j & ltn-I; j++)

if(a[j]& lt; a[j + 1])

{

temp = a[j];

a[j]= a[j+ 1];

a[j+ 1]= temp;

}

}

int main() {

int a[5];

for(int I = 0; I<5; i++)

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

sort(a,5);

Printf ("second maximum =%d", a [1]);

}