Comparing arrays within a class.

I need to compare an array within a class by using overloaded operator <.
It is within the bubble sort part of it. I have put in the friend bool operator, but can´t figure out how to make it work.

The class defines the < comparison operator (the less-than operator) that returns true if the first fraction is less than the second fraction (that is, a/b < c/d), but false otherwise.

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
#include <iostream>
using namespace std;


class Fraction
{
private:
    int numerator;
    int denominator;

public:
    Fraction();

    void input_numerator();
    void input_denominator();

    int get_num();
    int get_den();

    friend bool operator < (Fraction& a,Fraction& b);
    friend ostream& operator << (ostream& out, Fraction& arr);


};

int main()
{
    const int n = 10;

    Fraction arr[n];

    // Input ten fractions into the array
    for (int i = 0; i < n; i++)
    {
        arr[i].input_numerator();
        arr[i].input_denominator();
    }

    // Bubble sort the fractions; do not try this at home
    for (int i = 0; i < n; i++)
    {
        for (int j = 1; j < n; j++)
        {
            if (arr[j] < arr[j - 1])
            {
                Fraction tmp = arr[j];
                arr[j] = arr[j - 1];
                arr[j - 1] = tmp;
            }
        }
    }

    // Output the fractions
    for (int i = 0; i < n; i++)
    {
        cout << arr[i];
    }

    return 0;
}

Fraction :: Fraction()
{
    numerator = 0;
    denominator = 0;
}

void Fraction :: input_numerator()
{
    cin >> numerator;
}

void Fraction :: input_denominator()
{
    cin >> denominator;
}
bool operator < (Fraction& a, Fraction& b)
{
    return a.numerator < b.numerator;
}
int Fraction :: get_num()
{
    return numerator;
}
int  Fraction :: get_den()
{
    return denominator;
}

ostream& operator << (ostream& out, Fraction& arr)
{
    out << arr.denominator << "/" << arr.numerator;
    return cout;
}
Last edited on
You know, it would help if you told us what the problem you're having actually is, rather than sitting back and letting us figure it out.

I'm guessing you're seeing errors with lines 17 and 71?
Last edited on
Sorry for that.
The problem is with the friend bool operator <, and line 71.
I am not sure how to pass the arrays into it for it to be compared in there.

As it is now in line 17 and 71, as you point out, it´s not working.
Last edited on
What is it you actually want that operator to compare? It looks like it's not supposed to be comparing two arrays, but two Fraction objects. That's how you're using it at line 38, correct?

That would be correct, it should be comparing fraction a/b < c/d
I am lost in that part, where do i pull that information from.
Last edited on
The function header should be something like this:
 
bool operator < (const Fraction& a, const Fraction& b)
and the body should make use of the numerators and denominators of a and b.
Last edited on
I put in an edited version of the program.
I changed the bool operator and am making an ostream operator as well.

But it isn´t working. The program runs but nothing is out putted.

Any suggestions as to why?
Who can say - the problem might be in the part of the code that was edited out. I suggest you post the actual version of the code which is giving you the current problem.
the code at the first post is the code giving me the problems.

I'm not sure why there would be no output at all. Try adding cout << "About to output\n" at line 53 to make sure it actually gets there.

But I'm sure that your operator< is wrong. It compares the numerators only but ignores the denominators.
1
2
3
4
5
ostream& operator << (ostream& out, Fraction& arr)
{
    out << arr.denominator << "/" << arr.numerator;
    return cout;
}

that should be return out;
Ok, the problem was with the input to begin with. Line 70 and 75. I changed it to return instead of cin.
But it seems that there is alot of work for me to do here to fix this.

Thanks for the input.
Topic archived. No new replies allowed.