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<=num erator/2 || numer == false;numCount+ +)
{
if(numerator%nu mCount== 0)
{
numer = false;
break;
}
else
{
numer = true;
}
}
cout<<numer ;
return 0;
} 3 1955
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
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.co m> wrote in message
news:bg******** **@news6.svr.po l.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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 are
commonly available on most platforms except Windows, it would be fairly
easy to let py2exe generate a C source file installing this import hook,
and let distutils' C compiler build an executable file from this.
Would this be useful, or would...
|
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.
|
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 another story.) The data is
written once and then read many times. Each primitive read requires
unboxing. The data reads are critical to overall app performance. In the
hopes of improving performance, we have tried to find a way to avoid the...
|
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 vb.net windows programmer but I needed to do some web work
that involved web pages and data access. I was excited when I found out my
web provider had the ability to have ASP.NET applications built there.
I was in the process of attempting to...
|
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 to be able to flow
to the left of the quote box. For the bonus round I would like the
right edge of the box to align with the right edge of the footer Could
someone take a look at my code and help a newbie troubleshoot? my email
is lroeder at...
| |
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 thier Nat2Nat is not exactly reliable and they ignore any
questions about its source code.
It looks like they are simply starting a UDP connection from each
client/server VNC combo to the Nat2Nat server and handing off the UDP
connection once...
|
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 the user to be able to setup and
maintain the order in which the pens should be fed (ultimately in the Pen
table is a column named Sequence that gets an interger value).
So on the form...
|
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 program into the working word.
I had done my best since then as a software engineer, until late 2005,
when I
|
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 and PHP is SLOOOOOW.
Compiled stuff would be much faster, but no commercial hoster would allow
LAPACK stuff running on his machines.
In what way could java be of help ?
Are there hosters that allow my java stuff running on their machine and...
|
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 more why C++/CLI would be better to PInvoke than doing
the PInvoke in
C#?
Because, usually in C# as you already know we use DLLImport and extern
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |