hello coders, i hope you all are going well, one thing blows my mind today in c i can't explain it this is why title is akward, so the thing is i have an array called tab[n] i wanted to invert it elements from tab[0] to tab[n-1] and and the tab[midle] would remain the same so i used intermediate variable x, max, half, what i get in resutl is just unexpected because before the entering the loop for the value of max is equal to 14 once once in the for loop max is equal to 0 i don't understand why; sec time it is equal to 13 normal cause i have max--; then again max=1 i don't get why, then max=13 normal
#include <stdio.h>
#define DIM 15
int main(void){
int x ;
int i=0;
int max=14;
int tab[DIM]={1,5,3,0,4,0,5,8,3,0,5,3,0,7};
int half;
if(DIM%2==0){
half = DIM/2;
}
else{
half = (DIM-1)/2;
}
printf("max initial =%d\n", max);
printf("half initial =%d\n", half);
for (i=0; i<=half; i++) {
x=tab[i];
tab[i]=tab[max];
tab[max]=tab[i];
max--;
printf("max=%d\n", i);
printf("max=%d\n", max);
}
for (i=0; i<DIM; i++) {
printf("tab[%d]=%d\n", i, tab[i]);
}
return 0;
}