Connecting Tech Pros Worldwide Forums | Help | Site Map

would this work?

Luis
Guest
 
Posts: n/a
#1: Jul 19 '05
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;

}




Buster Copley
Guest
 
Posts: n/a
#2: Jul 19 '05

re: would this work?


Luis wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
> # include <iostream.h>
>
> using namespace std;
>
> int main()
> {
> int numerator;
> int denominator;
> bool denom;
> bool numer;
>
> cin >> numerator;[/color]

You didn't initialise 'numer'. It is undefined whether even one
iteration of this loop will be done.
[color=blue]
> for (int numCount = 2; numCount <= numerator / 2 || numer == false; numCount ++)
> {
> if(numerator % numCount == 0)
> {
> numer = false;
> break;
> }
> else
> {
> numer = true;
> }
> }[/color]

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.)
[color=blue]
> cout << numer;
>
> return 0;
> }[/color]

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

Luis
Guest
 
Posts: n/a
#3: Jul 19 '05

re: would this work?


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" <buster@none.com> wrote in message
news:bgn9uc$2u5$1@news6.svr.pol.co.uk...[color=blue]
> Luis wrote:[color=green]
> > 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?[/color][/color]
thanks.[color=blue]
>
> 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.
>[color=green]
> > # include <iostream.h>
> >
> > using namespace std;
> >
> > int main()
> > {
> > int numerator;
> > int denominator;
> > bool denom;
> > bool numer;
> >
> > cin >> numerator;[/color]
>
> You didn't initialise 'numer'. It is undefined whether even one
> iteration of this loop will be done.
>[color=green]
> > for (int numCount = 2; numCount <= numerator / 2 || numer == false;[/color][/color]
numCount ++)[color=blue][color=green]
> > {
> > if(numerator % numCount == 0)
> > {
> > numer = false;
> > break;
> > }
> > else
> > {
> > numer = true;
> > }
> > }[/color]
>
> 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.)
>[color=green]
> > cout << numer;
> >
> > return 0;
> > }[/color]
>
> 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
>[/color]


Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 19 '05

re: would this work?




Luis wrote:[color=blue]
>
> 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?[/color]

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
kbuchegg@gascad.at
Closed Thread