I agree with @Eugene Sh.'s and @FredK's suggestions. The line array[5]
in the line printf("%d", array[5]);
is out of bound since array
only have indexes from 0 to 4. Since I assume you want to print out the last element in the array, you should change it to printf("%d", array[4]);
. Another thing is that your assignment expression array[i] = a;
is wrong. I assume the expression is part of the swapping process from element in index i
with element in index rev
. If that was the case then you should change it to a = array[i];
instead. I update you code according to my suggestion and it outputs the correct result. I added the for loop to verify that the array
values are reversed for testing purpose. You can delete it after you're done testing.
#include <math.h>
int main() {
double size = 5;
int array[5] = {1, 2, 3, 4, 5};
int half_size = ceil(size / 2);
for(int i = 0; i < half_size; i++){
int a;
int rev = size - (i + 1);
a = array[i];
array[i] = array[rev];
array[rev] = a;
}
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
printf("%d", array[4]);
}