1

Im essentially brand new to C, so bear with me. So basically i have to create a program to calculate the amount of dough per sqft. After rechecking my math and debugging for a very long time; ive found that sradius/INCHES_PER_FEET is coming out to 0.

INCHES_PER_FEET = 12. Now the issue is if i input any value equal to or greater than 12 for the radius, the function will work properly; but if i input 8, and 8/12 is less than 1 a decimal, the program will automatically equate it to 0 and im not sure how i can fix that. Insight would be really appreciated.

int main(){
//define radius variables
int sradius;
printf("What is the radius of your small pizza, in inches?\n");
scanf("%d", &sradius);
int mradius;
printf("What is the radius of your medium pizza, in inches?\n");
scanf("%d", &mradius);
int lradius;
printf("What is the radius of your large pizza, in inches?\n");
scanf("%d", &lradius);


//define pizzas sold variables
int spizzas;
printf("How many small pizzas do you expect to sell this week?\n");
scanf("%d", &spizzas);
int mpizzas;
printf("How many medium pizzas do you expect to sell this week?\n");
scanf("%d", &mpizzas);
int lpizzas;
printf("How many large pizzas do you expect to sell this week?\n");
scanf("%d", &lpizzas);

//dough calculation per size
double sdough,mdough,ldough;
sdough = (PI*((sradius/INCHES_PER_FEET)*(sradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
mdough = (PI*((mradius/INCHES_PER_FEET)*(mradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
ldough = (PI*((lradius/INCHES_PER_FEET)*(lradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);

//final amount of dough
double fdough;
fdough = ((sdough*spizzas)+(mdough*mpizzas)+(ldough*lpizzas));

//print statement
printf("you need to order %.3f " ,fdough);

return 0;


}
Hayu
  • 19
  • 1
  • Change the type of `sradius`, `mradius`, etc from `int` to `double` and see what happens. You'll also have to change the conversion strings in your `scanf` calls. Best of luck. – Bob Jarvis - Слава Україні Sep 22 '14 at 01:48
  • possible duplicate of [How to get fractions in an integer division?](http://stackoverflow.com/questions/2976011/how-to-get-fractions-in-an-integer-division) – Chris Hayes Sep 22 '14 at 01:50
  • 1
    `int` is not a generic number type. It's for integers -- whole numbers -- only. – Charles Duffy Sep 22 '14 at 01:52
  • The How to get franctions in an interger division solved my issue, thanks for all of your help guys – Hayu Sep 22 '14 at 02:23
  • This line: sradius/INCHES_PER_FEET is performing an integer divide, and all integer divides round down. so, if sradius were 6, the result would be .5 but this is an integer divide, so round down to integer is 0. – user3629249 Sep 22 '14 at 05:03

3 Answers3

2

Cast all the radius into a double before calculating and you will get a double as the result like this:

double sdough,mdough,ldough;
sdough = (PI*(((double)sradius/INCHES_PER_FEET)*((double)sradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
mdough = (PI*(((double)mradius/INCHES_PER_FEET)*((double)mradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
ldough = (PI*(((double)lradius/INCHES_PER_FEET)*((double)lradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

C typically allows you to do operations on a single "data-type". However, if you try to do such operations, C has some preset defaults. For example, in the following code, C will assign the value of a as 0.0000 and not 0.6666, even though you have declared a as float:

int main(void)
{
    float a;
    int b=2, c=3;
    a=b/c;
    printf("%f",a);
    return 0;
}

This happens because b and c are declared integers. You use type-casting in such cases, i.e., force-changing the declared data-type. The above code will give you desired output if you do the following:

int main(void)
    {
        float a;
        int b=2, c= 3;
        a=(float)b/(float)c;
        printf("%f",a);
        return 0;
    }

The answer answered by @cool-guy depicts the same, in your specific case.

Pranav Totla
  • 2,182
  • 2
  • 20
  • 28
  • It is not a good idea to show someone how to code sloppily in the 25-year old C89 standard (and even that didn't encourage the notation you're using, though it did allow it of necessity). You should be using `int main(void)` with the return type as mandatory — the old C99 standard that's close to 15 years old requires that, let alone the current C11 standard — and the `void` preferred by me (and many others), not least because I compile with GCC and the option `-Wstrict-prototypes` and the empty parentheses aren't a strict prototype. – Jonathan Leffler Sep 22 '14 at 03:05
  • Thanks, but C auto-defaults itself to int main(void) if you just write main(). – Pranav Totla Sep 22 '14 at 03:09
  • Only in C89. C99 requires the explicit `int` return type; so does C11. – Jonathan Leffler Sep 22 '14 at 03:10
0

The excessive use (IMNSHO) of parentheses is forcing the division to be integer. If you had written

x = PI * sradius / INCHES_PER_FEET * sradius / INCHES_PER_FEET * DOUGH_PER_SQFT;

instead of

x = (PI*((sradius/INCHES_PER_FEET)*(sradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);

the divisions would have been done in floating point and you would get a more accurate value. Assigning that float value to an int causes truncation instead of rounding, so the accuracy could be improved by adding a half:

x = PI * sradius / INCHES_PER_FEET * sradius / INCHES_PER_FEET * DOUGH_PER_SQFT + 0.5;

Most compilers will complain about the implicit conversion to int if warning diagnostics are sufficiently enabled.

The other answers are correct in that forcing type conversion with a typecast will also get an accurate result. Or you could simply define the denominator as a float constant:

const float INCHES_PER_FEET = 12.0;

This requires each division to be done in floating point even though the numerator is an int.

wallyk
  • 56,922
  • 16
  • 83
  • 148