Need help with my code which can't calculate decimals properly

This is a discussion on Need help with my code which can't calculate decimals properly within the C Programming forums, part of the General Programming Boards category; This is my code for an assignment, i can do it myself but for some reason when i type in ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    Unhappy Need help with my code which can't calculate decimals properly

    This is my code for an assignment, i can do it myself but for some reason when i type in decimal numbers for a1, a2, b1, b2 the calculation is done but it doesnt give the right answer, need help, whats wrong with it?
    i also need it not to allow for letters only numbers to be inputted, if anyone could help MUCH appreciated =)
    Code:
    #include <stdio.h>                                                                                            #include <math.h>                                                                                            
    
    int add(void);                                                                                                 
    int sub(void);                                                                                                 
    int multi(void);                                                                                             
    int exit(void);                                                                                                 
    
    int addTwoComplexNumbers1( float a1, float a2 );                                           
    int addTwoComplexNumbers2( float b1, float b2 );                                           
    int subTwoComplexNumbers1( float a1, float a2 );                                           
    int subTwoComplexNumbers2( float b1, float b2 );                                           
    int mulTwoComplexNumbers1( float a1, float a2, float b1, float b2 );                
    int mulTwoComplexNumbers2( float a1, float a2, float b1, float b2 );                                        
    
    int main(void)
    {
        int choice;                                                                                                 
         
        do 
        {
            printf("---------- Main Menu ----------\n");                                                   
            printf("1. Add two complex numbers\n");                                                
            printf("2. Subtract two complex numbers\n");                                      
            printf("3. Multiply two complex numbers\n");                                           
            printf("4. Exit the program\n");                                                               
            printf("   Please select an option (1, 2, 3, or 4):");                                   
            scanf("%d",&choice);                                                                                
     
            switch(choice)                                                                                        
            {
                case 1: add();                                                                                    
                    break;
                case 2: sub();                                                                                    
                    break;
            case 3: multi();                                                                                
            break;
                case 4: printf("\nThe program has exited successfully\n\n");                                                    
                    return;
                    break;
                default: printf("Input error: %d is not a choice!\n\n\n", choice);                                
                    break;
            }
     
         }while (choice);
                                                                                               
    }
     
    int add(void)                                                                                                
    {
            float a1, b1, a2, b2;                                                                                    
        float c, d;                                                                                        
         
            printf("Enter the first complex numbers in the format a b: ");                                            
        scanf("%f %f", &a1, &b1);
                                                                             
        printf("Enter the second complex numbers in the format a b: ");                                            
        scanf("%f %f", &a2, &b2);
                                                                                          
        
        c = a1 + a2;                                                                                            
        d = b1 + b2;                                                                                            
        c = addTwoComplexNumbers1(a1, a2);                                                       
        d = addTwoComplexNumbers2(b1, b2);                                                                        
        
        printf("(%1.1f , %1.1f) + (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);                                    
        
        return 0;
    }    
    
    int sub(void)                                                                                                
    {
        float a1, b1, a2, b2;                                                                                    
        float c, d;                                                                                            
    
        printf("Enter the first complex numbers in the format a b: ");                                            
        scanf("%f %f", &a1, &b1);                                                                                
        
        printf("Enter the first complex numbers in the format a b: ");                                            
        scanf("%f %f", &a2, &b2);                                                                                
        
        c = a1 - a2;                                                                                            
        d = b1 - b2;                                                                                            
        c = subTwoComplexNumbers1(a1, a2);                                                                        
        d = subTwoComplexNumbers2(b1, b2);                                                                        
        
        printf("(%1.1f , %1.1f) - (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);                                    
        
        
        return 0;
    }
    
    int multi(void)                                                                                                
    {
        float a1, b1, a2, b2;                                                                                    
        float c, d;                                                                                                
        printf("Enter the first complex numbers in the format a b: ");                                            
        scanf("%f %f", &a1, &b1);                                                                                
        
        printf("Enter the first complex numbers in the format a b: ");                                            
        scanf("%f %f", &a2, &b2);                                                                                
        
        c = (a1 * a2) - (b1 * b2);                                                                           
        d = (a1 * a2) + (b1 * b2);                                                                           
        c = mulTwoComplexNumbers1(a1, a2, b1, b2);                                           
        d = mulTwoComplexNumbers2(a1, a2, b1, b2);                                                                
        
        printf("(%1.1f , %1.1f) * (%1.1f , %1.1f) = (%1.1f , %1.1f)\n\n", a1, b1, a2, b2, c, d);                                    
        
        return 0;
    }
    
    
    int addTwoComplexNumbers1( float a1, float a2)                                                                
    {
        return(a1+a2);                                                                                            
    }
    
    int addTwoComplexNumbers2( float b1, float b2)                                                                
    {
        return(b1+b2);                                                                                            
    }
    
    
    int subTwoComplexNumbers1( float a1, float a2)                                                                
    {
        return(a1 - a2);                                                                                        
    }
    
    int subTwoComplexNumbers2( float b1, float b2)                                                                
    {
        return(b1 - b2);                                                                                        
    }
    
    
    int mulTwoComplexNumbers1( float a1, float a2, float b1, float b2 )                                            
    {
        return((a1*a2) - (b1*b2));                                                                                
    }
    
    int mulTwoComplexNumbers2( float a1, float a2, float b1, float b2 )                                            
    {
        return((a1*a2) + (b1*b2));                                                                                
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Hi Skeletoon, by complex numbers you mean numbers with real and imaginary parts?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    From looking at this code, I can see that it is not doing anything like what it should be.
    There a several concepts which you don't seem to fully understand:

    Complex numbers. You don't seem to understand that complex numbers have both a real and an imaginary part. Two floats does not constitute two complex numbers.
    Functions. If you understood these, you wouldn't write two function that do the same thing and just have different names.
    Structures. If you understood these, you would probably have used them.
    Floating point maths. I'm going to go out on a limb here and suggest that you probably don't know how floating point numbers and calculations work. They have limited precision and even statements such as 0.1 + 0.9 == 1.0 are false. This is possibly why you are getting wrong results.

    Post the examples you are entering and the results you are getting.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    5
    ok where it says
    printf("Enter the first complex numbers in the format a b: "); for the addition function
    i type 1.5 1.2
    then it prompts me to enter the second complex numbers,
    i type in 1.2 1.4
    it calculates it by adding a1 with a2 and b1 with b2 to produce: c equalling 2.0 and d equalling 2.0 when its meant to be c equals 1.7 and d equals 1.6
    this occurs for the other functions as well, btw iMalc it's meant to simply be addition subtraction and multiplication, my teacher isnt very good at english

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    You are making computation on floats but returning ints from your function, therefore you get int values. Also you should really consider the comments from iMalc.
    HomePort : A C Web Service API for heterogeneous home automation systems

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    5
    yeah im extremely new to this c programming stuff and my teacher expects me to know everything all at once =P thanks pplz

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    5
    lol i changed the function prototypes to float from int and it works =P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed with code to calculate power use
    By dave_the_bear10 in forum C Programming
    Replies: 2
    Last Post: 10-10-2011, 08:26 AM
  2. how to properly comment on c code
    By agentsmith in forum C Programming
    Replies: 15
    Last Post: 01-30-2008, 12:13 PM
  3. Lining up decimals & text - code included inside.
    By INFERNO2K in forum C Programming
    Replies: 7
    Last Post: 11-27-2004, 03:49 PM
  4. what does this code calculate?
    By Guti14 in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 07:39 PM
  5. What's wrong with this code(decimals to fractions)?
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 05-01-2002, 09:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21