Farenheight and Celsius (Negative Ints?)

I'm having trouble figuring out what's not right with my program. The code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream.h>

int x=-80;
int y=0; //cannot equal zero due to the for loop
int i=0;


void main()
{
	cout << "x" << " " << "i" << " " << "y" << endl;

	do
	{
		i=x;				//x must be constant for incrementation
		cout << x << " " << i << " " << y << endl; //debug
		i=((5/9)(i-32));//formula for finding a celsius temperature from a farenheight one
		x++;	//increment x
		cout << x << " " << i << " " << y <<endl; //debug
		cout << endl; //debug
	}while(y!=x);

	cout << "The values of Celsius and Farenheight are equal at: " << y << " degrees." << endl;
}


It is supposed to find the value at which celsius and farenheight are equal, which I know is -40 but cannot hardcode into the program. I thought this would work, but every time I run it, my debug output statements show that i is being set to 0 after the equation to find the celsius temperature has concluded. When I took out the 5/9 part, it began changing values. Why is this? I've tried making the variables all floats, but it doesn't change anything.
closed account (o1vk4iN6)
You can't use math syntax here, (x)(y) does not mean (x) * (y). Also 5/9 is 0, you need to use floating point precision if you want decimals.

This problem can be approached using:


F(x) = x
C(x) = (5/9) * (x - 32)

F(x) = C(x)

x = 5/9 * (x - 32)

x / (x - 32) = 5/9

1 + (32 / (x - 32)) = 5/9

32 / (x - 32) = 5 / 9 - 1

x - 32 = -72

x = -40


Which makes your program that uses trial and error a bit inefficient and not very accurate.
Last edited on
What do you mean by floating point precision? Do you mean I must use float variables?
closed account (o1vk4iN6)
Yes you must use float or double variables, as well for constants, if you have an int divide by an int, that's an int.
1
2
3
4
5

float i = 5 / 9; // i = 0

float i = 5.f / 9; // i = 0.5555556f


at least one of the two variables in an operation involving variables or constants must be a float.

The 'f' means that it is single precision and should be used when you want floats.
1
2
3
float i = 5.f;

double j = 5.;

Last edited on
If you don't want to use floating points, just do the multiplication first:

1
2
3
4
int boilingF = 212;

int boilingC = (boilingF - 32) * (5 / 9);  // bad, is zero
boilingC = (boilingF - 32) * 5 / 9;  // OK, multiplication is first, so not zero. 
Okay, I understand. Thank you!
Topic archived. No new replies allowed.