473,387 Members | 1,574 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,387 software developers and data experts.

Fraction problem in C

xpun
39
Hi all. im kinda new to c and I could use some help to point me in the right direction. basically I have to perform basic math between fractions and put out a result in fraction form.

right now i am just trying to get the add and multiply functions to work . this is what i have so far..

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. long ans=0;
  13.  
  14. int add(int n1,int d1,int n2,int d2) 
  15. {
  16.  int hold1 = 0;
  17.  int hold2 = 0;
  18.  char  symhold = '/';   
  19.    hold1=((n1 * d2) + (n2 * d1)); 
  20.    hold2=(d1 * d2); 
  21. printf ("%d / %d\n"),hold1,hold2;   
  22. return (hold1);
  23. return (hold2);
  24.  
  25. }
  26. int mult(int n1,int d1,int n2,int d2) 
  27. {
  28.   int hold1 = 0;
  29.   int hold2 = 0;
  30.  char  symhold = '/';   
  31.    hold1=(n1 * n2); 
  32.    hold2=(d1 * d2); 
  33. printf ("%d / %d\n"),hold1,hold2;   
  34. return (hold1);
  35. return (hold2); 
  36. }
  37. int main(void)
  38. {
  39.     char   ra = 'y';
  40.  while (ra != 'n')   
  41.  {   
  42.  
  43.  printf(" Eter the symbol ");
  44.    scanf ("%c",&symbol);  
  45.   printf("Enter The 1st Numerator  ");
  46.     scanf("%d",&num1);
  47.   printf("Enter the 1st Denomanator  ");
  48.     scanf("%d",&denum1);
  49.   printf("Enter the 2nd Numerator  ");
  50.    scanf("%d",&num2); 
  51.   printf("Enter the 2nd Denomanator  ");
  52.     scanf("%d",&denum2); 
  53.  
  54.  
  55.     if (symbol = '+')
  56.     {
  57.     ans= add(num1,denum1,num2,denum2);
  58.  //    printf(" %d\n",ans);
  59.      }
  60.     if (symbol = '*')
  61.     {
  62.     ans= mult(num1,denum1,num2,denum2);
  63.  
  64.   //  printf(" %d\n",ans);
  65.      }
  66. getchar();
  67. printf("would you like to use the fraction operator again? Y or N\n");
  68. ra = getchar();
  69. getchar();
  70. }
  71. }
  72.  
any help is appreciated ...
Apr 3 '07 #1
16 2588
JosAH
11,448 Expert 8TB
You seem to be using C++ and I'd at least expected a Fraction class then.
Besides that, the following:
Expand|Select|Wrap|Line Numbers
  1. return hold1;
  2. return hold2;
... doesn't make any sense: a function can only return once, i.e it can't say:
"oh, yeah, before I forget, take this too".

kind regards,

Jos
Apr 3 '07 #2
xpun
39
You seem to be using C++ and I'd at least expected a Fraction class then.
Besides that, the following:
Expand|Select|Wrap|Line Numbers
  1. return hold1;
  2. return hold2;
... doesn't make any sense: a function can only return once, i.e it can't say:
"oh, yeah, before I forget, take this too".

kind regards,

Jos
hmm ok im writing in c by the way i don't know c++ yet
Apr 3 '07 #3
JosAH
11,448 Expert 8TB
hmm ok im writing in c by the way i don't know c++ yet
Ok, fair enough, but then you shouldn't include the iostream header file and you
should define a little struct that can represent a fraction. Something like this:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _fraction {
  2.    int n; /* the numerator */
  3.    int d; /* the denominator */
  4. } fraction_t;
kind regards,

Jos
Apr 3 '07 #4
xpun
39
Ok, fair enough, but then you shouldn't include the iostream header file and you
should define a little struct that can represent a fraction. Something like this:
Expand|Select|Wrap|Line Numbers
  1. typedef struct _fraction {
  2.    int n; /* the numerator */
  3.    int d; /* the denominator */
  4. } fraction_t;
kind regards,

Jos

