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