Jump to content

math question in C program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
s0n1c

s0n1c

    Newbie

  • Members
  • Pip
  • 6 posts
Hey guys;

I am currently creating a program that calculates the estimate for painting a house. The user gets the select the quality of paint that is to be used. There is premium, which is $40 per 400 sq ft, regular - $30 per 400 sq ft, and basic - $20 per 400 sq ft. I have created some equations to calculate the totals which are:

premiumPaint = (((40/400) * surfaceArea) * 2)

regulartPaint = (((30/400) * surfaceArea) * 2)

basicPaint = (((20/400) * surfaceArea) * 2)

the equation is saying that premiumPaint is equal to the ratio times the surfaceArea of the room, times 2 - for 2 coats of paint.

I know that my equation works because when I do it by hand I get the right answer, the problem comes about when I put it in the compiler, the computer gives me a result of '0'. Is this because it can't divide 40/400? Anyways what I did next was put in .1 instead of 40/400, which is the same thing, and now it works fine. Is there a way of using my regular equation?

P.S. I'm running Fedora 8 and using the cc compiler, if that matters.

Thanks in advance;
--
S0n1C!

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You need to use floating numbers, and not plain integers.

#3
s0n1c

s0n1c

    Newbie

  • Members
  • Pip
  • 6 posts
I'm actually using double, and I figured it out, I rewrote me equation;

premiumPaint = ((40 * surfaceArea) / 400) * 2

This gets me the answer that I want, Do you have another way of doing it?

Thanks.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
40/400 = 0 in C/C++
40.0/400 = 0.1 in C/C++
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
s0n1c

s0n1c

    Newbie

  • Members
  • Pip
  • 6 posts
oh, ok, thanks.