6 Replies - 312 Views - Last Post: 20 March 2013 - 03:19 PM Rate Topic: -----

#1 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:25 PM

#include <iostream>
using namespace std;

int main()
{  int tempF;
   int tempC = 100;
   char answer;
  
do  
{   
 for (tempF = 0; tempF != tempC; tempC--)
         { 
		 
	   tempF = (9 * tempC) / 5;
	   tempF = tempF + 32;
           cout << tempF;
           cout << endl;
           
           
         }
   
    cout << endl;
    cout << endl;
    cout << "Would you like to see the calculation again?\n";  
    cin  >> answer;  
}  while(answer == 'y' || answer == 'Y');
  return 0;	
}    


Okay, I basically have to make a program that finds the Fahrenheit temp that equals the same in Celsius, so -40. My program stops at -43. We have to use type int and when I changed 32 to 30, it stopped at -40 (but that's not the correct formula. Can anyone see what I did wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Program about Farenheit/Celisus (off by 2)

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:54 PM

Part of your problem is that you are doing integer math. Remember that integers have no fractions. So things like 1/2 evaluate to zero. You really should be using floating point numbers for these calculations.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 01:58 PM

I getcha. My teacher made it very clear that anything after a decimal point or whatever basically vanishes/is ignored. The word problem insists that we use type int though and that the results might not be super accurate. Is there anything I did that prevents it from being -40 or is -43 technically the right output for this situation?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 02:19 PM

What exactly is the assignment? If you do the math with floating point you end up with -43.6.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 02:31 PM

"Write a program that finds the temperature that is both the same in Fahrenheit and Celsius. Use the formula: (9 * Celsius) / 5 + 32. Your program should use two integer variables for the temperatures Fahrenheit and Celsius. Initialize the value of Celsius to 100. In a loop, decrement the value of Celsius and compute the corresponding Fahrenheit value until the two values are the same. Note: Since you are using integer values, the formula may not give you an exact result. This will not affect the solution to this particular problem."

So might -43 be the correct answer for this problem? It outputs numbers from 212 all the way down to -43.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 03:00 PM

Well the answer should actually be -40. I found this to work properly:

   do
   {
	   tempF = (9 * tempC) / 5;
	   tempF = tempF + 32;
      cout << tempF << endl;
   }while(tempF != tempC--);



Jim
Was This Post Helpful? 0
  • +
  • -

#7 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Re: Program about Farenheit/Celisus (off by 2)

Posted 20 March 2013 - 03:19 PM

Thanks, that worked for me too! I just got rid of the extra tempC in my for loop and also go -40.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1