-1

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;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Ad JR
  • 1
  • 1
  • 3
    What is the return value of scanf("%d%d%d", &a,&b,&c);? – kungjohan Oct 21 '20 at 13:59
  • 1
    I wouldn't use global variables. Your problem is probably that you're shadowing variables. Global `a1` and `a1` in `maxv` for example. – Fiddling Bits Oct 21 '20 at 13:59
  • 2
    Please [edit] your question to fix the indentation. It's very hard to read and follow with the inconsistent indentation currently shown. – Some programmer dude Oct 21 '20 at 14:01
  • the operator input 3 int a b and c and the program order them descending i tried to put a1 and b1 in the main but it displays me 3 time the c – Ad JR Oct 21 '20 at 14:02
  • 1
    "sorry for the identation i prefer checking all of that before to fix this" - just that you're asking _us_ to check it and the indentation makes it way harder for us. Posting properly formatted code is good etiquette. That also applies to all the unnecessary whitespace. – domsson Oct 21 '20 at 14:06
  • sorry i tried to fix but i'm a beginner don't know if the indentation is correct now – Ad JR Oct 21 '20 at 14:08
  • Your IDE / editor might be able to auto-indent for you. If not, you can find tools online that do it for you. But apart from that, if you really don't understand indentation then you don't understand scope, which is a very fundamental concept. I recommend looking into it. And: more than one empty line can and should be reduced to one empty line. – domsson Oct 21 '20 at 14:09
  • Also try to use meaningful variable names, this will help you read the code in two weeks. – Zaiborg Oct 21 '20 at 14:12
  • i did my best to make that more understandable, I hope it will. – Ad JR Oct 21 '20 at 14:17

1 Answers1

1

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 what a1 and b1 are at the time the maxvalue function is called, you'll see they will contain 0, because in C, a global int will 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. Because a1 and b1 are not what you expect, mv will not be what you expect, which is likely to foil the logic designed in the remainder of order. Always make sure a local variable is initialized or given a value before you ever use it.

If maxvalue is revised to select the MAX of the first two numbers entered: The Logic that chooses which should be i may fail. Because mv will always be one of a or b, and because one of a - mv ==0 or b - mv would always be true, c would never be assigned to i, and would not be printed first, even if it would be the highest.

In summary,

  • Use Local variables,
  • Initialize any variable before it is used.
  • Re-evaluate the logic of order once you are sure that maxvalue will always return the result you expect for any given input.
NateB
  • 509
  • 1
  • 3
  • 9
  • I kept this answer to the question at hand. Also I do not know if you have started learning about arrays and pointers yet, which would be useful in the task you are solving, not sure if an answer involving pointers, etc would be within the scope of your assignment. – NateB Oct 21 '20 at 18:26