Code:
#include <iostream>
#include <ctime>
using namespace std;
class Fraction
{
private:
int num; // Numerator
int den; // Denominator
public:
void setFraction (int); //sets fraction
int Reduce(); //will reduce the fraction to lowest terms
void print(); //will print the fraction in the form: numerator / denominator
int rand(); //will create random fractions
void multiply(); //will multiply the fractions
Fraction(); // constructor
Fraction(int,int); // 2 Argument Constructor
};
Fraction::Fraction()
{
num=0;
den=1;
}
Fraction::Fraction(int n,int d)
{
num=n;
den=(d==0)? 1 :d;
}
int rand ()
{
int Num[3];
int Den[3];
int answer; // user input
//int Numerator;
//int Denominator;
srand (time(0));
//Initiate checking for correct answer
while (answer != -1)
{
Num[0] = rand() % 9 + 1; // load the first random number into Num[0]
Num[1] = rand() % 9 + 1; // load the second into Num[1]
Num[2] = Num[0] * Num[1]; // load the answer into Num[2]
Den[0] = rand() % 9 + 1; // load the first random number into Den[0]
Den[1] = rand() % 9 + 1; // load the second into Den[1]
Den[2] = Den[0] * Den[1]; // load the answer into Den[2]
cout << "\n\n""What is " << Num[0] << "/" << Den[0]<< "*" << Num[1] << "/" << Den[1] <<"?" << endl;
cout << "Enter Numerator / Denominator" << endl;
cin >>answer ;
if ((answer == Num[0] * Num[1]) && (Den[0] * Den[1])); // check against the numbers array
{
cout << "\n""Correct!"<< endl; //Outcome for correct answer
}
//else //statement for while loop
// GIVE THE CORRECT ANSWER AN MOVE ON
//cout << "\nWrong! The correct answer was " << numbers[2] << endl; //Outcome for incorrect answer
}
return 0;
}
void multiply(int &num, int &den, int &num2, int &den2)
{
int calcnum;
int calcden;
calcnum = num * num2;
calcden = den * den2;
}
void Reduce(int &num, int &den, int &num2, int &den2)
{
int a, b, c, d, i, j = 0;
a = den;
b = num;
c = den2;
d = num2;
for (i = a * b; i > 1; i--)
{
if ((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
for (j = 50; j > 1; j--)
{
if ((c % j == 0) && (d % j == 0))
{
c /= j;
d /= j;
}
}
den = a;
num = b;
den2 = c;
num2 = d;
}
int Reduce(int a, int b)
{
int calcden = a;
int calcnum = b;
if(b == 0)
{
return a;
}
else
{
return Reduce(b, a % b);
}
}
void print(int &num, int &den)
{
cout << "The reduced and added fraction is " << num << "/" << den << endl;
}
int main()
{
int num, den, num2, den2;
int calcnum = 0;
int calcden = 0;
int a = 0;
int b = 0;
int m = 0;
int n = 0;
//int i;
//int rand ();
cout << "What is 3/4 * 3/5?" << endl;
//cout << "What is" << num[i]<< "/" << den[i]<< "*" << num2[i]<<"/"<<den2[i]<<"?"<<endl;
cout << "enter numerator / denominator" << " " << "Your answer must be in reduced form" << endl;
cin >> num >> den;
multiply(num, den, num2, den2);
Reduce(a, b);
print(num, den);
cout << endl;
return(0);
}


