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

Click For Summary

Discussion Overview

The discussion revolves around coding a function to find the temperature at which degrees Celsius is equivalent to degrees Fahrenheit. Participants explore various approaches to solving the problem, including the use of loops and equations, while addressing issues in the provided code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant presents an initial attempt at coding a function but expresses confusion about the loop structure and boolean conditions.
  • Another participant points out that the loop condition should check if the temperature is greater than or equal to -273 degrees instead of equal to -273.
  • Some participants suggest that the problem could be approached by solving simultaneous equations instead of using a loop, noting that the intersection point is the solution.
  • There is a discussion about the appropriateness of user input in the function, with some arguing that it complicates the solution unnecessarily.
  • A later reply emphasizes the importance of understanding the problem before coding, suggesting that a for loop may not be the optimal tool for this specific problem.
  • One participant provides a simplified algorithm for the solution, outlining the steps to decrement the Celsius temperature and calculate the corresponding Fahrenheit temperature.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to solving the problem, with some advocating for the use of loops and others suggesting a more mathematical approach. There is no consensus on the optimal method or the necessity of user input.

Contextual Notes

Participants highlight potential limitations in the initial code, including the loop condition and the use of the modulo operator, which may not yield the desired results. The discussion also reflects varying levels of programming experience among participants.

Who May Find This Useful

Beginner programmers interested in understanding loops, conditional statements, and mathematical problem-solving in coding may find this discussion beneficial.

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   Reactions: 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   Reactions: 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   Reactions: 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   Reactions: 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
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • Sticky
  • · Replies 1 ·
Replies
1
Views
17K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 25 ·
Replies
25
Views
15K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
12K
  • · Replies 18 ·
Replies
18
Views
11K