#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;
}

Recommended Answers

All 8 Replies

This 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 an example. If it takes me more than 30 seconds to read your post, most people won't bother.

Or better yet, read your comments/function names.

Last I checked Reduce != ReadFraction.

You 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 ].

Euclid's Algorithm

/*I am a noob to all of this and am looking for a little help in the process of posting and getting assistance. Any at all would great*/

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;

}

Thanks for the advice, i am new to this and am not sure how to post a problem and ask for assistance.

What is the problem you are experiencing with the code. That's what Zcool is trying to get you to express. "It doesn't work" is not enough for anyone to go on. What do you expect from it and how is it breaking?

If you don't know that go back and debug or put some couts in your program at strategic places. At least give us something to go on.

/*I am a noob to all of this and am looking for a little help in the process of posting and getting assistance. Any at all would great*/

1. Ask your quesiont in plain english. Spell it out clearly. Don't make us try to guess what you are trying to do and what your problem is.

2. There are lots of people on here that are experts and are willing ( and sometimes eager ) to help you. They do this ( for the most part ) on a volunteer basis. So, you should be as polite as possible, and you should make it clear what you need. Nobody wants to spend a lot of time guessing what you need.

3. Read the responses that people write. I tried to tell you that Euclid's Algorithm would be very helpful for your program, but apparently you didn't read about it.

4. No one is going to fix your code or write code for you ( usually ). Many people will, however, give advice and try to steer you in the right direction.

I am trying to get the code to multiply two fractions then reduce them. I tried the Euclid's Algorithm without any luck. I keep getting a large number instead of a reduced one.

I am trying to get the code to multiply two fractions then reduce them. I tried the Euclid's Algorithm without any luck. I keep getting a large number instead of a reduced one.

Well, I assure you that Euclid's Algorithm works. You may either not be implementing it correctly, or you aren't applying it correctly. Given two integers, Euclid's Algorithm will tell you their greatest common divisor. It should be obvious how this could be used to reduce two fractions.

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.