Thread: adding fractions

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Question adding fractions

    im having trouble not gettin correct output, it give me 0.

    Code:
    void addFrac(Frac a,Frac b,Frac c,Frac e)
    {
    int sum;
    	sum=(a.n*b.d)+(b.n*a.d);
    	sum+=(c.n*e.d)+(e.n*c.d);
    //printing sums
    	printf("\n%.2f is the total\n",sum);
    }
    Last edited by SledMan2002; 11-25-2002 at 06:17 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you trying to do here? You realize that you can't just add fractions and expect it to give you a non-fraction (ie: whole) number.

    Your addition is incorrect. Do you know how to add fractions?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    I was over at his house earlier (trying to figure this junk out). We decided to see if we could spit out a decimal that worked correctly. I'm not sure if how he was adding the fractions (cross multipying) was working or not. Right now for the decimal output we're receiving zero. I'm not sure what's wrong, but i know it has to do with how we're adding the fractions...we'll get fraction output later, for now decimal's fine. Thanks.
    PHP and XML
    Let's talk about SAX

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Angry adding Fractions and bad output!

    i think i have the adding function messed up. C what u can do with it for me.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    typedef struct fraction{
    	int n;
    	int d;
    }Frac;
    
    
    void getFrac(Frac a,Frac b,Frac c,Frac e);
    void addFrac(Frac a,Frac b,Frac c,Frac e);
    
    /////////////////////////////////MAIN/////////////////////////////////////////////////
    void main()
    {
    	Frac a={0,1},b={0,1},c={0,1},e={0,1},sum={0,1};
    	getFrac(a,b,c,e);
    	addFrac(a,b,c,e);
    
    }
    
    
    /////////////////////////////////FUNCTIONS////////////////////////////////////////////
    
    void getFrac(Frac a,Frac b,Frac c,Frac e)
    {
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&a.n,&a.d);
    	while(a.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&a.n,&a.d);
    	}
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&b.n,&b.d);
    	while(b.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&b.n,&b.d);
    	}
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&c.n,&c.d);
    	while(c.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&c.n,&c.d);
    	}
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&e.n,&e.d);
    	while(e.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&e.n,&e.d);
    	}
    
    //printing inputed
    	printf("Input Reads...\n");
    	printf("\n%d/%d\n",a.n,a.d);
    	printf("%d/%d\n",b.n,b.d);
    	printf("%d/%d\n",c.n,c.d);
    	printf("%d/%d\n\n",e.n,e.d);
    }
    
    
    void addFrac(Frac a,Frac b,Frac c,Frac e)
    {
    int sum;
    	sum=(a.n*b.d)+(b.n*a.d);
    	sum+=(c.n*e.d)+(e.n*c.d);
    //printing sums
    	printf("\n%.2f is the total\n",sum);
    }
    Last edited by SledMan2002; 11-25-2002 at 06:19 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should make a simple function first. Make one that adds two fractions:
    Code:
    int sum1, sum2;
    sum1 = fraction1.d;
    sum2 = fraction2.d;
    
    fraction1.n *= sum2;
    fraction1.d *= sum2;
    fraction2.n *= sum1;
    fraction2.d *= sum1;
    fraction1.n += fraction2.n;
    fraction1.d += fraction2.d;
    That should get you started.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    ah, quzah, you mean you did the code starting in the main function??? and then try to split it up to other function? is coding programming better and easlier?

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    well?

    what can u do with it then, i think itz a syntax error in it. let me know what u get.
    Dave

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491

    Re: well?

    Originally posted by SledMan2002
    what can u do with it then, i think itz a syntax error in it. let me know what u get.
    If it's a syntax error then you should be able to figure it out pretty easily. That is, just try to compile it.

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Still Need Help

    i have been tryin to figure out how to make the addFrac function come up with a common denominator, how can i make this give me the right answer in fraction and decimal form.
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    typedef struct fraction{
    	int n;
    	int d;
    }Frac;
    
    
    void getFrac(Frac a,Frac b,Frac c,Frac e);
    void addFrac(Frac a,Frac b,Frac c,Frac e);
    
    /////////////////////////////////MAIN/////////////////////////////////////////////////
    void main()
    {
    	Frac a={0,1},b={0,1},c={0,1},e={0,1},sum={0,1};
    	getFrac(a,b,c,e);
    	addFrac(a,b,c,e);
    
    }
    
    
    /////////////////////////////////FUNCTIONS////////////////////////////////////////////
    
    void getFrac(Frac a,Frac b,Frac c,Frac e)
    {
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&a.n,&a.d);
    	while(a.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&a.n,&a.d);
    	}
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&b.n,&b.d);
    	while(b.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&b.n,&b.d);
    	}
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&c.n,&c.d);
    	while(c.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&c.n,&c.d);
    	}
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&e.n,&e.d);
    	while(e.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&e.n,&e.d);
    	}
    
    //printing inputed
    	printf("Input Reads...\n");
    	printf("\n%d/%d\n",a.n,a.d);
    	printf("%d/%d\n",b.n,b.d);
    	printf("%d/%d\n",c.n,c.d);
    	printf("%d/%d\n\n",e.n,e.d);
    }
    
    
    void addFrac(Frac a,Frac b,Frac c,Frac e)
    {
    
    	int sum1,sum2,sum3,sum4;
    sum1 = a.d;
    sum2 = b.d;
    //a+b
    a.n *= sum2; //multiplies the numerator of a to the denomiator of b
    a.d *= sum2; //denom of a to denom of b
    b.n *= sum1; //first only reversed
    b.d *= sum1; //second reversed
    a.n += b.n; //add the numerators now that they're compatible
    a.d += b.d; //add denominators now that they're compatible
    //printing
    printf("\nSum for a and b: %d/%d\n",a.d,b.d);  //this works....but the problem is, 1 and 1/2 will come out as 2/1....not 1.5 over 1....spit out decimal next
    printf("Decimal: %d\n",(a.d/b.d));
    //c+d
    sum3 = c.d;
    sum4 = e.d;
    c.n *= sum4; //multiplies the numerator of c to the denomiator of e
    c.d *= sum4; //denom of c to denom of e
    e.n *= sum3; //first only reversed
    e.d *= sum3; //second reversed
    c.n += e.n; //add the numerators now that they're compatible
    c.d += e.d; //add denominators now that they're compatible
    //printing
    printf("\nSum for c and e: %d/%d\n",c.d,e.d);//this works....but the problem is, 1 and 1/2 will come out as 2/1....not 1.5 over 1....spit out decimal next
    printf("Decimal: %d\n",(c.d/e.d));
    
    //total
    printf("Sum Total is: %d\n",(a.d/b.d)+(c.d/e.d));
    
    }
    Thanks for all your help!
    Dave

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My original math was incorrect. This is what happens when you talk in the phone and code at the same time. Vola!
    Code:
    struct Frac addTwo( struct Frac a, struct Frac b )
    {
    	struct Frac x;
    	a.n *= b.d;
    	b.n *= a.d;
    	
    	x.n = a.n + b.n;
    	x.d = a.d * b.d;
    
    	return x;
    }
    Now just take this and use it to add up all your values.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Still wont work!

    it is not getting numbers in the addFrac().
    it gives me 0/1 as answers on everything. y? Heres what i came up with b4 u sent this. It should work, but itz not gettin the values to work with it.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    typedef struct fraction{
    	int n;
    	int d;
    }Frac;
    
    
    void getFrac(Frac a,Frac b,Frac c,Frac e);
    void addFrac(Frac a,Frac b,Frac c,Frac e);
    
    /////////////////////////////////MAIN/////////////////////////////////////////////////
    void main()
    {
    	Frac a={0,1},b={0,1},c={0,1},e={0,1};
    	getFrac(a,b,c,e);
    	addFrac(a,b,c,e);
    
    }
    
    
    /////////////////////////////////FUNCTIONS////////////////////////////////////////////
    
    void getFrac(Frac a,Frac b,Frac c,Frac e)
    {
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&a.n,&a.d);
    	while(a.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&a.n,&a.d);
    	}
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&b.n,&b.d);
    	while(b.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&b.n,&b.d);
    	}
    
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&c.n,&c.d);
    	while(c.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&c.n,&c.d);
    	}
    
    	printf("Enter Fraction\n");
    	scanf("%d%*c%d",&e.n,&e.d);
    	while(e.d==0){
    		printf("Enter Fraction Again\n");
    		scanf("%d%*c%d",&e.n,&e.d);
    	}
    
    
    //printing inputed
    	printf("Input Reads...\n");
    	printf("\n%d/%d\n",a.n,a.d);
    	printf("%d/%d\n",b.n,b.d);
    	printf("%d/%d\n",c.n,c.d);
    	printf("%d/%d\n\n",e.n,e.d);
    }
    
    
    
    void addFrac(Frac a,Frac b,Frac c,Frac e)
    {
    int sum1,sum2,numer,denom,sum3,sum4,numer2,denom2;
    sum1=a.d*b.n;
    sum2=a.n*b.d;
    numer=sum1+sum2;
    denom=a.d*b.d;
    printf("\na+b= %d/%d",numer,denom);
    
    sum3=c.d*e.n;
    sum4=c.n*e.d;
    numer2=sum3+sum4;
    denom2=c.d*e.d;
    
    printf("\nc+e= %d/%d\n",numer2,denom2);
    
    
    }
    c what u can come up with, i would appreciate it if you could work with the code i have provided. Im jus trying to learn how itz working. Thanks

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @SledMan2002
    Your latest code doesn't work because you are passing not populating the structs you created in main() with the data you are getting in getFrac(). Look:

    Code:
    ... in main() ...
    Frac a={0,1},b={0,1},c={0,1},e={0,1};
    getFrac(a,b,c,e);
    addFrac(a,b,c,e);
    You are passing the structs by value, meaning both getFrac and addFrac have their own copies of the fraction structs. Whatever changes they make to them do not affect those you created in main. To fix, you need to pass by reference (ie a pointer).

    To see exactly what I mean, add this to the addFrac() function:
    >printf("\n%d/%d\n",a.n,a.d);

    And don't use void main. Use int main(void) and return 0; at the end.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    ok, i got that

    i got that 2 work, but i still cant get it to reduce to simplest terms.
    Dave

  14. #14
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    I came up with smoething for him this morning to fix it. And it was due today too so the mods can close this thread.
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  3. Java: Sorting and Adding a Row in Table
    By alphaoide in forum Tech Board
    Replies: 4
    Last Post: 08-12-2005, 08:22 PM
  4. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 04:33 AM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM