I'm coding in C on geany a program supposed to display 3 inputed int by descending order, the compilation is successfull but it displays me only the last number and two zero , my teacher told us to use function so... i don't know what's wrong in my code ps : sorry for the identation i prefer checking all of that before to fix this.
#include <stdio.h>
#include <stdlib.h>
int a1,b1;
int a,b,c;
int i,j,k;
int mv;
int maxvalue ( int a1, int b1){
if (a1 > b1){
return a1;
}
else {
return b1;
}
}
void order ( int a, int b ,int c){
int mv = maxvalue( a1, b1);
if (a - mv == 0){
i = a;
}
else if ( b - mv == 0){
i= b;
}
else {
i = c ;
}
printf("%d\n", i);
if (( a < i) && ( a-mv ==0)) {
j=a;
}
else if (( b< i)&&( b - mv ==0 )) {
j = b;
}
else{
j = c;
}
printf("%d\n",j);
if (( a < j) && ( a - mv == 0)){
k=a;
}
else if (( b < j) &&( b - mv ==0 )) {
k = b;
}
else{
k = c;
}
printf("%d", k);
}
int main(int argc, char **argv)
{
a = a1;
b = b1;
scanf("%d%d%d", &a,&b,&c);
order(a, b , c);
return 0;
}
One problem as pointed out in the question's comments is that local variables shadow global variables of the same name, and that you should study the concept of SCOPE. As suggested, make variables LOCAL.
Also. in
order,int mv = (a1, b1);: You declared, but never explicitly gave values to,a1&b1. If you examine whata1andb1are at the time themaxvaluefunction is called, you'll see they will contain 0, because in C, a globalintwill automatically be Zero when declared. (This is not the case for a locally declared variable, in which case the will be filled with meaningless bits until initialized. Becausea1andb1are not what you expect,mvwill not be what you expect, which is likely to foil the logic designed in the remainder oforder. Always make sure a local variable is initialized or given a value before you ever use it.If
maxvalueis revised to select the MAX of the first two numbers entered: The Logic that chooses which should beimay fail. Becausemvwill always be one ofa or b, and because one ofa - mv ==0orb - mvwould always be true,cwould never be assigned toi, and would not be printed first, even if it would be the highest.In summary,
orderonce you are sure thatmaxvaluewill always return the result you expect for any given input.