HELP WITH A PROBLEM

Hi..
I'm a begginer programmer and i don't know how to solve this problem.
When i'm compile on the website where i learn c++ ( this site asked me in the program that ranges 3 numbers) it said " mij,m and M may be used uninitialized in this function".
Please help me...

m = lowest number
M =the biggest
mij = the "one in the middle"

Sorry for my bad english..im Romanian :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  
#include <iostream>

using namespace std;

int main()
{
    int a,b,c,M,m,mij;
    cin>>a>>b>>c;
    if(a==b && b==c){
        cout<<a<<" "<<b<<" "<<c;
    }
    else{ if(a==b){
                if(a>c){
                    cout<<c<<" "<<a<<" "<<a;
                }
                else{
                    cout<<a<<" "<<a<<" "<<c;
                }
           }
          else if(a==c){
                    if(a>b){
                        cout<<b<<" "<<a<<" "<<a;
                    }
                    else{
                        cout<<a<<" "<<a<<" "<<b;
                    }
               }
              else if(b==c){
                    if(b>a){
                        cout<<a<<" "<<b<<" "<<b;
                    }
                    else{
                        cout<<b<<" "<<b<<" "<<a;
                    }
                   }
                   else{
                    ///M=max(a,max(b,c));
                    if(a>b && a>c){
                        M=a;
                    }
                    if(b>a && b>c){
                        M=b;
                    }
                    if(c>a && c>b){
                        M=c;
                    }
                    ///m=min(a,min(b,c));
                    if(a<b && a<c){
                        m=a;
                    }
                    if(b<a && b<c){
                        m=b;
                    }
                    if(c<a && c<b){
                        m=c;
                    }
                    if(m<a && a<M){ ///caut mijlocul
                        mij=a;
                    }
                    if(m<b && b<M){
                        mij=b;
                    }
                    if(m>c && c<M){
                        mij=c;
                    }
                    cout<<m<<" "<<mij<<" "<<M;
                }
    }
    return 0;
}
Lets take M. You do use it on lines 58, 61, 64, and 67.
When you do reach line 58, have you set the value of M to be something?

You might set the value on lines 40, 43, and/or 46. None of those is executed, if a==b && b==c, but the compiler does not see like we do that the whole branch is skipped if that is true.

The compiler has both "errors" and "warnings".
It is good to pay attention to them all.

You can initialize variables with known values and modify your conditions. Both will remove the warning. The important thing is to check that your logic is sound.


You had //M=max(a,max(b,c)); . You could mimic that:
1
2
3
4
5
if ( b < c ) M=c;
else M=b;
// M is now max(b,c)

if ( M < a ) M = a;

Or, the common "find largest" logic:
1
2
3
int M = a;
if ( M < b ) M = b;
if ( M < c ) M = c;


You don't have to declare the M, etc already on line 8. It is enough to declate them within lines 38-67, where they are needed.
Your code is really hard to parse, super bug prone, and impossible to maintain.

mij,m and M may be used uninitialized in this function".

Indeed. Uninitialized in this case means that the variable is not assigned a value before you attempt to use the value of the variable.

For example,
1
2
int foo;
cout << foo << endl;

foo is unintialized -- you will print out garbage! Not good.

Your code:
The error message is saying that by the time you get to line 67, it's possible that either your one of your variables (m, mij, or M) are uninitialized, meaning they have garbage in them.

This is evident by the following example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  
#include <iostream>

using namespace std;

int main()
{
    int a,b,c,M,m,mij;
    cin>>a>>b>>c;
    if(a==b && b==c){
        cout<<a<<" "<<b<<" "<<c;
    }
    else{ if(a==b){
                if(a>c){
                    cout<<c<<" "<<a<<" "<<a;
                }
                else{
                    cout<<a<<" "<<a<<" "<<c;
                }
           }
          else if(a==c){
                    if(a>b){
                        cout<<b<<" "<<a<<" "<<a;
                    }
                    else{
                        cout<<a<<" "<<a<<" "<<b;
                    }
               }
              else if(b==c){
                    if(b>a){
                        cout<<a<<" "<<b<<" "<<b;
                    }
                    else{
                        cout<<b<<" "<<b<<" "<<a;
                    }
                   }
                   else{
                    ///M=max(a,max(b,c));
                    if(a>b && a>c){
                        std::cout << "M=a" << std::endl;
                        M=a;
                    }
                    if(b>a && b>c){
                        std::cout << "M=b" << std::endl;
                        M=b;
                    }
                    if(c>a && c>b){
                        std::cout << "M=c" << std::endl;
                        M=c;
                    }
                    ///m=min(a,min(b,c));
                    if(a<b && a<c){
                        std::cout << "m=a" << std::endl;
                        m=a;
                    }
                    if(b<a && b<c){
                        std::cout << "m=b" << std::endl;
                        m=b;
                    }
                    if(c<a && c<b){
                        std::cout << "m=c" << std::endl;
                        m=c;
                    }
                    if(m<a && a<M){ ///caut mijlocul
                        std::cout << "mij=a" << std::endl;
                        mij=a;
                    }
                    if(m<b && b<M){
                        std::cout << "mij=b" << std::endl;
                        mij=b;
                    }
                    if(m>c && c<M){
                        std::cout << "mij=c" << std::endl;
                        mij=c;
                    }
                    cout<<m<<" "<<mij<<" "<<M;
                }
    }
    return 0;
}


Input:
3 1 2

Output:

M=a
m=b
1 0 3 


mij is never assigned here. It looks like m and M will always be assigned, but because mij won't always be assigned, your program has undefined behavior so the compiler probably didn't try every possible combination, and just spit out a warning instead.

You do not correctly account for the case where a > b, a > c, but b < c.

Last edited on
If you want a more elegant solution, first try figuring out the logic for all 6 test cases (there are 6 permutations of 3 elements).
1
2
3
4
5
6
1 2 3 -- a < b, a < c, b < c
1 3 2 -- a < b, a < c, b > c
2 1 3 -- a > b, a < c, b < c
2 3 1 -- a < b, a > c, b > c
3 1 2 -- a > b, a > c, b < c
3 2 1 -- a > b, a > c, b > c

There is a pattern.

If you don't care about elegance, just add another if statement to account for
a > b, a > c, b < c.
To spoil the solution, check out https://stackoverflow.com/a/19045659/8690169
Last edited on
Topic archived. No new replies allowed.