here in lies another problem we haven't been taught structures yet lol I know nothing of them yet . .. although thats not stopping me from figuring them out lol
Apr 3 '07 #5
JosAH
11,448 Expert 8TB
here in lies another problem we haven't been taught structures yet lol I know nothing of them yet . .. although thats not stopping me from figuring them out lol
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d;
  5.  
  6.    return m;
  7. }
good luck and

kind regards,

Jos
Apr 3 '07 #6
xpun
39
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d;
  5.  
  6.    return m;
  7. }
good luck and

kind regards,

Jos
cool thanks alot
Apr 3 '07 #7
sicarie
4,677 Expert Mod 4TB
(THREAD HIJACK ALERT!)

Jos-

Ever consider writing one of the tutorials? That was a good, concise explanation of a struct that was pretty easy to understand.
Apr 3 '07 #8
xpun
39
Good, that's the spirit; I like your positive attitude. The code snippet from my
previous reply defined an alias 'fraction_t' for two ints 'n' and 'd'. The fraction_t
almost works as if it was a simple thing such as a double or char etc.

This implies that I can say this:
Expand|Select|Wrap|Line Numbers
  1. fraction_t frac;
Now I have a variable frac which has a type fraction_t (a name I came up with)
and this frac variable has two parts which can be used as 'frac.n' and 'frac.d'.

I'll do the multiplication of two fractions for you, you do the rest:
Expand|Select|Wrap|Line Numbers
  1. fraction_t mul(fraction_t a, fraction_t b) {
  2.    fraction_t m; /* m= a*b */
  3.    m.n= a.n*b*n;
  4.    m.d= a.d*b.d; 
  5.    return m;
  6. }
good luck and

kind regards,

Jos

ok now i keep getting problems saying that printf is undefined
Apr 3 '07 #9
sicarie
4,677 Expert Mod 4TB
ok now i keep getting problems saying that printf is undefined
#include "stdio.h"

And I'm having a major brain freeze right now, I can't remember if that's supposed to be in quotes or carats, so try it like that and if that doesn't work, try it with < and > around it.
Apr 3 '07 #10
xpun
39
yeah its <stdio.h>

that fixed the undefined printf but i have some other syntax issues

i am guessing i dont have the structure in the right place.. this code is getting sloppier by the second lol

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. long ans=0;
  13.  
  14. int add(int n1,int d1,int n2,int d2) 
  15. {
  16.  int hold1 = 0;
  17.  int hold2 = 0;
  18.  char  symhold = '/';   
  19.    hold1=((n1 * d2) + (n2 * d1)); 
  20.    hold2=(d1 * d2); 
  21. //printf ("%d / %d\n"),hold1,hold2;   
  22. //return (hold1,hold2);
  23. }
  24.  
  25.  
  26. int main(void)
  27. {
  28.   {
  29. typedef struct _fraction {
  30.    int n; /* the numerator */
  31.    int d; /* the denominator */
  32. } fraction_t;  
  33.  char   ra = 'y';
  34.  while (ra != 'n')   
  35.  {   
  36.  
  37.  
  38.  printf(" Eter the symbol ");
  39.    scanf ("%c",&symbol);  
  40.   printf("Enter The 1st Numerator  ");
  41.     scanf("%d",&num1);
  42.   printf("Enter the 1st Denomanator  ");
  43.     scanf("%d",&denum1);
  44.   printf("Enter the 2nd Numerator  ");
  45.    scanf("%d",&num2); 
  46.   printf("Enter the 2nd Denomanator  ");
  47.     scanf("%d",&denum2); 
  48.  
  49.  
  50.     if (symbol = '+')
  51.     {
  52.     ans= add(num1,denum1,num2,denum2);
  53.  
  54.  //    printf(" %d\n",ans);
  55.      }
  56.     if (symbol = '*')
  57.     {
  58.    fraction_t mul(fraction_t a, fraction_t b) {
  59.    fraction_t m; /* m= a*b */
  60.    m.n= a.n*b*n;
  61.    m.d= a.d*b.d;     
  62.  
  63.    return m; 
  64. }
  65.     }
  66.  
  67.   //  printf(" %d\n",ans);
  68.      }
  69. getchar();
  70. printf("would you like to use the fraction operator again? Y or N\n");
  71. ra = getchar();
  72. getchar();
  73. }
  74. }
  75.  
