rev.c 690 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void reverseArray(int * theList, int count) {
  4. for (int i = 0; i < count >> 1; i++) {
  5. int lastIdx = count - i - 1;
  6. int lastValue = theList[lastIdx];
  7. theList[lastIdx] = theList[i];
  8. theList[i] = lastValue;
  9. }
  10. }
  11. int main(int argc, const char * argv[]) {
  12. if (argc != 3) {
  13. fprintf(stderr, "Usage: array-reverse <size> <iterations>\n");
  14. return 1;
  15. }
  16. int count = atoi(argv[1]);
  17. int iterations = atoi(argv[2]);
  18. int * array = malloc(sizeof(int) * count);
  19. for (int i = 0; i < count; i++) {
  20. array[i] = i;
  21. }
  22. for (int i = 0; i < iterations; i++) {
  23. reverseArray(array, count);
  24. }
  25. free(array);
  26. return 0;
  27. }