Search code examples
cmathalgebra

C Bug: Algebraic Expression Giving More Than Expected Output, Counter Checked With A Calculator


I was given a task by the University to evaluate the following exponential formulas using C:

1.  (a + b)2 = a2 + 2ab + b2      
2.  (a - b)2 = a2 - 2ab + b2         
3.  (a + b)3 = a3 + 3a2b + 3ab2 + b3          
4.  (a - b)3 = a3 - 3a2b + 3ab2 - b3          
5.  a2 - b2 = (a - b) (a + b)          
6.  a3 - b3 = (a - b)3 + 3 a b (a - b)      
7.  a3 - b3 = (a - b) (a2 + a b + b2)         
8.  a3 + b3 = (a + b) (a2 - a b + b2)      
9.  a3 + b3 = (a + b)3 - 3 a b (a + b)   
    

And here's my code, which takes two int values from the user and then calculates and prints them using the above algebraic expressions:


//Lab 1 Task  Putting the values in the math formulas and solving them

#include <stdio.h>
#include <math.h>

int main (void)
{
    int a;
    int b;

    printf("\nEnter The First Integer : ");
    scanf("%i" , &a);

    printf("\nEnter The Second Integer : ");
    scanf("%i" , &b);

    double f1 = (a * a) + 2 * (a * b) + (b * b);

    double f2 = (a * a) - 2 * (a * b) + (b * b);

    double f3 = pow(a,3) + 3 * pow(a,2) * b + 3 * a * pow(b,3) + pow(b,3);

    double f4 = pow(a , 3) - 3 * (pow(a , 2) * b) + 3 * (pow((a * b) , 2) - pow(b , 3));

    double f5 = (a - b) * (a + b);

    double f6 = pow((a - b) , 3) + 3 * ((a * b) * (a - b));

    double f7 = (a - b) * (pow(a , 2) + (a * b) + pow(b , 2));

    double f8 = (a + b) * (pow(a , 2) - (a * b) + pow(b , 2));

    double f9 = pow((a + b) , 3) - 3 * ((a * b) * (a + b));


    printf("\n1.    (%i + %i) ^ 2 = %.0lf\n\n" , a , b , f1);

    printf("2.  (%i - %i) ^ 2 = %.0lf\n\n" , a , b , f2);

    printf("3.  (%i + %i) ^ 3 = %.0lf\n\n" , a , b , f3);

    printf("4.  (%i - %i) ^ 3 = %.0lf\n\n" , a , b , f4);

    printf("5.  %i ^ 2 - %i ^ 2 = %.0lf\n\n" , a , b , f5);

    printf("6.  %i ^ 3 - %i ^ 3 = %.0lf\n\n" , a , b , f6);

    printf("7.  %i ^ 3 - %i ^ 3 = %.0lf\n\n" , a , b , f7);

    printf("8.  %i ^ 3 + %i ^ 3 = %.0lf\n\n" , a , b , f8);

    printf("9.  %i ^ 3 + %i ^ 3 = %.0lf\n\n" , a , b , f9);
}

It works fine except for the 3rd and 4th where it gives much-more-than-expected output. What am I doing wrong?

Here's the output being received with the expected and faulty output mentioned:

~/FinalLabProjects/ $ ./formulas <--- I run the program

Enter The First Integer : 7 

Enter The Second Integer : 5

1.      (7 + 5) ^ 2 = 144   <----fine

2.      (7 - 5) ^ 2 = 4     <----fine

3.      (7 + 5) ^ 3 = 3828  <----expected 1728

4.      (7 - 5) ^ 3 = 2908  <----expected 8

5.      7 ^ 2 - 5 ^ 2 = 24  <----fine

6.      7 ^ 3 - 5 ^ 3 = 218 <----fine

7.      7 ^ 3 - 5 ^ 3 = 218 <----fine

8.      7 ^ 3 + 5 ^ 3 = 468 <----fine

9.      7 ^ 3 + 5 ^ 3 = 468 <----fine

Solution

  • You wrote:

     double f3 = pow(a,3) + 3 * pow(a,2) * b + 3 * a * pow(b,3) + pow(b,3);
    

    which equals

    f3 = a^3 + 3ba^2 + 3ab^3 + b^3
    

    But it has to be:

    f3 = a^3 + 3ba^2 + 3ab^2 + b^3
    

    so change this line to:

    double f3 = pow(a,3) + 3 * pow(a,2) * b + 3 * a * pow(b,2) + pow(b,3);
    

    And also

    double f4 = pow(a , 3) - 3 * (pow(a , 2) * b) + 3 * (pow((a * b) , 2) - pow(b,3));
    

    equals:

    f4 = a^3 - 3(ba^2) + 3[(ab)^2 - b^3]
    

    This is wrong. Instead:

    f4 = a^3 - 3ba^2 + 3ab^2 - b^3
    

    so:

     double f4 = pow(a , 3) - 3 * (pow(a , 2) * b) + 3 * a * pow(b , 2) - pow(b,3);
    

    Lastly, both bases and exponents are int numbers. In fact, pow() function is of form:

    double pow( double base, double exponent );
    

    You may want to use round() with pow() function. Or better write your own function to compute your expressions because you will work with relatively small integers. (I guess)