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

1. ## 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 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("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)
{
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);

}

{
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;

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. Hi Skeletoon, by complex numbers you mean numbers with real and imaginary parts?

3. 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.

4. 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. 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.

6. yeah im extremely new to this c programming stuff and my teacher expects me to know everything all at once =P thanks pplz

7. lol i changed the function prototypes to float from int and it works =P