10 Replies - 2713 Views - Last Post: 26 August 2012 - 08:13 PM Rate Topic: -----

#1 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

Simplified Ratio Problem

Posted 24 August 2012 - 11:31 PM

Hi,

I'm beginning in C++ so my code is probably redundant and/or excessively lengthy.

The goal: Calculate the ratio of 5-year average wins to 5-year average losses.

Progress: So far I've been able to gather the data and define "AverageWin" and "AverageLoss"

Problem: I am having difficulty working with the Euclid Algorithm to find the greatest common factor, then reduce the "AverageWin" and "AverageLoss" to simplest terms and present them in terms of a ratio (x:x).

Errors: "Variables a,b,gcd being used without being initialized."
"Integer division by zero"

I've researched as much as I could and tried to work the algorithm towards my needs but I can't seem to figure it out.

Any help would be appreciated, thank you.




#include <iostream>
using namespace std;

int main(  )
{
	int year1Win, year2Win, year3Win, year4Win, year5Win;
	double AverageWin;
	cout << "Enter the number of wins for year 1: ";
	cin >> year1Win;
	cout << "Enter the number of wins for year 2: ";
	cin >> year2Win;
	cout << "Enter the number of wins for year 3: ";
	cin >> year3Win;
	cout << "Enter the number of wins for year 4: ";
	cin >> year4Win;
	cout << "Enter the number of wins for year 5: ";
	cin >> year5Win;
	AverageWin = (year1Win + year2Win + year3Win + year4Win + year5Win)/5;
	cout << "Five year average win for this team is: " << AverageWin;
	cout << "\n";

	int year1Loss, year2Loss, year3Loss, year4Loss, year5Loss;
	double AverageLoss;
	cout << "Enter the number of losses for year 1: ";
	cin >> year1Loss;
	cout << "Enter the number of losses for year 2: ";
	cin >> year2Loss;
	cout << "Enter the number of losses for year 3: ";
	cin >> year3Loss;
	cout << "Enter the number of losses for year 4: ";
	cin >> year4Loss;
	cout << "Enter the number of losses for year 5: ";
	cin >> year5Loss;
	AverageLoss = (year1Loss + year2Loss + year3Loss + year4Loss + year5Loss)/5;
	cout << "Five year average loss for this team is: " << AverageLoss;
	cout << "\n";

	int n, d, gcd, a, b, smplfyWin, smplfyLoss;
	AverageWin=a;
	AverageLoss=b;
	while (gcd!=0) { gcd = a % b; a = b; b = gcd; }
	smplfyWin = (a/gcd);
	smplfyLoss = (b/gcd);

	cout << "The ratio of wins to losses is " << smplfyWin;
	cout << ":"<< smplfyLoss;
	cout << "\n";
	
	system ("pause");
	return 0;



Is This A Good Question/Topic? 0
  • +

Replies To: Simplified Ratio Problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Simplified Ratio Problem

Posted 24 August 2012 - 11:48 PM

Considering that the only way to exit the while loop on line 41 is if gcd is zero, then no wonder you are dividing by zero on the next line.

Also notice that you are entering the while loop with gcd not initialized to anything. Take a closer look at the reference where you got the algorithm to see how to properly initialize it.
Was This Post Helpful? 0
  • +
  • -

#3 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

Re: Simplified Ratio Problem

Posted 25 August 2012 - 01:17 PM

View PostSkydiver, on 24 August 2012 - 11:48 PM, said:

Considering that the only way to exit the while loop on line 41 is if gcd is zero, then no wonder you are dividing by zero on the next line.

Also notice that you are entering the while loop with gcd not initialized to anything. Take a closer look at the reference where you got the algorithm to see how to properly initialize it.


So how do you write that GCD is > 0 < "a" or "b"?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Simplified Ratio Problem

Posted 25 August 2012 - 02:02 PM

This is a case of poorly named variables. Your variable name gcd is not actually the GCD. Another one of your variables is holding it.

This is why I told you to go back to wherever you got the algorithm and take a closer look.

If you don't remember where you got it, here is a bad example of how to compute the GCD. It is bad not only because the cognitive load it puts on the reader, but because of the irony that the variable that ends up holding the greatest common denominator happens to not be named denominator.
Was This Post Helpful? 0
  • +
  • -

#5 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

Re: Simplified Ratio Problem

Posted 25 August 2012 - 11:36 PM

Ok I started from scratch again with a different snippet of code for the algorithm.

This time it went all the way through compilation and ran with only an error that some information may be lost due to switching from "double" to "int" but I'm not sure how important that is for what I am doing.

The problem is that after the program calculates the average losses, it hangs and does not show the ratio. There isn't the "Press any key to continue" message, the cursor is simply blinking.

Here is what I added to replace the other algorithm:

	int a,b,gcd;
	a=AverageWin;
	b=AverageLoss;

    int c;

    if ( b == 0 ) 
        return a;

    else
        while ( b != 0 )
        
            c = b;
            b = a % b;
            a = c;
        

        return a;
	
	int smplfyWin,smplfyLoss;
	
	smplfyWin=(a/gcd);
	smplfyLoss=(b/gcd);
	
	cout << "The ratio of wins to losses is " << smplfyWin;
	cout << ":"<< smplfyLoss;
	cout << "\n";
	
	system ("pause");
	return 0;
}




