from this,
Code:
(i = 1; i < sizeof(array) / sizeof(int) - 1; i++)
to this,
Code:
// either here
size_t i = 0;
( ; i < sizeof(array) / sizeof(array[0]); i++)
//or
(size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
should be a good thing too.
run this and take a good look at all of your output to see where your 1 went to, then try to figure out what to do with lastOne to get your 1 back into your array before printing it out.
Code:
#include <stdio.h>
int swap(int *a, int *b) {
printf("one: a %d, b %d\n", *a,*b);
int tmp = *a;
printf("!:: a %d, b %d tmp %d\n", *a,*b, tmp);
*a = *b;
*b = tmp;
printf("a %d, b %d tmp %d\n", *a,*b, tmp);
return tmp;
}
int main() {
int array[] = {1, 2, 3, 4};
int lastOne = 0;
for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
printf("i=%ld %d ",i, array[i]);
printf("\n");
size_t i = 0;
for (; i < sizeof(array) / sizeof(array[0]); i++) {
lastOne = swap( &array[i], &array[i + 1]);
printf("i==%ld lastOne %d\n", i,lastOne);
}
printf("outside loop\nlastOne %d i= %ld\n",lastOne, i);
for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
for your 2dn one you want something like this?
look at your switch and what you're really doing.