Multiplying and Dividing Fractions

I need to take an inputed fraction and multiply and divide it by another inputed fraction. Then I have to output both results. I need a multfract function and a divfract funtion.
I've declared my structure type as:
1
2
3
4
5
6
struct fraction
    {
    int num;
    int denom;
    bool positive;
    };


I have my fuctions written as such:
1
2
3
4
5
6
7
8
9
10
11
12
void multiply_frac (fraction *f1, fraction* f2, int * num, int *denom);
	{
	num = f1->num * f2->num;
	denom = f1->enom * f2->denom;
	} 


void div_fract (fraction *f1, fraction *f2, int *num, int *denom);
	{
	num = f1->num * f2->denom;
	denom = f1->denom * f2->num;
	}


As a whole this is my code:
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
#include <iostream>
#include <cmath>

using namespace std;
struct fraction
{
int num;
int denom;
bool positive;
};
void multiply_fract (fraction f1, fraction f2);
void div_fract (fraction f1, fraction f2);
int main()
{

fraction f1;
fraction f2;
fraction fresult;
char tempchar;
bool positive = true;

cout << "Input the first numerator." << endl;
cin >> f1.num;
cout << "Input the first denominator." << endl;
cin >> f1.denom;
cout << "Is the fraction positive? (Y/N)" << endl;
cin >> tempchar;

if (tempchar == 'Y')
{
positive = true;
}

if (tempchar == 'N' )
{
positive = false;
}

cout << "Input the second numerator." << endl;
cin >> f2.num;
cout << "Input the second denominator." << endl;
cin >> f2.denom;
cout << "Is the fraction positive? (Y/N)" << endl;
cin >> tempchar;

if (tempchar == 'Y')
{
positive = true;
}

if (tempchar == 'N' )
{
positive = false;
}

fresult = multiply_fract (f1, f2, num, denom);


cout << "Result is: ";
if(!(fresult.positive))
{
cout << "-";
}

cout << fresult.num << " / " << fresult.denom << endl;
fresult = div_fract (f1, f2, num, denom);
cout << "Result is: ";

if(!(fresult.positive))
{
cout << "-";
}

cout << fresult.num << " / " << fresult.denom << endl;
}

void multiply_frac (fraction *f1, fraction* f2, int * num, int *denom);
	{
	num = f1->num * f2->num;
	denom = f1->enom * f2->denom;
	} 


void div_fract (fraction *f1, fraction *f2, int *num, int *denom);
	{
	num = f1->num * f2->denom;
	denom = f1->denom * f2->num;
	}


It won't build because of the number of errors. Any suggestions?
Submit the first few errors you are getting from the compiler -- most folks won't try to compile your code for you.
Hy tjinx!
Most of your mistakes are typing errors, not that serious:

line 77: void multiply_frac (fraction *f1, fraction* f2, int * num, int *denom);
you put an semicolon; after the function

line80: denom = f1->enom * f2->denom;
what is enom?? you probaly ment denom

line84: void div_fract (fraction *f1, fraction *f2, int *num, int *denom);
you put an semicolon; after the function

line 56:fresult = multiply_fract (f1, f2, num, denom);
fresult is an integer and multiply_fract is a void. You can not assign a void to an integer, only an integer to an integer and so on.The variable and the function are not of the same type.
Not to mention that the function doesnt'e even return anything( because it's a void I guess :) ).Didn't you mean:
1
2
3
fresult.nom = f1.nom * f2.nom;
fresult.denom = f1.denom * f2.denom;
fresult.positive = f1.positive && f2.positive;

This of course eliminates the need of the multiplication function.
I think your function was supposed to be something like this:
1
2
3
4
5
void multiply_frac (fraction *f1, fraction *f2, int * num, int *denom)
{
	*num = f1->num * f2->num;
	*denom = f1->denom * f2->denom;
}

But unfortunetly this is not very helpfull, because the function can't return more than one variable at a time, so it can't return both num and denum.You will say that it doesn't matter because num and denum are passed by pointer, not by value, their change remains even after the function terminates.True.But why did you bother with a structure anyway?No?
You probably meant this:
1
2
3
4
5
void multiply_frac (fraction *f1, fraction *f2, fraction *f3)
{
	f3->num = f1->num * f2->num;
	f3->denom = f1->denom * f2->denom;
}

Much simpler.You put in 3 fractions and the function makes the 3rd fraction be the multiple of the first 2.

This is a version that seems to work:
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
#include <iostream>
#include <cmath>

using namespace std;

struct fraction // the structure
{
  int num;
  int denom;
  bool positive;
};


void multiply_frac (fraction *f1, fraction *f2, fraction *f3) // for multiplying
{
	f3->num = f1->num * f2->num;
	f3->denom = f1->denom * f2->denom;
}

void div_frac (fraction *f1, fraction *f2, fraction *f3) // for dividing
{
	f3->num = f1->num * f2->denom;
	f3->denom = f1->denom * f2->num;
}


int main()
{

  fraction f1;
  fraction f2;
  fraction fresult;
  char tempchar;
  bool positive1 = true;
  bool positive2 = true; // you need 2 positives, for fraction 1 and fraction 2

  cout << "Input the first numerator." << endl;
  cin >> f1.num;
  cout << "Input the first denominator." << endl;
  cin >> f1.denom;
  cout << "Is the fraction positive? (Y/N)" << endl;
  cin >> tempchar;

  if (tempchar == 'Y')
    positive1 = true;
  else
  {
    if (tempchar == 'N' )
      positive1 = false;
    else
    {
      cout<<"You must type Y or N .The program will asume the fraction is positive"<<endl;
      positive1 = true; // makes the fraction positive in case the user types wrong
    }
  }

  cout << "Input the second numerator." << endl;
  cin >> f2.num;
  cout << "Input the second denominator." << endl;
  cin >> f2.denom;
  cout << "Is the fraction positive? (Y/N)" << endl;
  cin >> tempchar;

  if (tempchar == 'Y')
    positive2 = true;
  else
  {
    if (tempchar == 'N' )
      positive2 = false;
    else
    {
      cout<<"You must type Y or N .The program will asume the fraction is positive"<<endl;
      positive2 = true; // makes the fraction positive in case the user types wrong
    }
  }

  multiply_frac(&f1, &f2, &fresult); // the multiplication
  // be carefull! it's &f1, not f1 or f2, etc. , because you pass by pointer

  cout << "Result of multiplication is: ";
  if( !fresult.positive )
  {
    cout << "-";
  }

  cout << fresult.num << " / " << fresult.denom << endl;

  div_frac(&f1, &f2, &fresult); // the division
  cout << "Result of division is: ";

  if(!(fresult.positive))
  {
    cout << "-";
  }

  cout << fresult.num << " / " << fresult.denom << endl;

  return 0;
}

Last edited on
Topic archived. No new replies allowed.