Use the rand library function to randomly generate 20 integers and output them, call the function to sort the 20 integers in descending order, and output the sorted data again in the main function.

The call of the rand library function requires the header file #include<stdlib.h>,
which was mentioned in the article Random Seed last time.
Use with srand.

Idea :
Randomly generate 20 numbers and output them, considering the loop structure.
Then sort the 20 integers in descending order, consider the loop structure and the array assignment, and then a simple bubble sort...

Code display:

#include<stdio.h>
#include<stdlib.h>
int main()
{
    
    
	
	int i,a[20],j,temp;
	srand(0);
	for(i=0;i<20;i++)
	{
    
    
		a[i]=rand();
	printf("%d ",a[i]);
	}
**printf("\n");//此处为了换行我用过puts('\0');但之后运行时只有随机数产生却不能排序输出,想想为什么。**
	
	for(i=0;i<19;i++)
		for(j=0;j<=18-i;j++)
		{
    
    
           if(a[j]<a[j+1])
		   {
    
    
            temp=a[j];
			a[j]=a[j+1];
			a[j+1]=temp;
		   }
		}

printf("将这些数字降序排序后得:\n");
for(i=0;i<20;i++)
printf("%d ",a[i]);

	printf("\n");
	return 0;
}

One point worth noting is marked in the code.

Guess you like

Origin blog.csdn.net/yooppa/article/details/114404364