473,396 Members | 1,843 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.

please fix this so it works

can some one fix this so it actually reduces a fraction, and actually works?
I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html
Jul 19 '05 #1
5 2275
Please don't post homework so that others can do it for you
"Luis" <ki***@comcast.net> wrote in message
news:T4*******************@rwcrnsc52.ops.asp.att.n et...
can some one fix this so it actually reduces a fraction, and actually works? I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html

Jul 19 '05 #2

"Luis" <ki***@comcast.net> wrote in message
news:T4*******************@rwcrnsc52.ops.asp.att.n et...
can some one fix this so it actually reduces a fraction, and actually works? I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html


Your code is confused, it can't be easily fixed. Look at the code for GCF

int Fraction :: GCF()
{
int remainder = 1;
int gcf;
while(remainder!=0)
{
remainder = denominator%numerator;
denominator = numerator;
numerator = remainder;
}
gcf = denominator;
return gcf;
}

Why is GCF a member of Fraction? What does calculating a GCF have to do with
fractions? More to the point why does GCF change the numerator and
denominator? Its the job of Reduce to change the numerator and denominator
by dividing by the GCF. Its not the job of GCF to do that. As I said you
are confused.

First job, write this function

int GCF(int x, int y)
{
// code to calculate GCF of x and y
return gcf;
}

Not this function is not a member of Fraction, its doesn't have anything to
do with Fractions, all it does it calculate a GCF. This is a good rule for
programming; one function, one job.

Now look at Reduce

Fraction Fraction :: Reduce ()
{
Fraction reducedFraction;
reducedFraction.set(numerator/GCF(),denominator/GCF());
return reducedFraction;
}

You've written Reduce to return a new Fraction, not reduce the existing one.
That's not wrong but its a bit strange. Its seems much more logical to
reduce the existing fraction When I look at DividedBy I see more evidence of
this confusion.

Fraction Fraction :: DividedBy (Fraction otherFraction)
{
Fraction reducedFraction;
Fraction quotient(numerator * otherFraction.denominator,
denominator * otherFraction.numerator);
Reduce();
return quotient;
}

Which fraction to do think is being reduced by the call to Reduce here?
reducedFraction? quotient? this? The answer is none of them! You wrote
Reduce to create a new Fraction which it returns, but when you call Reduce
here, you throw the return value away! You call to Reduce does nothing at
all (apart from waste some time).

Lets rewrite reduce so that it reduces the existing fraction, not returns a
new one, also let is use the GCF function I said you should write above.

void Fraction :: Reduce ()
{
int gcf = GCF(numerator, denominator);
numerator /= gcf;
denominator /= gcf;
}

See how it deivdes the existing Fractions numerator and denominator, its
doesn't try to create a new Fraction. The return type is void.

Now let rewrite DividedBy to use the new Reduce

Fraction Fraction :: DividedBy (Fraction otherFraction)
{
Fraction quotient(numerator * otherFraction.denominator,
denominator * otherFraction.numerator);
quotient.Reduce();
return quotient;
}

See how I call quotient.Reduce() to reduce quotient, simple!

Carry on in this vein, and you'll be getting closer. Its very important to
think clearly about you program design, you cannot throw together a few
functions that look roughly right and expect to get anything to work.

john
Jul 19 '05 #3

"Luis" <ki***@comcast.net> wrote in message
news:T4*******************@rwcrnsc52.ops.asp.att.n et...
can some one fix this so it actually reduces a fraction, and actually works? I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html


In your reduce function you have:

reducedFraction.set(numerator/GCF(),denominator/GCF());

you call numerator/GCF() first then denominator/GCF()
The two GCF() calls will return different values, instead execute GCF() once
and store it and then divide the two, i.e

gcf = GCF();
reducedFraction.set(numerator/gcf, denominator/gcf);

HTH
Allan
Jul 19 '05 #4

"Mark" <bi***@charter.net> wrote in message
news:vj************@corp.supernews.com...
Please don't post homework so that others can do it for you
"Luis" <ki***@comcast.net> wrote in message
news:T4*******************@rwcrnsc52.ops.asp.att.n et...
can some one fix this so it actually reduces a fraction, and actually

works?
I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html



Hmm top posting - I was told off for that when I started out here.
I think a bit more constructive effort is required - at least the OP has
attempted something, other than just asking someone to do it from scratch.
Allan
Jul 19 '05 #5

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:bg**********@news.freedom2surf.net...

"Luis" <ki***@comcast.net> wrote in message
news:T4*******************@rwcrnsc52.ops.asp.att.n et...
can some one fix this so it actually reduces a fraction, and actually works?
I get a windows error when i try to run it.

http://www.rafb.net/paste/results/wS672281.html


In your reduce function you have:

reducedFraction.set(numerator/GCF(),denominator/GCF());

you call numerator/GCF() first then denominator/GCF()
The two GCF() calls will return different values, instead execute GCF()

once and store it and then divide the two, i.e

gcf = GCF();
reducedFraction.set(numerator/gcf, denominator/gcf);

HTH
Allan


But his GCF function also changes the Fractions state. Its very confused
coding which doesn't have a quick fix.

john
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Danny Masti | last post by:
Hello, I have a HIDDEN div with a "Please Wait Message". OnSubmit I show the hidden div with the "Please Wait Message". It works fine. But if I replace the "Please Wait Message" with an animated...
2
by: TeknoCat | last post by:
Hey everyone, I may be repeating myself here, but if someone sent a reply then I missed it, and I can't get Outlook Express to download any messages more than 2 days old. Anyway, I'm having a...
0
by: Jane Withnolastname | last post by:
I have a page layout with a couple of absolute divs. It looks great on Moz. On IE, the second div doesn't center as it does in Moz. When I change the second div to relative, it works in IE, but in...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
6
by: Raul Rodriguez | last post by:
I posted this question already but nobody could answer me. Please forgive for doing it again, but it's very important for my company. We have been searching for this error with different engines...
28
by: Tim_Mac | last post by:
hi, i'm new to .net 2.0, and am just starting to get to grips with the gridview. my page has autoEventWireUp set to true, which i gather is supposed to figure out which handlers to invoke when...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
8
by: John Austin | last post by:
I need to understand why if I add a control and use AddHandler to connect its click event, it will work in Page_Load, but not in a Button_Click. The idea is that the user types some data, presses...
9
by: ckfan.painter | last post by:
I've run into a seemingly bizarre problem with insert() for std::vector. (This was done on Microsoft Visual C++ 2005 express version 8...maybe it is a compiler specific bug?) Here's the code: ...
6
by: shapper | last post by:
Hello, I am creating a form that includes a few JQuery scripts and TinyMCE Editor: http://www.27lamps.com/Beta/Form/Form.html I am having a few problems with my CSS: 1. Restyling the Select
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
Oralloy
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...

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.