fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. void shift_positives(int values[], int num_values) {
  6. int left = 0;
  7. int temppoint=-1;
  8. for (int right = 0; right < num_values; right++)
  9. if (values[right] > 0)
  10. temppoint=(temppoint==-1)?right:temppoint;
  11. else if(temppoint!=-1)
  12. {
  13. memcpy(values+left,values+temppoint,sizeof(int)*(right-temppoint));
  14. left+=right-temppoint;
  15. temppoint=-1;
  16. }
  17. memset(values+left,0,sizeof(int)*(num_values-left+1));
  18. }
  19.  
  20. int main(void) {
  21. int data[] = {1,0,99,-1,-2,3,-5};
  22. shift_positives(data, 7);
  23. for (int i = 0; i < 7; i++) {
  24. printf("%d -> %d\n", i, data[i]);
  25. }
  26. return 0;
  27. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
0 -> 1
1 -> 99
2 -> 3
3 -> 0
4 -> 0
5 -> 0
6 -> 0