Advertisement
luizaspan

Rand IV (ordenando)

Jun 2nd, 2015
486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> // rand() ou random()
  3. #include <time.h>
  4.  
  5. #define n 100
  6.  
  7. // ordenando o vetor com o algoritmo bubblesort
  8.  
  9. int main(void)
  10. {
  11.   int i,a[n],j,temp;
  12.  
  13.   // gerando nova sequencia de numeros aleatórios:
  14.   srand(time(0));
  15.      
  16.   for (i=0;i<n;i++)
  17.     {
  18.       a[i]=rand()%n +1;
  19.       a[i]=(double) rand()/RAND_MAX*n;
  20.  
  21.       // printf("%d \n",a[i]);      
  22.     }
  23.  
  24.   for (i=0;i<n-1;i++) {
  25.     for(j=n-1;j>i;j--) {
  26.       if (a[j-1] > a[j])
  27.     {
  28.       temp=a[j-1];
  29.       a[j-1]=a[j];
  30.       a[j]=temp;
  31.     }
  32.     }
  33.   }
  34.  
  35.   for (i=0;i<n;i++)
  36.     printf("%d \n",a[i]);
  37.      
  38.   // RAND_MAX definido no <stdlib.h>
  39.   // número aleatório entre 1 e RAND_MAX
  40.      
  41.   return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement