fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void sort(int *values, int n);
  6. void swap(int arr[], int i);
  7. void populate(int arr[]);
  8. const int arraysize = 10;
  9.  
  10.  
  11. int main(){
  12.  
  13. srand((long int) time(NULL));
  14.  
  15. int ela[10] = {0};
  16. int i;
  17.  
  18. populate(ela); // populates with random values
  19.  
  20. printf("before func\n");
  21. for( i = 0; i < arraysize; i++)
  22. {
  23. printf("%i\n", ela[i]);
  24. }
  25.  
  26.  
  27. sort(ela, arraysize); // this is the function that is not working
  28.  
  29. printf("\n\nafter func\n");
  30.  
  31. for( i = 0; i < arraysize; i++)
  32. {
  33. printf("%i\n", ela[i]);
  34. }
  35.  
  36. return 0;
  37. }
  38.  
  39.  
  40.  
  41.  
  42.  
  43. void sort(int *values, int n)
  44. {
  45. int count = 1,i;
  46. while(count != 0)
  47. {
  48. count = 0;
  49. for ( i = 0; i < n-1; i++) {
  50.  
  51. if(values[i] > values[(i + 1)] )
  52. {
  53. swap(values, i);
  54. count++;
  55. }
  56. //if (count == 0) break;
  57.  
  58. }
  59.  
  60. }
  61.  
  62. }
  63.  
  64.  
  65. void swap(int arr[], int i)
  66. {
  67. int save = arr[i];
  68. arr[i] = arr[i+1];
  69. arr[i + 1] = save;
  70. return;
  71. }
  72.  
  73. void populate(int arr[])
  74. { int i;
  75. for( i = 0; i < arraysize; i++)
  76. {
  77. arr[i] = (rand() % 15);
  78. }
  79. }
  80.  
Success #stdin #stdout 0s 2008KB
stdin
Standard input is empty
stdout
before func
6
5
4
11
6
11
12
3
3
0


after func
0
3
3
4
5
6
6
11
11
12