would this work?

L

Luis

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;

}
 
B

Buster Copley

Luis said:
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
 
L

Luis

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?
 
K

Karl Heinz Buchegger

Luis said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top