Need a little help with Fractions and Structs

Point me in the right direction? For an assignment I'm suppose to put an Array in my program that I have already made. The program is very simple, you put in a numerator and a denominator and it prints it as numerator/denominator.

Now I'm suppose to make it so it has an Array and will take in multiple integers and print them out as fractions.

Later I will have to change it so it adds these fractions, and leaves them in fraction form.

Anyone have any suggestions?

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


class fraction{
private:
        int numerator, denominator;
public:
        int n, d;
        fraction();
        fraction(int n, int d=1);
        void print();
        int set_numerator(int n);
        int set_denominator(int d);


};



fraction::fraction(){



        cout << "Put in the numerator and denominator" << endl;
        cin >> n;
        cin >> d;

        if (d==0){

                cout << "CAN NOT DIVIDE BY ZERO" << endl;
                denominator == 1;
                }
        else
                denominator = d;
        }

int fraction::set_numerator(int n){

                numerator = n;
}

int fraction::set_denominator(int d){

                denominator = d;
}


void fraction::print(){
       if(d >= 2){ cout << n << "/" << d << endl;
        } else { cout << n << endl;
        }
}

int main(){

        fraction fraction;
                fraction.print();

}
Alright..... I'm just sitting here stuck... all out of ideas and no one to even point me in a new direction...
1
Last edited on
fraction fraction; All right... doesn't that give you at least a warning?

So far so good. Now you need to use an array?

1
2
3
4
5
6
7
8
9
int main()
{
    fraction arrayOfFractions[10]; // ten fractions

    // counting starts from 0, so the first fraction is
    arrayOfFractions[0].print();
    // and the last is
    arrayOfFractions[9].print(); // that's right, nine not ten
}

I thought this forum was for help?


I thought people who needed something from strangers and relied on nothing but the goodwill of those strangers to get it were smart enough to ask politely and clearly, and not act like a child when the universe doesn't revolve around them.
Last edited on
Ok, that helped some, thank you!

Is there a way that the user can put in how many fractions they want to put in and then the array is set to that?
Last edited on
Not in C++. (Good old C has variable-length arrays, which are what you want.)

In C++ you could use dynamic memory allocation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    fraction *pointerToFractions;
    // we use a pointer, which is a variable holding a memory address

    int howMany; // how many fractions

    std::cout << "How many? : ";
    std::cin >> howMany;

    pointerToFractions = new fraction[howMany];

    // now use pointerToFractions as if it's an array, as in previous example

    delete[] pointerToFractions;
    // give back the memory we took after we're done with it.
    // otherwise we'll have a "memory leak"
    // languages that can de-allocate memory themselves are said to be
    // "garbage collected"
}


I would say this is the old school approach.
The new school way to do this is by using Containers, provided by the C++ library. But they'll take some getting used to, if you're new to templates and classes.

http://cplusplus.com/reference/stl/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <vector> // std::vector container

int main()
{
    int howMany;

    std::cout << "How many? : ";
    std::cin >> howMany;
    std::vector<fraction> vectorOfFractions(howMany);

    // now you can use vectorOfFractions as an array
    vectorOfFractions[0].print();
}


By the way, adding a using namespace std; makes it unnecessary to write std:: over and over, but in the end it's a style thing.
Topic archived. No new replies allowed.