I wrote two programs to switch the order of an array. I wrote them in the same way, but only one of them works correctly. Could someone tell me what is wrong?
Oh I just figured out the problem
This is the one that works:
http://i49.tinypic.com/2ljqp2c.png
Code:
#include <stdio.h>
#define SIZE 10
void main(void)
{
int x[] = {-3,2,27,0,-10,31,-99,4,-47,33};
int c=0,temp=0,last_index=0;
last_index=SIZE-1;
for( c=0; c < last_index/2; c++){
temp= x[c];
x[c] = x[last_index-c];
x[last_index-c]=temp;
}
for(c= 0; c < SIZE; c++) {
printf("\n x[%d] = %d ",c,x[c]);}
}
This is the one that doesn't work:
http://i45.tinypic.com/8vq6c0.png
Code:
#include <stdio.h>
#define SIZE 10
void main(void)
{
int x[] = {-3,2,27,0,-10,31,-39,4,-47,33};
int i=0,last=0,temp=0;
last = SIZE-1;
for(i = 0; i < last/2 ;i++){
temp = x[i];
x[i] = x[last-i];
x[last-i] = temp;
}
for(i = 0; i < last/2 ;i++){
printf("x[%d] is %d\n",i,x[i]);
}
}