Progress?
Was This Post Helpful? 0
  • +
  • -

#6 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 208
  • View blog
  • Posts: 552
  • Joined: 12-July 09

Re: Simplified Ratio Problem

Posted 25 August 2012 - 11:56 PM

        while ( b != 0 )
        
            c = b;
            b = a % b;
            a = c;

Only the first statement is part of the while loop. If you want the loop to execute multiple statements, you must surround them with {}.

        return a;

Why is that there? It will exit the program before the rest of the code executes.

AverageWin = (year1Win + year2Win + year3Win + year4Win + year5Win)/5;

If you divide two integers, the result will be an integer, e.g. 3/5==0. If you want a double result, one of the operands must be a double:
AverageWin = (year1Win + year2Win + year3Win + year4Win + year5Win)/5.0;

Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Simplified Ratio Problem

Posted 26 August 2012 - 12:20 AM

Once you fix the infinite loop, I think that line 18 will confuse you next. :)
Was This Post Helpful? 0
  • +
  • -

#8 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

Re: Simplified Ratio Problem

Posted 26 August 2012 - 02:01 PM

Hezekiah - thank you for the explanations. I changed the double values to integers and added the braces.

Skydiver - I am only on my second day of class in C++, so needless to say we haven't gotten to loops and Googling how to exit an infinite loop does not help solve the loop that I am stuck in. We were assigned the task with little guidance as to how to complete it. I have learned a lot already, but I don't know how to get out of this loop or if it will even work correctly once I am.

With this code:
int a,b,gcd;
	a=AverageWin;
	b=AverageLoss;

    int c;
	{
    if ( b == 0 ) 
        return a;
	
    else
        while (b != 0)
		{
            c = b;
            b = a % b;
            a = c;
		}
	} 
	
	int smplfyWin,smplfyLoss;
	
	smplfyWin=(a/c);
	smplfyLoss=(b/c);



I am able to compile but the ratio appears as "1:0" no matter what numbers I input.

I must be close but I'm not sure what is going on at this point.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Simplified Ratio Problem

Posted 26 August 2012 - 03:02 PM

Your exit condition for the while loop on line 11 is b equal to zero. So of course, on line 22, zero divided by anything else is zero.

Notice that in your while loop, you are modifying the values of a and b. By the time you exit the loop, a and b are not the same values as AverageWin and AverageLoss anymore. Doesn't that imply that you'll want to the simplified win to be average win divided by c? The same goes for average loss.
Was This Post Helpful? 1
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,161
  • Joined: 05-May 12

Re: Simplified Ratio Problem

Posted 26 August 2012 - 03:23 PM

BTW, according to this, it looks like a, not c is the GCD: http://en.wikipedia....Implementations
Was This Post Helpful? 0
  • +
  • -

#11 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

Re: Simplified Ratio Problem

Posted 26 August 2012 - 08:13 PM

I knew I had to be close at this point. After making the needed corrections I was able to get the program to run as I wanted it to.

Thanks for being patient with me, as I said I've barely started with C++.

Now we will see how many others Google this problem and show up to class with the same code.

Kudos!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1