read fraction like matrix (2-D array) use class

i have exercise of 2-D array i want read of two matrix function i have my own way!

this is the input
1/2 2/3 3/2 4/5
0/1 2/5 3/1 3/4
1/1 1/1 0/1 0/1
3/2 4/3 3/2 1/2
2/3 3/1 2/3 1/9
5/2 6/5 7/8 8/9


and here my own way to read it be careful the out put should be fraction how i can do it (num / denum)

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
#include <iostream>
#include <fstream>

using namespace std;

int c=0;
int r=0;

class fraction
{
private:
	float num[24];
	float denum[24];
	float one[3][4];
	float two[3][4];

public:
	void read()
	{
		ifstream indata;
		indata.open("matrix.txt");

		for(int n=0;n<24;n++)
		{
			indata >> num[n];
			indata.ignore(1,'/');
			indata >> denum[n];
		}

		for(c;c<3;c++)
		{
			for(r;r<1;r++)
			{
				for(int n=0;n<12;n++)
				{
				one[c][r]= num[n]/denum[n];

				cout << one[c][r] << endl; //this to test it if it is working
				}
			}
		}
		
	

                for(c;c<3;c++)
		{
			for(r;r<1;r++)
			{
				for(int n=12;n<24;n++)
				{
				two[c][r]= num[n]/denum[n];

				cout << two[c][r] << endl;//this to test it if it is working
				}
                        }
                }
                           indata.close();
        }

}; // end of class


int main()
{
	fraction sss;
	
	sss.read();
	
	return 0;
}



then i should add two matrix and also subtract them.
Last edited on
Wouldn't it be better if you made your Fraction class hold one fraction and then make a 4x4 static (or nxn dynamic) array of Fraction objects? Your Fraction class could look like this:

1
2
3
4
5
6
7
8
9
10
11
class Fraction
{
    int num; //numerator
    int den; //denominator

    public:
    Fraction (int n=0, int d=1) {num=n, den=d; pos=p;}

    Fraction add(const Fraction & f) const {/*...*/}
    Fraction sub(const Fraction & f) const {/*...*/}
}

HINT on implementing add function:
Suppose you have a/b + c/d. To find this do (a*d + b*c)/(b*d). Now, let's say that N=(a*d + b*c) and D=b*d. Then find GCD(N,D) and divide N and D with that number. Now, N/D is the fraction you want.
Last edited on
Topic archived. No new replies allowed.