# Simplified Ratio Problem

Page 1 of 1

## 10 Replies - 6732 Views - Last Post: 26 August 2012 - 08:13 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=289939&amp;s=d123f577d90e9c0a56d86adc4d686308&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aviateplus

• New D.I.C Head

Reputation: 0
• 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

• Code herder

Reputation: 6221
• Posts: 21,473
• 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.

### #3 aviateplus

• New D.I.C Head

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

## Re: Simplified Ratio Problem

Posted 25 August 2012 - 01:17 PM

Skydiver, 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"?

### #4 Skydiver

• Code herder

Reputation: 6221
• Posts: 21,473
• 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.

### #5 aviateplus

• New D.I.C Head

Reputation: 0
• 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?

### #6 Hezekiah

Reputation: 208
• 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;
```

### #7 Skydiver

• Code herder

Reputation: 6221
• Posts: 21,473
• 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.

### #8 aviateplus

• New D.I.C Head

Reputation: 0
• 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.

### #9 Skydiver

• Code herder

Reputation: 6221
• Posts: 21,473
• 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.

### #10 Skydiver

• Code herder

Reputation: 6221
• Posts: 21,473
• 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

### #11 aviateplus

• New D.I.C Head

Reputation: 0
• 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!