Fefe Rich
Fefe Rich

Reputation: 11

Unexpected values for basic math function

I am learning the basics for programming in C++ and I need to write a program that performs some basic math functions using if/else statements: subtraction, addition, multiplication ect. I don't get any compile or run time errors, but the values that I end up with are always incorrect.

I changed some things around trying to see where I went wrong but what is printed on the screen as far as words go are correct just not the answers. For instance when I scan a - (subtraction sign) as input it will read the difference between x and y is some ridiculously high number.

Anyway, here is my code & any help would be GREATLY appreciated!

 /*
 * This program performs some basic math calculations
 */

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

int main() {
    int x,y;
    float sum = x+ y;
    float difference = x-y;
    float product = x*y;
    float quotient = x/y;
    float exponent = x^y;
    float modulus = x%y;
    char symbol;


    printf("What is the value of x?\n");
    scanf("%d", &x);

    printf("What is the value of y?\n");
    scanf("%d", &y);

    printf("What is your operator?\n");
    scanf(" %c", &symbol);

    if (symbol== '+') 
       printf("The sum of x and y = %f\n", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %f\n", difference);
    else if (symbol=='*')
       printf("The product of x and y = %f\n", product);
    else if (symbol=='/')
       printf("x divided by y = %f\n", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %f\n", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%f\n", modulus);

    return 0;
}

Upvotes: 1

Views: 1006

Answers (2)

dirkgently
dirkgently

Reputation: 111120

You are performing ALL of the calculations even before you've initialized x and y. Uninitialized variables have indeterminate contents and thus any result you have are indeterminate. You are running the risk of invoking Undefined Behavior with some of your statements (which means that it is impossible to say what your code will do with any amount of certainty). Do your operations only after you've read in the values of x and y.

#include <math.h>

Why do you have this include if you don't use any functions from <math.h>?

float sum = x+ y;

The sum (or difference) of two integers will always be an integer. Why are you using a float here?

float quotient = x/y;

This does integer division and the result is converted to a float. So, the results may not be what you intend. Try to convert any one of the arguments to a float/double in order to get the correct result. Also, you should always check for division by zero. Same goes for your modulus calculation.

float exponent = x^y;

There is no exponentiation operator in C. You need to use a library function (such as pow) or roll your own.

scanf(" %c", &symbol);

Why do you have the space in front?

There is also no need for this huge if-else ladder. Try using a switch.

You need to pick up a good book and start reading to understand how C works.

Upvotes: 1

Kamran Amini
Kamran Amini

Reputation: 1062

Sine this is a C++ application you can define variables as you want. But keep in mind that a program written in C or C++ and for all other languages will be executed top to bottom so when you declare x and y they has no value read from the user. Always trace your codes from top to bottom. Here I declare x and y and read their values from the user first. Then I calculate their different operations. I think most of them except division can be integers again like x and y but I tries to correct your code.

If you want to have exponentiation in C or C++ you have to write your own code.

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

    float power( int x , int y )
    {
        float ret = 1 ;
        for( int i = 1 ; i <= y ; i++ )
            ret *= x ;
        return ret ;
    }

    int main() {

    int x,y;

    printf("What is the value of x?\n");
    scanf("%d", &x);

    printf("What is the value of y?\n");
    scanf("%d", &y);

    printf("What is your operator?\n");
    scanf(" %c", &symbol);


    float sum = (float)x+ y;
    float difference = (float)x-y;
    float product = (float)x*y;
    float quotient = (float)x/y;
    //float exponent = (float)x^y; //Exponentiation should be implemented using multiplication and loops.

   float exponent = power( x , y ) ;
    float modulus = (float)x%y;
    char symbol;

    if (symbol== '+') 
       printf("The sum of x and y = %f\n", sum);
    else if (symbol=='-')
       printf("The difference between x and y = %f\n", difference);
    else if (symbol=='*')
       printf("The product of x and y = %f\n", product);
    else if (symbol=='/')
       printf("x divided by y = %f\n", quotient);
    else if (symbol=='^')
       printf("x multiplied exponentially by y = %f\n", exponent);
    else if (symbol=='%')
       printf("x is this much percent of y =%f\n", modulus);

    return 0;
    }

Upvotes: 2

Related Questions