Advertisement
Kaze79

debugging_01_fixed

Dec 5th, 2015
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <float.h>
  5.  
  6. /* Insertion sort
  7.  * Learn debugging
  8.  * Work on implementation of shit
  9.  */
  10.  
  11. void input(int * array, int size, int *num_inuts);
  12. void findingsmaller(int *array, int num_inputs);
  13. void goleft(int *array, int a, int b);
  14.  
  15. int main(void)
  16. {
  17.     int array[200];
  18.     int size = sizeof(array)/sizeof(int);
  19.     int num_inputs = 0;
  20.     int i;
  21.  
  22.     input(array, size, &num_inputs);
  23.     findingsmaller(array, num_inputs);
  24.  
  25.     for (i=0; i <= num_inputs-1; i++) {
  26.         printf("%d ", array[i]);
  27.     }
  28.     printf("\n");
  29.  
  30.     return (EXIT_SUCCESS);
  31. }
  32.  
  33. void input(int * array, int size, int *num_inputs) {
  34. /* Reads input
  35.  * Writes into array
  36.  * Records the number of numbers in array
  37.  */
  38.     int i = 0;
  39.  
  40.     printf("Enter numbers:\n");
  41.     while( scanf("%d", &array[i]) != EOF ) {
  42.         i++;
  43.         (*num_inputs)++;
  44.         if (i == (size-1))
  45.             break;
  46.     }
  47. }
  48. void findingsmaller (int *array, int num_inputs){
  49. /* Finds a number that is smaller than the one on its left
  50.  * Enters "toleft" which places it somewhere on the left
  51.  * Keep doing it until I'm at the end of array
  52.  */
  53.     int a = 0;
  54.     int b = 1;
  55.  
  56.     for ( b=1; b <= num_inputs-1; b++ ) {
  57.         if ( array[a] > array[b] ) {
  58.             goleft(array, a, b);
  59.             a++;
  60.         } else {
  61.             a++;
  62.         }
  63.     }
  64. }
  65. void goleft(int *array, int a, int b) {
  66. /* Goes left in an array until it finds a number smaller, then keeps it there
  67.  */
  68.     int temp;
  69.     do {
  70.         temp = array[a]; //replace
  71.         array[a] = array[b];
  72.         array[b] = temp;
  73.         a--;
  74.         b--;
  75.     } while ( a >= 0 && array[a] > array[b] );
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement