Home > Back-end >  O great god carry!!!!!!
O great god carry!!!!!!

Time:03-14

C language, design a dynamic array, an array of input by the user the number of array elements, and a random assignment each element two integers, finally from small to large output the array,

CodePudding user response:

 # include & lt; Stdio. H> 
#include


Void bubble_sort (int * arr, int len);
Int main ()
{

Int n, * arr, I;

The scanf (" % d ", & amp; N);

Arr=(int *) malloc (sizeof (int) * (n));
if (! Arr)
return -1;

For (I=0; i The scanf (" % d ", & amp; Arr [I]);

Bubble_sort (arr, n);

For (I=0; i Printf (" % d ", arr [I]);
Putchar (10);

/*
For (I=1; i Printf (" % d ", arr [I]);
Putchar (10);
For (I=2; i Printf (" % d ", arr [I]);
Putchar (10);
*/

return 0;
}

Void bubble_sort (int * arr, int len)
{
int i, j;
Int TMP.

For (I=0; i For (j=0; J & lt; Len - 1; J++) {
If (arr [j] & gt; Arr [m + 1]) {
TMP=arr [j];
Arr=arr [j] [j + 1);
Arr=[m + 1] TMP;
}
}
}
}

For your reference ~

CodePudding user response:

Plan a, the use of variable-length array, need support for C99 compiler
 
# include "stdio.h"
# include "stdlib. H"
# include "time. H"
Int main (void)
{
Int num=0, I=0;
Printf (" input array length: ");
The scanf (" % d ", & amp; Num);
Int arr (num);
Srand (time (0));
For (I=0; i{
Arr [I]=rand () % 100;
Printf (" % d ", arr [I]);
}
return 0;
}


Scheme 2, application memory, C90 can also
 
# include "stdio.h"
# include "stdlib. H"
# include "time. H"
Int main (void)
{
Int num=0, I=0;
Printf (" input array length: ");
The scanf (" % d ", & amp; Num);
Int * arr=calloc (num, sizeof (int));.
Srand (time (0));
For (I=0; i{
Arr [I]=rand () % 100;
Printf (" % d ", arr [I]);
}
return 0;
}
  • Related