Functions homework help

This is what i have so far. We have to use functions to get input, find min, and print min. We have to overload functions also. This compiles but once I input some integers it doesnt do anything with them. My question is where do i go from here? it needs to find minimun with the numbers i enter.


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cctype>

using namespace std;

int find_min(int num1, int num2);
float find_min(float dnum1, float dnum2);
int find_min(int num1, int num2, int num3);
void get_input(int &num1, int &num2);
void get_input(float &dnum1, float &dnum2);
void get_input(int &num1, int &num2, int &num3);
void print_min(int num1);
void print_min(float dnum1);

int main() {
    int num1, num2, num3;
    float dnum1, dnum2;

    // Two integer version
    cout << "Enter two integers: ";
    get_input(num1, num2);
    print_min(find_min(num1, num2));
    cout << "\n";

    // Two float version
    cout << "Enter two numbers: ";
    get_input(dnum1, dnum2);
    print_min(find_min(dnum1, dnum2));
    cout << "\n";

    // Three integer version
    cout << "Enter three integers: ";
    get_input(num1, num2, num3);
    print_min(find_min(num1, num2, num3));
    cout << "\n";

    return 0;
}

int find_min(int num1, int num2) {
    if (num1 == num2) {
        return num1;
    } else if (num1 < num2) {
        return num1;
    } else {
        return num2;
    }
}

float find_min(float dnum1, float dnum2) {
    float dmin;
    if (dnum1 == dnum2) {
        dmin = dnum1;
    } else if (dnum1 < dnum2) {
        dmin = dnum1;
    } else {
        dmin = dnum1;
    }
    return dmin;
}

int find_min(int num1, int num2, int num3) {
    int min;
    if (num1 == num2 && num1 == num3) {
        min = num1;
    } else if (num1 < num2 && num1 < num3) {
        min = num1;
    } else if (num2 < num1 && num2 < num3) {
        min = num2;
    } else if (num3 < num2 && num3 < num1) {
        min = num3;
    }
    return min;
}

void get_input(int &num1, int &num2) {
    cin >> num1 >> num2;
    find_min(num1, num2);
}

void get_input(float &dnum1, float &dnum2) {
    cin >> dnum1 >> dnum2;
}

void get_input(int &num1, int &num2, int &num3) {
    cin >> num1 >> num2 >> num3;
}

void print_min(int num1) {
    cout << "The minimum is ";
}

void print_min(float dnum1) {
    cout << "The minimun is ";
}
Last edited on
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
int find_min(int num1, int num2) { return num2 < num1 ? num2 : num1 ; }
/*
    if (num1 == num2) {
        return num1;
    } else if (num1 < num2) {
        return num1;
    } else {
        return num2;
    }
}
*/

float find_min(float dnum1, float dnum2) { return dnum2 < dnum1 ? dnum2 : dnum1 ; }
/*
    float dmin;
    if (dnum1 == dnum2) {
        dmin = dnum1;
    } else if (dnum1 < dnum2) {
        dmin = dnum1;
    } else {
        dmin = dnum1;
    }
    return dmin;
}
*/

int find_min(int num1, int num2, int num3) { return find_min( find_min(num1,num2), num3 ) ; }
/*
    int min;
    if (num1 == num2 && num1 == num3) {
        min = num1;
    } else if (num1 < num2 && num1 < num3) {
        min = num1;
    } else if (num2 < num1 && num2 < num3) {
        min = num2;
    } else if (num3 < num2 && num3 < num1) {
        min = num3;
    }
    return min;
}
*/

void get_input(int &num1, int &num2) {
    cin >> num1 >> num2;
    //find_min(num1, num2); // ********
}

void get_input(float &dnum1, float &dnum2) {
    cin >> dnum1 >> dnum2;
}

void get_input(int &num1, int &num2, int &num3) {
    cin >> num1 >> num2 >> num3;
}

void print_min(int num1) {
    // cout << "The minimum is ";
    cout << "The minimum is " << num1 << '\n' ; // **********
}

void print_min(float dnum1) {
    // cout << "The minimum is ";
    cout << "The minimun is " << dnum1 << '\n' ; // **********
}
@JLBorges in the find_min functions, what is that called that you wrote in the {} on lines 1, 13, and 27. Is there another way to do this?
> what is that called that you wrote in the {} on lines 1, 13,

Conditional operator (aka ternary operator, arithmetic-if operator)
http://msdn.microsoft.com/en-us/library/e4213hs1.aspx

> Is there another way to do this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int find_min(int num1, int num2) {

    if( num1 <= num2 ) return num1 ;
    else return num2 ;
}

float find_min(float dnum1, float dnum2) {

    if( dnum1 <= dnum2 ) return dnum1 ;
    else return dnum2 ;
}

int find_min(int num1, int num2, int num3) {

    const int min_of_num1_num2 = find_min(num1,num2) ;
    return find_min( min_of_num1_num2, num3 ) ;
}
Awesome Thanks!
Topic archived. No new replies allowed.