By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

would this work?

P: n/a
I am trying to write a program that checks to see if a fraction is
reducable, and to do this both numerator and denominator cannot be tre(
prime) will this work? if not can some one fix it up a bit for me? thanks.

# include <iostream.h>
using namespace std;

int main()
{
int numerator;
int denominator;
bool denom;
bool numer;
cin>>numerator;

for (int numCount = 2;numCount<=numerator/2 || numer == false;numCount++)
{
if(numerator%numCount== 0)
{

numer = false;
break;
}
else
{
numer = true;
}
}
cout<<numer ;

return 0;

}

Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Luis wrote:
I am trying to write a program that checks to see if a fraction is
reducable, and to do this both numerator and denominator cannot be tre(
prime) will this work? if not can some one fix it up a bit for me? thanks.
What does 'tre (prime)' mean?

That both the numerator and denominator are prime is a sufficient but
unnecessary condition for the fraction to be in its lowest terms.
Consider three-quarters.
# include <iostream.h>

using namespace std;

int main()
{
int numerator;
int denominator;
bool denom;
bool numer;

cin >> numerator;
You didn't initialise 'numer'. It is undefined whether even one
iteration of this loop will be done.
for (int numCount = 2; numCount <= numerator / 2 || numer == false; numCount ++)
{
if(numerator % numCount == 0)
{
numer = false;
break;
}
else
{
numer = true;
}
}
Rather than using a separate flag, you could test how far the loop got
by checking the value of numCount afterwards. (Of course, you would have
to move the declaration of numCount above the for statement.)
cout << numer;

return 0;
}


A fraction is reducable if its numerator and denominator are coprime,
that is, if their greatest common divisor is 1. Use Euclid's algorithm
to calculate a gcd.

It seems one of your class mates sought help here yesterday (on Monday).
You might find something useful in the replies he got.

Good luck,
Buster

Jul 19 '05 #2

P: n/a
I just realized that i actually do not need this function at all becasue I
already have a GCF function, which will determine the gcf obviously, and if
a gcf is great than 1, then it is reducable, is that correct?
"Buster Copley" <bu****@none.com> wrote in message
news:bg**********@news6.svr.pol.co.uk...
Luis wrote:
I am trying to write a program that checks to see if a fraction is
reducable, and to do this both numerator and denominator cannot be tre(
prime) will this work? if not can some one fix it up a bit for me? thanks.

What does 'tre (prime)' mean?

That both the numerator and denominator are prime is a sufficient but
unnecessary condition for the fraction to be in its lowest terms.
Consider three-quarters.
# include <iostream.h>

using namespace std;

int main()
{
int numerator;
int denominator;
bool denom;
bool numer;

cin >> numerator;


You didn't initialise 'numer'. It is undefined whether even one
iteration of this loop will be done.
for (int numCount = 2; numCount <= numerator / 2 || numer == false;

numCount ++) {
if(numerator % numCount == 0)
{
numer = false;
break;
}
else
{
numer = true;
}
}


Rather than using a separate flag, you could test how far the loop got
by checking the value of numCount afterwards. (Of course, you would have
to move the declaration of numCount above the for statement.)
cout << numer;

return 0;
}


A fraction is reducable if its numerator and denominator are coprime,
that is, if their greatest common divisor is 1. Use Euclid's algorithm
to calculate a gcd.

It seems one of your class mates sought help here yesterday (on Monday).
You might find something useful in the replies he got.

Good luck,
Buster

Jul 19 '05 #3

P: n/a


Luis wrote:

I just realized that i actually do not need this function at all becasue I
already have a GCF function, which will determine the gcf obviously, and if
a gcf is great than 1, then it is reducable, is that correct?


Yes.
If you think if it: What do you do with the gcf (or gcd). You divide
the numerator and the denumerator by it to obtain a new fraction which
represents the same value but with different (smaller) numbers. So when
the gcf equals 1 you still can perform the divide but you will end up with,
uhm aeh, the same numbers. Thus you have not reduced the fraction in this
case.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.