9 Replies - 1006 Views - Last Post: 27 March 2011 - 12:26 PM Rate Topic: -----

#1 Ness69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-January 11

Rational Numbers

Posted 27 March 2011 - 11:59 AM

I have an assignment due tomorrow and these are the requirements
Create a class RationalNumber (fractions) with the following functionality:
● Has a constructor that prevents a 0 denominator in a fraction and calls the reduce function to simplify the fraction
● Has a default constructor
● Has a private member function that reduces a fraction (see below)
● Has accessor and mutator functions for both numerator and denominator
● Has a function that displays the fraction in appropriate format. For example, the fraction 3/1 should be displayed as 3 and 7/2 as 3 1/2

This class should be implemented in two files, a header (.h) file and a source (.cpp) file. There should be a third file that contains a main function. In the main function, create and display at least three fractions including one that is reduced by the constructor, one with a denominator of 1 and one with a numerator greater than the denominator.

I have done everything, yet it doesnt want to run. anyone care to look it over

rational.cpp
#include <iostream>
#include "rational.h"
using namespace std;


int main()
{
	cout << "Fraction 1 = ";
	RationalNum rn(3,1);

	cout << "Fraction 2 = ";
	RationalNum rn1(2,6);

	cout << "Fraction 3 = ";
	RationalNum rn2(7,2);

	return 0;
}

RationalNum:: RationalNum()
{
	Numerator = 0;
	Denominator = 0;

	cout << 0;
}

RationalNum::RationalNum(int n, int d)
{
	Numerator = n;
	Denominator = d;

	if(d==0)
		cout << "Entry is Invalid" << endl;
	else
		reduce();
}

void RationalNum::setNumerator(int n)
{
	Numerator = n;
}

void RationalNum::setDenominator(int d)
{
	Denominator = d;
}

int RationalNum::getDenominator()
{
	return Denominator;
}

int RationalNum::getNumerator()
{
	return Numerator;
}

//Function to redure the fraction
void RationalNum::reduce()
{
	int Largest, GCD = 1; // Greatest Common Divisor

	Largest = (Numerator > Denominator) ? Numerator: Denominator;
	for(int loop = 2; loop <=Largest; loop++)
		if(Numerator % loop == 0 && Denominator % loop == 0)

	GCD = loop;
	Numerator /= GCD;
	Denominator /= GCD;

	display();
}

void RationalNum::display()
{
	if(Denominator == 1)
		cout << Numerator << endl;
	else if(Numerator < Denominator)
		cout << Numerator << "/" << Denominator << endl;
	else
	{
		double Whole = (double) Numerator/Denominator;
		int Mixed = Whole;
		Whole = Whole - Mixed;
		Whole*=20;
		cout << Mixed << " ";
		RationalNum(10,20);
	}
}



rationaltester.cpp
#include <iostream>
#include "rational.h"
using namespace std;

int main()
{
	RationalNum rn(7,3);
	return 0;
}



rational.h
#ifndef RATIONALNUM_H
#define RATIONANUM_H

class RationalNum
{
private:
	int Numerator;
	int Denominator;
	void reduce();

public:
	RationalNum();
	RationalNum(int, int);

	void setNumerator(int);
	void setDenominator(int);
	void display();

	int getNumerator();
	int getDenominator();

};
#endif 



Is This A Good Question/Topic? 0
  • +

Replies To: Rational Numbers

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Rational Numbers

Posted 27 March 2011 - 12:03 PM

If you are getting compiler error messages, you should post the exact error messages, including the program line numbers that are referenced in the errors.

If it compiles & doesn't give you the expected results, what output do you expect and what output do you get?
Was This Post Helpful? 0
  • +
  • -

#3 Ness69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-January 11

Re: Rational Numbers

Posted 27 March 2011 - 12:05 PM

i want this output
6	int main()
07	{
08	    cout << "Fraction 1 = ";
09	    RationalNum rn(3,1);
10	 
11	    cout << "Fraction 2 = ";
12	    RationalNum rn1(2,6);
13	 
14	    cout << "Fraction 3 = ";
15	    RationalNum rn2(7,2);
16	 
17	    return 0;
18	}

Was This Post Helpful? 0
  • +
  • -

#4 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Rational Numbers

Posted 27 March 2011 - 12:06 PM

First off, what output are you getting?
If it does not run at all, what errors are you getting?

otherwise, we can't really help much.

Although one problem is :
    for(int loop = 2; loop <=Largest; loop++)
        if(Numerator % loop == 0 && Denominator % loop == 0)
    GCD = loop;
    Numerator /= GCD;
    Denominator /= GCD;



think about this.
if the condition is met, GDC = loop.
However even if it isn't met, they are both divided by GDC
Was This Post Helpful? 0
  • +
  • -

#5 Ness69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-January 11

Re: Rational Numbers

Posted 27 March 2011 - 12:07 PM

Got it. I dont need the tester.
Well Can you look over the code for rational.cpp
what would you change to make it look better and more professional
Was This Post Helpful? 0
  • +
  • -

#6 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Rational Numbers

Posted 27 March 2011 - 12:08 PM

View PostNess69, on 27 March 2011 - 12:05 PM, said:

i want this output
6	int main()
07	{
08	    cout << "Fraction 1 = ";
09	    RationalNum rn(3,1);
10	 
11	    cout << "Fraction 2 = ";
12	    RationalNum rn1(2,6);
13	 
14	    cout << "Fraction 3 = ";
15	    RationalNum rn2(7,2);
16	 
17	    return 0;
18	}


Okay, but what output do you want in the console window?
You are getting the output shown there, but we want the output that you want to see, and the output that you are getting
Was This Post Helpful? 0
  • +
  • -

#7 Ness69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 27-January 11

Re: Rational Numbers

Posted 27 March 2011 - 12:09 PM

output is
fraction 1 = 3
fraction 2 = 1/3
fraction 3 = 3 1/2
Was This Post Helpful? 0
  • +
  • -

#8 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: Rational Numbers

Posted 27 March 2011 - 12:09 PM

Also, now that I look at the code again, why have you got two main functions?
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Rational Numbers

Posted 27 March 2011 - 12:12 PM

View PostNess69, on 27 March 2011 - 02:09 PM, said:

output is
fraction 1 = 3
fraction 2 = 1/3
fraction 3 = 3 1/2

In your main function (in rational.cpp), for each of the fractions, all you are doing is calling the constructor to create each RationalNum. If you want to display each number you also have to call your display() function for each one.

edit: sorry, nevermind, I see that your reduce() function calls display().

So, it is displaying the fractions. What's the problem?

This post has been edited by r.stiltskin: 27 March 2011 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Rational Numbers

Posted 27 March 2011 - 12:26 PM

To comply with the "separate files" requirement, you should take the main function out of rational.cpp and use that to replace the main function in rationaltester.cpp.

Then, when you compile both rationaltester.cpp and rational.cpp together, it will run correctly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1