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

In summary, the conversation is about a coding problem where the goal is to find the temperature at which degrees Celsius is equal to degrees Fahrenheit. The initial approach of using a for loop with a decrementing counter and a boolean statement is questioned, and an alternative solution using two simultaneous equations is suggested. The use of a modulo operator is also discussed. The idea of using different tools, such as a while loop, is proposed for learning purposes.
  • #1
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
  • #2
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
  • #3
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
  • #4
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
  • #5
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.
 
  • #6
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
  • #7
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
 

1. What is the purpose of this function?

The purpose of this function is to find a temperature in a given location and return the result in a specified unit of measurement.

2. What are the parameters of this function?

The parameters of this function include the location, unit of measurement, and any additional information needed for the function to accurately find the temperature.

3. How does this function find the temperature?

This function uses a combination of data from reliable sources, such as weather databases or APIs, and mathematical calculations to find the temperature in the given location.

4. Can this function handle different unit conversions?

Yes, this function can handle different unit conversions based on the specified unit of measurement parameter. It can convert temperatures from Celsius to Fahrenheit or vice versa.

5. Is this function accurate?

While this function uses reliable data and calculations, its accuracy may vary depending on the quality and timeliness of the data sources. It is always recommended to double-check the results with other sources for the most accurate temperature reading.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Sticky
  • Engineering and Comp Sci Homework Help
Replies
1
Views
13K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top