LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2019, 06:59 PM   #1
anon033
Member
 
Registered: Mar 2019
Posts: 188

Rep: Reputation: 13
Talking K&R Exercise 1-4: Help Checking My Math


I am working through K&R as extra practice for my classes. I am on exercise 1-4 (converting Celsius to Fahrenheit). I believe my code should be solid, but I just need some help checking my math. My code is this:

Code:
#include <stdio.h>

/* printf Celsius-Fahrenheit table */

int main()
{
        float celsius, fahr;
        short lower, upper, step;

        lower = -17;
        upper = 148;
        step = 20;

        celsius = lower;
        printf("Celsius to Fahrenheit Table\n");

        while (celsius <= upper) {
                fahr = (9.0/5.0) * (celsius+32.0);
                celsius = celsius + step;
                printf("%3.0f %6.1f\n", celsius, fahr);
        }
}
which gives this output:

Code:
Celsius to Fahrenheit Table
  3   27.0
 23   63.0
 43   99.0
 63  135.0
 83  171.0
103  207.0
123  243.0
143  279.0
163  315.0
I am confident my code (in the sense of the actual C code) is right on track, however I would love it if someone could check my math. Thank you in advance!

# Solution:

Code:
#include <stdio.h>

/* printf Celsius-Fahrenheit table */

int main()
{
        float celsius, fahr;
        double lower, upper, step;

        lower = -17.78;
        upper = 148.889;
        step = 20;

        celsius = lower;
        printf("Celsius to Fahrenheit Table\n");

        while (celsius <= upper) {
                fahr = ((celsius) * (9.0/5.0)) + 32.0;
                printf("%3.2f %6.1f\n", celsius, fahr);
                celsius = celsius + step;
        }
}
The first issue is found here:

Code:
fahr = (9.0/5.0) * (celsius+32.0);
this is the wrong equation. The correct equation is
Code:
fahr = ((celsius) * (9.0/5.0)) + 32.0;
Then the issue is caused the line

Code:
celsius = celsius + step;
being executed before the print. You will then have the issue of the first number being 18 and thus the whole thing looks wrong and werid. To fix this change
Code:
printf("%3.0f %6.1f\n", celsius, fahr);
to
Code:
printf("%3.2f %6.1f\n", celsius, fahr);
Problem solved

Code:
Celsius to Fahrenheit Table
-17.78   -0.0
2.22   36.0
22.22   72.0
42.22  108.0
62.22  144.0
82.22  180.0
102.22  216.0
122.22  252.0
142.22  288.0
The resulting code should be as follows:

Code:
#include <stdio.h>

/* printf Celsius-Fahrenheit table */

int main()
{
        float celsius, fahr;
        double lower, upper, step;

        lower = -17.78;
        upper = 148.889;
        step = 20;

        celsius = lower;
        printf("Celsius to Fahrenheit Table\n");

        while (celsius <= upper) {
                fahr = ((celsius) * (9.0/5.0)) + 32.0;
                printf("%3.2f %6.1f\n", celsius, fahr);
                celsius = celsius + step;
        }
}

Last edited by anon033; 05-22-2019 at 09:26 PM.
 
Old 05-22-2019, 07:19 PM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
If you search for celsius to fahrenheit on DuckDuckGo, it shows a conversion box which states that 3C is 37.4F and 23C is 73.4F so something is wrong.

Your output (left-hand side) is rushing ahead of itself and upper is also not being respected. Check your code - the first line should have -17 in the first column.

Also, giving an answer as 135.0 gives the impression that the .0 is calculated with that level of precision. 27.0, for example, should be either 27 or e.g. 27.4 (it could of course be 27.0 but all of the results won't be y.0).

T(°F) = (T(°C) × 9/5) + 32

Using the above equation, work out why your formula is incorrect.

I'll leave the C technicalities to other people.

Last edited by hydrurga; 05-22-2019 at 07:20 PM.
 
Old 05-22-2019, 07:29 PM   #3
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Unhappy

I did some more searching and tried the following code:

Code:
#include <stdio.h>

/* printf Celsius-Fahrenheit table */

int main()
{
        float celsius, fahr;
        double lower, upper, step;

        lower = -17.78;
        upper = 148.889;
        step = 20;

        celsius = lower;
        printf("Celsius to Fahrenheit Table\n");

        while (celsius <= upper) {
                fahr = (9.0/5.0) * (celsius+32.0);
                celsius = celsius + step;
                printf("%3.0f %6.1f\n", celsius, fahr);
        }
}
however this is wrong as well. I can't seem to figure out exactly what is wrong with my formula... this seems like it should be the correct math to solve this problem. I looked here: https://www.metric-conversions.org/t...fahrenheit.htm even to check my numbers. I am very confused as to why this isn't working...
 
Old 05-22-2019, 07:32 PM   #4
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Spot the difference:

T(°F) = (T(°C) × 9/5) + 32

T(°F) = (T(°C) + 32) × 9/5

Which is correct?
 
1 members found this post helpful.
Old 05-22-2019, 07:32 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
As you asked for someone to check your math, you can easily see that it is not correct.

Water freezes at 0 Celsius and 32 Farenheit, so 3 C cannot correspond to 27 F!

Similarly for boiling, 100 C and 212 F, so 103 C cannot correspond to 207 F!

The problem is in order of operations (i.e., grouping). You are adding 32 to C before scaling to F. It should be the other way around.
 
1 members found this post helpful.
Old 05-22-2019, 07:34 PM   #6
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
I have this:

T(°F) = (T(°C) × 9/5) + 32

eg

fahr = (9.0/5.0) * (celsius+32.0);

I am not sure how this wrong. I clearly can't spot the difference. I need some help understanding how this is wrong.
 
Old 05-22-2019, 07:36 PM   #7
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Quote:
Originally Posted by FOSSilized_Daemon View Post
I have this:

T(°F) = (T(°C) × 9/5) + 32

eg

fahr = (9.0/5.0) * (celsius+32.0);

I am not sure how this wrong. I clearly can't spot the difference. I need some help understanding how this is wrong.
Those two are not the same. What are you multiplying by 9/5 in the first? Right, what are you multiplying 9/5 by in the second?
 
1 members found this post helpful.
Old 05-22-2019, 07:40 PM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by FOSSilized_Daemon View Post
I have this:

T(°F) = (T(°C) × 9/5) + 32

eg

fahr = (9.0/5.0) * (celsius+32.0);

I am not sure how this wrong. I clearly can't spot the difference. I need some help understanding how this is wrong.
No, you have this...

T(°F) = 9/5 * (T(°C) + 32))

