Advertisement
Guest User

4chan number sorting 2

a guest
Jun 24th, 2019
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // list      - list of integers
  4. // size      - size of the list
  5. // target   - number to put at the end
  6. void move_to_back(int *list, size_t size, int target) {
  7.     unsigned int index = 0;
  8.     int solved_list[size]; 
  9.  
  10.     // Create a new list without the target number
  11.     for (unsigned int i = 0; i < size; i++) {
  12.         if (list[i] != target) {
  13.             solved_list[index++] = list[i];
  14.         }
  15.     }
  16.    
  17.     // Append the appropriate number of targets to the end
  18.     for (unsigned int i = index; i < size; i++) {
  19.         solved_list[i] = target;
  20.     }
  21.    
  22.     // Copy solved_list to list (Do I need a free() here?)
  23.     memcpy(list, solved_list, sizeof(solved_list));
  24. }
  25.  
  26.  
  27. int main() {
  28.     int list[9] = {1,9,7,4,9,3,2,6,9};
  29.     move_to_back(list, 9, 9);
  30.    
  31.     for (int i = 0; i < 9; i++)
  32.         printf("%d ", list[i]);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement