Fractions

Well I pretty much have the code 100% complete except that our submission program, Athene, keeps saying something that my CMD does not. I have to perform addition, subtraction, multiplication, and division on fractions and then reduce them. I ran across one problem while doing this, which I though I fixed. Whenever the fractions subtracted and became negative, when I reduced them, the denominator and numerator had swapped signs. So I put in a little bit of code to bypass this (Lines 23-28) and it worked when I ran it through Codeblocks, so I submitted it and Athene showed me the output which had turned the multiplication and division results negative. So I thought maybe it was something that CodeBlocks couldn't detect so I ran it in my CMD on my computer and it also said that I was correct. Here's the code that I submitted with the 'negative' fix. On line 37, I didn't use brackets for the 'IF' statement because I never had to in the past, could that be why it is messing up in Athene?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  #include <iostream>
using namespace std;

// This struct will comprise a Fraction value
struct Fraction {
	int num; // numerator
	int den; // denominator
};

// This calculates greatest common divisor (GCD)
// I expect that you will find it useful
int gcd(int a, int b)
{
	if( b == 0 )
		return a;
	return gcd(b,a%b);
}

// Reduces the given Fraction value lowest possible denominator
void reduce(Fraction &f)
{
  //Make a space to temporarily remove negative.
  int n;
  if(f.num < 0)
  {
      n = f.num;
      f.num = f.num * -1;
  }

  int div;
  div = gcd(f.num,f.den);

  f.num = f.num / div;

  //Put negative back in if there was one.
  if(n < 0)
    f.num = f.num*-1;
  
  f.den = f.den / div;
}

// Add the two given Fractions, return the result as a new Fraction (reduced, of course)
Fraction add(Fraction a, Fraction b)
{
  //find out the least common multiple.
  int lcm;
  lcm = (a.den * b.den)/gcd(a.den,b.den);

  //Get the multiples required for numerator changes.
  int multa, multb;
  multa = lcm/a.den;
  multb = lcm/b.den;

  //Make required changes to fractions.
  a.den = lcm;
  a.num = a.num*multa;
  b.den = lcm;
  b.num = b.num*multb;

  //Create final fraction.
  Fraction F;
  F.num = a.num + b.num;
  F.den = lcm;
  reduce(F);

  return F;
}

// Subtract b from a, return the result as a new Fraction (reduced, of course)
Fraction subtract(Fraction a, Fraction b)
{
  //find the least common multiple.
  int lcm;
  lcm = (a.den * b.den)/gcd(a.den,b.den);

  //Get the multiples required for numerator changes.
  int multa, multb;
  multa = lcm/a.den;
  multb = lcm/b.den;

  //Make required changes to fractions.
  a.den = lcm;
  a.num = a.num*multa;
  b.den = lcm;
  b.num = b.num*multb;

  //Create final fraction.
  Fraction F;
  F.num = a.num - b.num;
  F.den = lcm;

  reduce(F);

  return F;

}

// Multiply the two given Fractions, return the result as a new Fraction (reduced, of course)
Fraction multiply(Fraction a, Fraction b)
{
  Fraction c;
  c.num = a.num * b.num;
  c.den = a.den * b.den;

  reduce(c);
  return c;
}

// Divide given Fractions (a/b), return the result as a new Fraction (reduced, of course)
// You may assume that b will be a non-zero value
Fraction divide(Fraction a, Fraction b)
{
  Fraction c;
  c.num = b.den;
  c.den = b.num;

  c.num = a.num * c.num;
  c.den = a.den * c.den;
  reduce(c);
  return c;
}

int main()
{
    Fraction x;
    cout << "Enter numerator and denominator for X: ";
    cin >> x.num;
    cin >> x.den;

    Fraction y;
    cout << "Enter numerator and denominator for Y: ";
    cin >> y.num;
    cin >> y.den;

    cout << endl;

    Fraction r;
    r = add(x,y);
    cout << "ADD: " << r.num << "/" << r.den << endl;

    r = subtract(x,y);
    cout << "SUB: " << r.num << "/" << r.den << endl;

    r = multiply(x,y);
    cout << "MUL: " << r.num << "/" << r.den << endl;

    r = divide(x,y);
    cout << "DIV: " << r.num << "/" << r.den << endl;

}
Nevermind I got it. I thought the negative swapped in the reduction function but it placed the negative into the gcd. So I put the negative swap in for the gcd instead of a negative numerator like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void reduce(Fraction &f)
{

  int div;
  div = gcd(f.num,f.den);

  //Make a space to temporarily remove negative.
  int n;
  if(div < 0)
  {
      n = div;
      div = div * -1;
  }

  f.num = f.num / div;
  f.den = f.den / div;
}
Topic archived. No new replies allowed.