arithmetic functions

Hey there, I need help understanding the assignment I'm working on.

I need to define four arithmetic functions named add, sub, mult, and div
Each function should accept one explicit fraction object as an argument passed by value and should return the result as a new fraction object returned by value. The
implicit object is the left hand operand and the explicit object is the right hand
operand. Do not change the two original fraction objects.

i do have these formulas:

Addition: a/b + c/d = (a*d + b*c) / (b*d)
Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
Multiplication: a/b * c/d = (a*c) / (b*d)
Division: a/b / c/d = (a*d) / (b*c)

so with the function, in the .cpp file, i put the function outside of int main(), but does it start with

1
2
3
4
5

void add ()
    {
      a/b + c/d = (a*d + b*c) / (b*d) 
    }


Not really sure. I have to use 3 files, the driver file, my .cpp file and a header file with 2 private member variables (which I've created)

1
2
3
4
5
6
7
8
// function.h file
class fraction
{
private:
	int numerator; // the numberator 
	int denominator; // the denominator
	
};


anything will help. there is a lot more to the assignment, but im just trying to get started and understand it
this is my .cpp file so far, it needs alot of work, and clarification, so be gentle... lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

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

int a;
int b;
int c;
int d;

int main ()
{




/*
Addition: a/b + c/d = (a*d + b*c) / (b*d)
Subtraction: a/b - c/d = (a*d - b*c) / (b*d)
Multiplication: a/b * c/d = (a*c) / (b*d)
Division: a/b / c/d = (a*d) / (b*c)
*/














return 0;
}

void add (int a, int b, int c, int d)
{
	a/b + c/d = (a*d + b*c) / (b*d)
}
a/b + c/d = (a*d + b*c) / (b*d)
That is nonsensical in C++. You've simply written down a formula.

Be aware that in C++ and C, an int divided by an int gives an int. So what is 3/2? One. What is 2/3? Zero.
im just trying to get started; C++ has proven to be a royal pain in my butt... I'm in 1410 at a local college and im still getting the hang of it; and based of the code, i have a long way to go
I think you should start with something simpler, like adding and multiplying integers.
Is there more to the brief? It's a little bit confusing.

I mean, I understand the concept; writing functions that will perform arithmetic. But some of it is phrased horribly. The function will take one explicit parameter? How does it know what to add? What's this implicit left hand side object?

Are the functions supposed to be a part of the fraction class? That would make a little more sense, at least. If not, I fail to see where this implicit fraction lives.
The implicit object is the left hand operand and the explicit object is the right hand operand.


Sounds like the idea is to define a fraction object, and then overload '+' so that you can do something like:

fractionObjectOne + fractionObjectTwo;



that makes a little sense; but since functions and objects are my favorite thing, i tend to struggle with them tremendously. where is the best way to understand them>?

It's a miracle what the search function can do on this website! I've found some function basics, I hope it makes sense. I can usually get most things, but for some reason, functions are KILLER!

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
@Moschops, yeah, that's what I would have thought. My next question was going to be along the lines of why we're favouring functions instead of overloaded operators for this.
so how would I overload that function? how does the addition function look?
I'm reluctant to give the code because you've mentioned that this is classwork.

I'm happy to give some pointers, though.

Let's look at the addition. Let's also assume that the function required is part of the fraction class.

The brief asks you to give a fraction as an argument and return a fraction. It mentions that we don't want to change any of the fractions give, which is a huge hint towards using the const keyword.

So, your prototype will look like this:
 
fraction Add( fraction f const ) const;


The first const is promising we're not going to change the fraction argument that's been passed in within the scope of the function. The second const promises that we're not going to make any changes to the members of the class we're working on.

I've noticed you've interpreted the formulae you've been given as straightforward equations with divisions and such. They're not. It's telling you how to calculate the numerator and denominator separately for each operation.

Addition: a/b + c/d = (a*d + b*c) / (b*d)

This means that the numerator of the new fraction is (a*d + b*c).
The denominator will be (b*d).
Where a/b is your member fraction and c/d is the fraction you pass into the function.

The skeleton code for your function would be as follows.
Create a temporary fraction within your function.
Set temporary numerator to a*d + b * c.
Set temporary denominator b * d.
Return temporary fraction.


As discussed, this will work a lot nicer when you look at operator overloading. At the moment, you'd have to do something like this to get a new fraction:
1
2
3
4
5
fraction A, B, C;

// Let's assume that fractions A and B have been populated

C = A.Add( B );


When you overload the operators (which is surprisingly easy to do and a stones throw away from your function code), you can implement something like this:
1
2
3
4
5
fraction A, B, C;

// Let's assume that fractions A and B have been populated

C = A + B;


I would work on getting the functions in place, as operator overloading might be jumping the gun for you a little and it's not part of the given brief. It's inclusion here is merely for demonstration purposes.

Hope this helps.
iHutch105, you helped more than you think. Its still fuzzy in some areas, BUT i have something that I can re-read over and over till I get it! :)

I just need to do more reading ; and that should help i think. Any good books or online resources?
I have no clue on books, to be honest; I learned most of my C++ from University and this site. This site is an excellent resource for various libraries and concepts.

I hear a lot of people recommend one of Stroustrup's books. I forget the title. All I remember it's that it's not the Stroustrup book that I have, which is a fairly hideously written tome called "The C++ Programming Language". I think the one people go for might be called "Principles and Practice Using C++".

Other than that, if there's something in particularly you're struggling with there'll always be someone knocking about on this forum who are willing to help. :-)
Topic archived. No new replies allowed.