Apr 3 '07 #11
sicarie
4,677 Expert Mod 4TB
Yeah, a struct is supposed to be declared outside the main(), where you would declare a function or global, so right above 'add' would be my recommendation.
Apr 3 '07 #12
xpun
39
ok cool but what the heck is this talking about ..
no match for 'operator*' in 'a._fraction::n * b'
Apr 3 '07 #13
sicarie
4,677 Expert Mod 4TB
ok cool but what the heck is this talking about ..
no match for 'operator*' in 'a._fraction::n * b'
I'm pretty sure that's saying that the compiler doesn't know how to do this operation:

m.n= a.n*b*n;
m.d= a.d*b.d;

You need to either 'overload' the * operator, or access those variables differently.
Apr 3 '07 #14
JosAH
11,448 Expert 8TB
i am guessing i dont have the structure in the right place.. this code is getting sloppier by the second lol
Yep, all I can say is that your totally right. You're just trying to copy and paste
code where it doesn't belong at all. And you even dare to feed that mess to
your poor compiler; tsk tsk. ;-)

C compilers use "one pass" parsers which basically means that you need to
declare something 100% before you can use it.

Typedefs (that mumbo jumbo dealing with the fraction_t) need to be defined
at the top level, i.e. outside any function and before any function uses it.

Reorganize your code first before you blindly feed it to your poor compiler.

I did that multiplication function for you; try to get that to work before you start
adding other sophisticated stuff to your program; and be absolutely sure that
you compile C code instead of C++ code.

kind regards,

Jos
Apr 3 '07 #15
JosAH
11,448 Expert 8TB
(THREAD HIJACK ALERT!)

Jos-

Ever consider writing one of the tutorials? That was a good, concise explanation of a struct that was pretty easy to understand.
Maybe somewhere in the near future I might when I can find the time. I'll do some
of the Java tips of the week first and then we'll see whether people like it or if
they get sick and tired of my babbling ;-)

In the mean time it's difficult enough not to let posters go astray with their own
code.

Thanks for the compliment and

kind regards,

Jos
Apr 3 '07 #16
xpun
39
Maybe somewhere in the near future I might when I can find the time. I'll do some
of the Java tips of the week first and then we'll see whether people like it or if
they get sick and tired of my babbling ;-)

In the mean time it's difficult enough not to let posters go astray with their own
code.

Thanks for the compliment and

kind regards,

