I'm trying to print the reverse of an array using pointers eg. input is 4 8 15 7, I want to print 7 15 8 4.
When I run it, it reverses some numbers but not all? Sometimes it doesn't reverse any numbers. I am very confused.
I need it to work for an unknown number of elements in the array, which is why I used the while loops in 'readIntoArray' and 'printArray'. I'm really just struggling with 'reverseArray' and 'swap'.
void reverseArray(double numbers[], int size);
void printArray(double numbers[], int size);
int readIntoArray(double numbers[]);
void swap(double *a, double *b);
int main(void) {
double numbers[MAX_SIZE];
int size = readIntoArray(numbers);
reverseArray(numbers, size);
printArray(numbers, size);
return 0;
}
int readIntoArray(double numbers[]) {
double in;
int i = 0;
while (i < MAX_SIZE && scanf("%lf", &in) != 0) {
numbers[i] = in;
i++;
}
return i;
}
void reverseArray(double numbers[], int size){
int x = 0;
double *a = &numbers[x];
double *b = &numbers[size - x];
double temp;
while (x < size){
swap(a,b);
x++;
}
}
void printArray(double numbers[], int size){
int i = 0;
while (i<size){
printf("%lf ", numbers[i]);
i++;
}
}
void swap (double *a, double *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
How do I make it work? What am I missing?
int temp;anddouble *a, double *b-- something is very wrong here. (one of these things is not like the other, one of the things just doesn't belong... sung to Sesame Street Tune...) (hint:int4-bytes,double8-bytes so what is going intotemp = *a;?)