Hey,
This is one of the tasks I have been given in a practical:
"Create a file called order.c (and its associated header file).Inside, create a static function called ascending2() that takes two int pointers andreturns void. The function should place the smaller of the two int values in the firstmemory location and the larger in the second.In other words, ascending2() should swap its parameters if the first is larger than thesecond, but do nothing otherwise."
My code compiles although the 2 values (inputed by the user) still swap places in memory even when the first value inputed is already smaller than the second.
Here's the code:
Code:
#include <stdlib.h>
#include <stdio.h>
void ascending2(int *i, int *j);
int main(void)
{
int valueOne;
int valueTwo;
printf("enter 2 numbers\n");
scanf("%d %d", &valueOne, &valueTwo);
ascending2(&valueOne, &valueTwo);
printf("%d, %d\n", valueOne, valueTwo);
return 0;
}
void ascending2(int *i, int *j)
{
int coolInt;
if (i > j) {
coolInt = *i;
*i = *j;
*j = coolInt;
}
return;
}
Any ideas? Thanks!