Jos
I was talking to my professor and he told me not to screw around with structures just yet so i did this instead . The program works i just need to figure out a way to reduce the resulting fraction.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. char symbol;
  6. int num1=0;
  7. int num2=0;
  8. int denum1=0;
  9. int denum2=0;
  10. int ansnum=0;
  11. int denumans=0;
  12. int ans=0;
  13.  
  14.  
  15. int add(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  16. {
  17.  int fnum = 0;
  18.  int fden = 0;
  19.  char  symhold = '/'; 
  20.  if (d1==d2)
  21.       { 
  22.       fnum=(n1+n2);
  23.       fden=(d2);
  24.       *top=fnum;
  25.       *bottom=fden;
  26.       }
  27.  else{       
  28.    fnum=((n1 * d2) + (n2 * d1)); 
  29.    fden=(d1 * d2); 
  30.  *top=fnum;
  31.  *bottom=fden;
  32.  }
  33. }
  34.  int sub(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  35.  {
  36.  int fnum = 0;
  37.  int fden = 0;
  38.  char  symhold = '/';
  39.  if (d1==d2)
  40.       { 
  41.       fnum=(n1-n2);
  42.       fden=(d2);
  43.       *top=fnum;
  44.       *bottom=fden;
  45.       }
  46.  else{     
  47.    fnum=((n1 * d2)-(n2 * d1)); 
  48.    fden=(d1*d2) ; 
  49.  *top=fnum;
  50.  *bottom=fden;
  51. }
  52.  }
  53. int mul(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  54. {
  55.  int fnum = 0;
  56.  int fden = 0;
  57.  char  symhold = '/';   
  58.    fnum=(n1 * n2); 
  59.    fden=(d1 * d2); 
  60.  *top=fnum;
  61.  *bottom=fden;
  62. }
  63. int div(int n1,int d1,int n2,int d2, int*top,int*bottom) 
  64. {
  65.  int fnum = 0;
  66.  int fden = 0;
  67.  int nh ;
  68.  int dh ;
  69.  char  symhold = '/';   
  70.    nh=d2;
  71.    dh=n2;
  72.     fnum=(n1 * nh); 
  73.    fden=(d1 * dh); 
  74.  *top=fnum;
  75.  *bottom=fden;
  76. }
  77. int main(void)
  78. {
  79. int numerator;
  80. int denomonator;
  81.  char   ra = 'y';
  82.  while (ra != 'n')   
  83.  {   
  84.  
  85.  
  86.  printf(" Eter the symbol ");
  87.    scanf ("%c",&symbol);  
  88.   printf("Enter The 1st Numerator  ");
  89.     scanf("%d",&num1);
  90.   printf("Enter the 1st Denomanator  ");
  91.     scanf("%d",&denum1);
  92.   printf("Enter the 2nd Numerator  ");
  93.    scanf("%d",&num2); 
  94.   printf("Enter the 2nd Denomanator  ");
  95.     scanf("%d",&denum2); 
  96.  
  97.  
  98.     if (symbol == '+')
  99.     {
  100.     ans = add(num1,denum1,num2,denum2,&numerator,&denomonator);
  101.     printf(" %d/%d\n",numerator,denomonator);
  102.      }
  103.     if (symbol == '-')
  104.     {
  105.     ans = sub(num1,denum1,num2,denum2,&numerator,&denomonator);
  106.     printf(" %d/%d\n",numerator,denomonator);
  107.      }
  108.    if (symbol == '*')
  109.     {
  110.     ans = mul(num1,denum1,num2,denum2,&numerator,&denomonator);
  111.     printf(" %d/%d\n",numerator,denomonator);
  112.     }
  113.     if (symbol == '/')
  114.     {
  115.     ans = div(num1,denum1,num2,denum2,&numerator,&denomonator);
  116.     printf(" %d/%d\n",numerator,denomonator);
  117.     }
  118. getchar();
  119. printf("would you like to use the fraction operator again? Y or N\n");
  120. ra = getchar();
  121. getchar();
  122. }
  123. }
Apr 3 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
9
by: arun.hallan | last post by:
I need to derive fractions from decimals as so: 0.3333 = 1/3 0.6666 = 2/3 The decimal could also be 0.33, or 0.3333333 but the point is that it that the fraction needs to be extracted. ...
0
by: amarok | last post by:
Hello all. I'm a Software Engineering student, and I'm attempting to write a program in Java that does as follows: UML for the class: Fraction() Fraction(numerator: int) ...
6
evilmonkey
by: evilmonkey | last post by:
I am very new to programming as well as Java and this is my first post so please forgive me if this is not quite posted correctly. My Problem is that I have only been using scanner to get user input...
7
by: d0ugg | last post by:
Hi, Iam writing a code for my class and I cant figure out how to solve it. I will be glad if somebody can teach me how to do something to correct it. The program is divided it 3 parts. ...
1
by: d0ugg | last post by:
Hi, I'm did a fraction program for one of my programming classes and it did compile, however when I'm running the program it crashes for some reason that I do not know. // fraction.cpp ...
2
by: d0ugg | last post by:
Hi, I'm doing a FRACTION program for one of my Programming classes and I'm getting some errors that I can't figure it out. Here is the Assignment: 1. Convert the fraction structure into a...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
4
by: Semajthewise | last post by:
Hi All For those of you that helped me with the questions on this... Thank you! I believe I have solved all the bugs in the code and wanted to post the final product. This is a set of code to solve...
3
by: Myxamatosis | last post by:
we're given an implementation file to base our class of Fraction off of. Input and output are in the format x/y, with x and y being ints, with the "/" character in the middle. so to create an...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.