Write a function that will find a temperature....

AI Thread Summary
The discussion revolves around coding a function to find the temperature at which Celsius and Fahrenheit are equal. The original code attempts to use a for loop but contains logical errors, such as incorrect loop conditions and misuse of the modulo operator. Participants suggest that the user should not take input and instead focus on solving the equations directly. They emphasize the importance of understanding the problem and using appropriate programming constructs, such as while loops, for better efficiency. Overall, the conversation highlights common beginner mistakes and encourages a more analytical approach to coding.
elly_55
Poster has been reminded to use the Homework Help Template when starting threads in the schoolwork forums
Hi everybody,

I seem to be having trouble coding a function that returns the integer value at which degrees celsius is equivalent to degrees fahrenheit. Here is the question:

Write a function that will find a temperature that is the same in both Fahrenheit and Celsius. First define two variables farTemp and celTemp and initialize celTemp to 100. Using a loop decrement (or decrease) the celTemp by 1 and check if the calculated farTemp is equal to the celTemp. Stop the loop when farTemp=celTemp

Here is the code that I have so far,
C:
int cent, fahr;
cout << "input your temperature in degrees celsius \n";
cin >> cent;

CentEqualF (int cent)

{

for (cent = 100; cent == -273; cent --)  // is the parameter here a valid statement (cent == -273)?
    fahr = ( (cent*1.8) +32 )
    if fahr % cent == 1
    cout << cent << "is the temperature at which both measurement systems are equivalent \n";
    else 
[I]

         // this is where I get really stuck I know that if my 'if' statement is true then i want the computer
        // to output the temperature. And if the above 'if' statement is false I would like the loop
        // to iterate until the 'if' statement is true but I do not know what to do in order to fix this.
        // I have found several other websites where people ask essentially the same question
        // but I find myself doing more copying than understanding. Thanks for the help![/I]

}
Specifically I seem to be having problems understanding how to code the boolean statement within my for loop, but if you see any problems or simplifications that can be made please share as I am brand new to coding and would love to learn from anyone more experienced!
 
Last edited by a moderator:
Physics news on Phys.org
the for loop takes in a parameter, checks if a condition is met, and if so it keeps looping until the condition is no longer met

So in your case it would set c to 100 and then check if c is equal to -273. that is not the case, so the loop exits immediately.
you want to check if c is greater than or equal (>=) to -273 deg.

the modulo operator (%) returns the remainder of a division. so if we divide 17 by 4 for example, 17%4 would be 1. So I'm not sure that will give you the result you're after.
 
  • Like
Likes elly_55
You are attacking the problem all wrong. You should not be taking any input from a user, you should be solving two simultaneous equations, one for C and one for F. They are both linear equations and where they cross is the answer. It is, by the way, just coincidentally, the temperature at which alcohol freezes, so amusingly, If you have the question "Alcohol freezes at T degrees [and of course you have to fill in the right T] --- Is that in degrees C or degrees F?" The answer is "yes".

OOPS ! I just read the rest of the problem and I see that you are required to do it your way instead of the more direct way that I just suggested. o:)

Thinking about it even more, I think this problem sucks because it depends on the fact that the place where the two lines cross happens to be an integer. Were that not the case, your (required) method would never get the right answer.
 
  • Like
Likes elly_55
Marc Rindermann said:
the for loop takes in a parameter, checks if a condition is met, and if so it keeps looping until the condition is no longer met

So in your case it would set c to 100 and then check if c is equal to -273. that is not the case, so the loop exits immediately.
you want to check if c is greater than or equal (>=) to -273 deg.

the modulo operator (%) returns the remainder of a division. so if we divide 17 by 4 for example, 17%4 would be 1. So I'm not sure that will give you the result you're after.
Awesome, thanks for the quick reply I will get on that right away!
 
  • Like
Likes Marc Rindermann
phinds said:
You are attacking the problem all wrong. You should not be taking any input from a user, you should be solving two simultaneous equations, one for C and one for F. They are both linear equations and where they cross is the answer. It is, by the way, just coincidentally, the temperature at which alcohol freezes, so amusingly, If you have the question "Alcohol freezes at T degrees [and of course you have to fill in the right T] --- Is that in degrees C or degrees F?" The answer is "yes".

OOPS ! I just read the rest of the problem and I see that you are required to do it your way instead of the more direct way that I just suggested. o:)

Okay, thank you! I think I have a better understanding of the problem now... I find it funny how I turned off my math knowledge whilst trying to code this problem instead of merging the two worlds, rookie mistake.
 
if you're a beginner programmer I don't see a problem trying to use the tools a programming language provides to solve simple problems. Once you solve the problem using a for loop you might see that a for loop isn't the optimal tool to solve this particular problem since the for loop continues looping after you found your solution for as long as cent is greater than -273 deg unless you break out of the loop. So after you found the solution using the for loop you might want to look at while loops.

As a beginner you want to learn to use the tools first. If you were a carpenter apprentice you wouldn't go to Hollywood and try to build huge sets for the next blockbuster. You would take a hammer, a nail and a piece of timber and try to hammer the nail into the timber without bending it.

Once you are an intermediate programmer you might want to look at the problem first and try to understand it first before you start to write a program.
 
  • Like
Likes elly_55
The algorithm should go something like this:

celTemp = CelTemp - 1
farTemp = celTemp*1.8+32
If farTemp is not equal to celtemp, go back an increment celTemp again

Otherwise, print celTemp
 

Similar threads

Replies
4
Views
2K
Replies
3
Views
2K
Replies
10
Views
3K
Replies
6
Views
3K
Replies
1
Views
2K
Replies
3
Views
1K
Back
Top