but you need this...

T(°F) = (T(°C) × 9/5) + 32

Remember, operations inside parenthesis are performed first!
 
1 members found this post helpful.
Old 05-22-2019, 07:40 PM   #9
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
I am making the changes I believe I am supposed to make based on the advice given. My code is as follows:

Code:
#include <stdio.h>

/* printf Celsius-Fahrenheit table */

int main()
{
        float celsius, fahr;
        double lower, upper, step;

        lower = -17.78;
        upper = 148.889;
        step = 20;

        celsius = lower;
        printf("Celsius to Fahrenheit Table\n");

        while (celsius <= upper) {
                fahr = ((celsius) * (9.0/5.0)) + 32.0;
                celsius = celsius + step;
                printf("%3.0f %6.1f\n", celsius, fahr);
        }
}
however the output is still wrong:

Code:
Celsius to Fahrenheit Table
  2   -0.0
 22   36.0
 42   72.0
 62  108.0
 82  144.0
102  180.0
122  216.0
142  252.0
162  288.0
I am confused about what is causing the output to be wrong. If I am correct this should give the desired output, I don't understand why the output is as it is.

Last edited by anon033; 05-22-2019 at 07:44 PM.
 
Old 05-22-2019, 07:43 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
You are changing the celsius value before printing the output of the calculation. Another kind of order of operations error!
 
1 members found this post helpful.
Old 05-22-2019, 07:45 PM   #11
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
With the greatest of respect, you're not carefully reading the answers you have been given. You've got the equation right now but both astrogeek and I have told you why column 1 is incorrect.

For a start, can you please return lower and upper to -17 and 148 so that the analysis of your results is easier.
 
1 members found this post helpful.
Old 05-22-2019, 07:47 PM   #12
anon033
Member
 
Registered: Mar 2019
Posts: 188

Original Poster
Rep: Reputation: 13
Aha! I am almost there!

Code:
Celsius to Fahrenheit Table
-18   -0.0
  2   36.0
 22   72.0
 42  108.0
 62  144.0
 82  180.0
102  216.0
122  252.0
142  288.0
I stepped to soon haha. My numbers appear to be off, I am not wanting the answer; however what is the cause? Is it operational, data type or wrong numbers?
 
Old 05-22-2019, 07:51 PM   #13
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
Incorrect display of results in both columns due to excessive rounding.

Edit: a hint: -17.78 is not -18.

Make it easier on yourself to verify your data and read the second part of post #11.

Last edited by hydrurga; 05-22-2019 at 07:54 PM.
 
Old 05-22-2019, 07:58 PM   #14
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
On quick inspection those look pretty good to me.

2 C should be about 35.6 F (2 * 1.8 + 32).

102 C should be about 215.6 F (212 + (2 * 1.8)).

So your math looks right now, but the precision of your results as printed looks to be wrong.

Last edited by astrogeek; 05-22-2019 at 08:04 PM.
 
Old 05-22-2019, 07:58 PM   #15
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I see you've marked the thread as "Solved". Any chance you could post your final code and output to help others in the future?
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Math::Bezier returns Math::Bizarre output captainentropy Programming 3 10-09-2013 09:00 PM
C (math.h)not doing right math? exp() issue. knockout_artist Programming 7 11-25-2011 02:13 PM
Running a 2.6.* kernel with math emulation ( Does the math emulation work ?) dar_beh_dar Linux - Kernel 3 05-20-2009 11:43 PM
math program that I can enter math functions ... Four General 5 04-19-2006 08:02 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration