473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

would this work?

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
3 1939
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Thomas Heller | last post by:
I'm currently working on a new version of py2exe, which will require Python 2.3 and later, because it uses the zipimport mechanism. Since py2exe is a distutils extension, and since C compilers...
3
by: Unforgiven | last post by:
I have the following situation: Given this class: template<typename T> class Expression { /* omitted */ }; This is the base class for a BooleanExpression and an ArithmeticExpression.
43
by: Mountain Bikn' Guy | last post by:
I have a situation where an app writes data of various types (primitives and objects) into a single dimensional array of objects. (This array eventually becomes a row in a data table, but that's...
5
by: scorpion53061 | last post by:
I would like your thoughts and impressions of the events of today and ask how you would handle this or if I am wrong and this is "normal" behaviour for a web provider and IIS. I am normally a...
1
by: laurakr | last post by:
I am trying to use a clear to get my bottom nav bar below the quote box on the right, but it isn't working. I would like the bottom edge of the quote box to "stick" to the footer nav bar but copy...
4
by: smerf | last post by:
I like UltraVNC for helping remote users, and the authors have a server called Nat2Nat that you can hook up to to get thru firewalls without having to set up ports and such. Problem is that...
5
by: Ronald S. Cook | last post by:
I need ideas on how to best design a Windows form for my particular situation. On a cattle feeding yard there will be between about 300 and 600 pens. On my "Pen Feeding Sequence" form, I want...
0
by: The Eternal Squire | last post by:
To all, About twenty years ago, I was admitted to a Ph.D. program for computer science. It was also around that time that I was diagnosed for chronic depression, which forced me out of that...
21
by: Osiris | last post by:
suppose I wanted to make availble on the WWW a facility to do some rather floating-point-calculation-intensive stuff. I can do that in PHP, but then all work must be done on the hosters' computer...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.