一个随手写的作业
#include< iostream>
#include< cstdlib>
#include< ctime>
using namespace std;
void getRandom ( int a[ ] , int size)
{
srand ( unsigned ( time ( NULL) ) ) ;
for ( int i= 0 ; i< size; i++ )
{
a[ i] = rand ( ) % ( 100 + 1 ) ;
}
cout<< "The randomly generated array is:" << endl;
for ( int j= 0 ; j< size; j++ )
{
cout<< a[ j] << " " ;
}
}
void sortArray ( int a[ ] , int size)
{
int temp;
for ( int i= 0 ; i< size- 1 ; i++ )
{
for ( int j= 0 ; j< size- i- 1 ; j++ )
{
if ( a[ j] > a[ j+ 1 ] )
{
temp= a[ j] ;
a[ j] = a[ j+ 1 ] ;
a[ j+ 1 ] = temp;
}
}
}
cout<< "\nThe sorted array is:" << endl;
for ( int n= 0 ; n< size; n++ )
{
cout<< a[ n] << " " ;
}
}
int main ( )
{
int a[ 20 ] ;
getRandom ( a, 20 ) ;
sortArray ( a, 20 ) ;
system ( "pause" ) ;
return 0 ;
}
! [ 这是运行结果] ( https: / / img- blog. csdnimg. cn/ 20191206204510444. PNG? x- oss- process= image/ watermark, type_ZmFuZ3poZW5naGVpdGk, shadow_10, text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTc1NDUw, size_16, color_FFFFFF, t_70)