#include <iostream>
#include <ctime>
using namespace std;
class Fraction
{
private:
int Num;
int Denom;
public:
void Reduce(); //will reduce the fraction to lowest terms
void AddFraction(); //will add the two fractions together
void print(); //will print the fraction in the form: numerator / denominator
};
void Reduce(int &Num, int &Denom, int &Num2, int &Denom2)
/* This function is called after ReadFraction(). This function will
reduce the two fractions.
Pre: Two Fractions
Post: Two reduced fractions */
{
int a, b, c, d, i, j = 0;
a = Denom;
b = Num;
c = Denom2;
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;
}
}
Denom = a;
Num = b;
Denom2 = c;
Num2 = d;
}
void Reduce(int &Num, int &Denom)
/* This function is called from AddFraction(). The fraction added in
AddFraction() is reduced here.
Pre: One fraction added from two
Post: A reduced fraction */
{
int a = 0;
int b = 0;
int i = 0;
a = Denom;
b = Num;
for (i = 50; i > 1; i--)
{
if ((a % i == 0) && (b % i == 0))
{
a /= i;
b /= i;
}
}
Denom = a;
Num = b;
}
void print(int &Num, int &Denom)
/* This function displays the reduced and added fraction. This
function is called after AddFraction()
Post: Prints fraction */
{
cout << "The reduced and added fraction is " << Num << "/" << Denom << endl;
}
int main ()
{
int numbers[3];
int answer; // USER INPUT
srand (time(0)); // ONLY NEEDS TO BE SEEDED ONCE
// NO NEED FOR A COUNTER IN WHILE LOOPS
//Initiate checking for correct answer
while (answer != -1) // WHILE USER HASN'T ENTERED -1
{
numbers[0] = rand() % 9 + 1; // load the first random number into numbers[0]
numbers[1] = rand() % 9 + 1; // load the second into numbers[1]
numbers[2] = numbers[0] * numbers[1]; // load the answer into numbers[2]
cout << "\n\n""What is " << numbers[0] << "*" << numbers[1] << "?" << endl;
cin >> answer;
if (answer == numbers[0] * numbers[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;
}
KRal
-1
Light Poster
Recommended Answers
Jump to PostThis is a very bad way to ask for help. Don't just paste all your code and ask us to fix it.
Use the debugger and common sense to narrow down the problem area. Then ask your question in WORDS, and maybe have 3 lines of code to show …
Jump to PostYou should also consider using a better reduction algorithm. I think you probably don't know about Euclid's Algorithm. It would be very helpful for this problem, and you would have to test every value in the interval ( 1, a*b ].
All 8 Replies
Zcool31
0
Junior Poster in Training
dusktreader
137
Posting Whiz in Training
KRal
-1
Light Poster
KRal
-1
Light Poster
jonsca
1,059
Quantitative Phrenologist
Team Colleague
Featured Poster
dusktreader
137
Posting Whiz in Training
KRal
-1
Light Poster
dusktreader
137
Posting Whiz in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.