Fahrenheit to Celsius function

For my class I need tabular view or celcius to fahrenheit and fahrenheit to celcius as described.... I'm trying to do basic mathfunctions called by main... any pointers on my code so far?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <math.h>
// c)	Use these functions to write a program that prints a chart showing the Fahrenheit equivalent of all Celsius 
//temperature from -100 to 100, and the Celsius equivalent of all Fahrenheit temperature from -40 to 212. print the output 
//in a tabular format that minimizes the amount of line so it is readable. 

int CelsiusToFahrenheit(void);
int fahrenheitToCelcius(void);


int main(void)
	{
		int tempC=-100;
		int tempF=-40;
		int count;

		printf( "Celcius\tFahrenheit\n\n" ); // print column headers

		for ( tempC = -100; tempC <= 100; ++tempC ) //for loop to print the columns of temperatures as long as it's in the celcius range
			{
				printf("\n%d\t%d", tempC, CelsiusToFahrenheit(tempC) );

		}//end for

		printf( "\nFahrenheit\tCelcius\n\n" ); // print column headers
		
		for ( tempF = -40; tempF <= 212; ++tempF ) //for loop to print the columns of temperatures as long as it's in the celcius range
			{
				printf("\n%d\t%d", tempF, fahrenheitToCelcius(tempF) );

			}//end second for
		getchar();
}//end main


int CelsiusToFahrenheit(int FtempC) // Call Function for °C to °F	Multiply by 9, then divide by 5, then add 32
{
	return FtempC * 9 / 5 + 32;
}//end fahrenheit conversion function

int fahrenheitToCelcius(int FtempF) //Call function for °F to °C	Deduct 32, then multiply by 5, then divide by 9
{
	return FtempF - 32 * 5 / 9;

}//end Celsuis Conversions function 
Last edited on
I think my professor wants the two sets of columns to be side by side.... but I'm not sure how to accomplish this, I'm sure i'm missing something simple. ( my cmd window seems to run out of memory and remove the first results)
@mebruler

Side by side
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <math.h>
// c)	Use these functions to write a program that prints a chart showing the Fahrenheit equivalent of all Celsius 
//temperature from -100 to 100, and the Celsius equivalent of all Fahrenheit temperature from -40 to 212. print the output 
//in a tabular format that minimizes the amount of line so it is readable. 

int CelsiusToFahrenheit(int);// Can't use void, as you are sending data
int fahrenheitToCelcius(int);


int main()
	{
		int tempC;  // No need to initialize, as you are using for loop to do it.
		int tempF;
		int count;

		printf( "Celcius\tFahrenheit\t\tFahrenheit\tCelcius\n\n" ); // print column headers

		for ( tempC = -100; tempC <= 100; ++tempC ) //for loop to print the columns of temperatures as long as it's in the celcius range
			{
				printf("\n%d\t%d", tempC, CelsiusToFahrenheit(tempC) );
				tempF = CelsiusToFahrenheit(tempC); //Get converted temp
// Then send to be converted back
				printf("\t\t\t%d\t%d", tempF, fahrenheitToCelcius(tempF) );

		}//end for
		/*
// This section not needed
		printf( "\nFahrenheit\tCelcius\n\n" ); // print column headers
		
		for ( tempF = -40; tempF <= 212; ++tempF ) //for loop to print the columns of temperatures as long as it's in the celcius range
			{
				printf("\n%d\t%d", tempF, fahrenheitToCelcius(tempF) );

			}//end second for*/
		getchar();
return 0;
}//end main


int CelsiusToFahrenheit(int FtempC) // Call Function for °C to °F	Multiply by 9, then divide by 5, then add 32
{
	return ((FtempC * 9) / 5) + 32;
}//end fahrenheit conversion function

int fahrenheitToCelcius(int FtempF) //Call function for °F to °C	Deduct 32, then multiply by 5, then divide by 9
{
	return ((FtempF - 32) * 5) / 9; // Correct way to convert the temp

}//end Celsuis Conversions function  
Last edited on
Thanks whitenite!

but... line 22 states "get" and "temp" are undefined, and this won't run, Also notice in the first comments that it's requiring two different number ranges for two different conversions. the first -100 to 100 C to F the second -40 to 212 F to C

Where my code was running both, but printed too many lines, and could not display everything I need to display.

any ideas?
Last edited on
@mebruler

Sorry, I forgot to add the // before the text of 'Get converted temp'. Also, I thought that the two sets of temps were actually the same values for Celsius and Fahrenheit. Didn't check to much, else I would have noticed that they weren't.

Anyway, here's a way to get them side by side.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <math.h>
// c)	Use these functions to write a program that prints a chart showing the Fahrenheit equivalent of all Celsius 
//temperature from -100 to 100, and the Celsius equivalent of all Fahrenheit temperature from -40 to 212. print the output 
//in a tabular format that minimizes the amount of line so it is readable. 

int CelsiusToFahrenheit(int);
int fahrenheitToCelcius(int);


int main(void)
	{
		int tempC=-100;
		int tempF=-40;
		int count;

		printf( "Celcius\tFahrenheit\t\tFahrenheit\tCelcius\n\n" ); // print column headers

			do
			{
        				if(tempC<=100)
					{
						printf("\n%d\t%d", tempC, CelsiusToFahrenheit(tempC) );
				tempC+=1;
				}
				else
					printf("\n\t");
				printf("\t\t\t%d\t\t%d", tempF, fahrenheitToCelcius(tempF) );
				tempF+=1;
		} while(tempF<=212);//end while
		
		getchar();
}//end main


int CelsiusToFahrenheit(int FtempC) // Call Function for °C to °F	Multiply by 9, then divide by 5, then add 32
{
	return ((FtempC * 9) / 5) + 32;
}//end fahrenheit conversion function

int fahrenheitToCelcius(int FtempF) //Call function for °F to °C	Deduct 32, then multiply by 5, then divide by 9
{
	return ((FtempF - 32) * 5) / 9;

}//end Celsuis Conversions function  


Of course, your results are not the actual values, since your using ints, and the temperature values get rounded up or down. But I guess, they are close enough.
Last edited on
Thank you again :)

I know I could probably use float numbers here with 1-2 decimal places, but it's not specified that I need to in my homework. I did try to use float in my code, by swapping basically all my int for "floats" I also tried "double"s but my result's ended up not even giving me remotely the same answer... If I wanted to add one decimal place using floats, it wouldn't be that difficult right?


Thanks again for the help, you rock!
Last edited on
@mebruler

I see you used a while statement and ended without {} Can this be done differently so the while loop is first, and a second if is contained within the while?

Actually, I believe that is already how it is shown.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
do // do/while loop first
{
       	if(tempC<=100) // if is contained inside do/while
	{
		printf("\n%d\t%d", tempC, CelsiusToFahrenheit(tempC) );
                // This is printed to screen IF tempC <= 100
		tempC+=1; increase tempC by one
	 }
	else
	    printf("\n\t"); // Otherwise JUST print a newline and tab to keep columns aligned
        
        printf("\t\t\t%d\t\t%d", tempF, fahrenheitToCelcius(tempF) );
	tempF+=1; // Increase tempF by one
} while(tempF<=212);//end while 


Also for some reason, the math isn't occuring properly in the second conversion chart.


It's not converting right, because of the int rounding. And, yes, adding one decimal place would not be that hard to do.

Also, you could do a for loop for the Fahrenheit to Celsius conversion, since it's the longest, and put a if statement to print out the Celsius to Fahrenheit only when the Celsius is less than, or equal to, 100.
Last edited on
I updated my username to be a little less personal, can you update your last post to reflect my username? Also I removed the post's you pasted in here, so I'm not sure how you got them, my only question at this point (which I'll play around a little with my code) is, am I right in saying, if I just make these float numbers that should give me my correct answers... I know there's a way to limit to one decimal using the floor function, but I think there is also a way to limit it with something similar to the %1 , but I don't quite remember how to do that.

Any pointers?
@mebruler

I don't believe you can delete my posts, only yours. Anyway, here's how to show the floats, with one decimal place.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <math.h>
// c)	Use these functions to write a program that prints a chart showing the Fahrenheit equivalent of all Celsius 
//temperature from -100 to 100, and the Celsius equivalent of all Fahrenheit temperature from -40 to 212. print the output 
//in a tabular format that minimizes the amount of line so it is readable. 

float CelsiusToFahrenheit(float);
float fahrenheitToCelsius(float);


int main()
{
	float tempC = -100;
	float tempF;
	
	printf( "  Fahrenheit\tCelsius\t\tCelsius\tFahrenheit\n\n" ); // print column headers

	for ( tempF = -40; tempF <= 212; ++tempF ) //for loop to print the columns of temperatures as long as it's in the celcius range
	{
		printf("\n\t%-2.0f\t%-2.1f", tempF, fahrenheitToCelsius(tempF));
		if(tempC <= 100)
		{
			printf("\t\t%-2.0f\t%-2.1f", tempC, CelsiusToFahrenheit(tempC) );
			tempC+=1;
		}
	} //end for
	getchar();
	return 0;
}//end main


float CelsiusToFahrenheit(float FtempC) // Call Function for °C to °F	Multiply by 9, then divide by 5, then add 32
{
	return ((FtempC * 9) / 5) + 32;
}//end fahrenheit conversion function

float fahrenheitToCelsius(float FtempF) //Call function for °F to °C	Deduct 32, then multiply by 5, then divide by 9
{
	return ((FtempF - 32) * 5) / 9;

}//end CelsiUs Conversions function 
perfect, thank you :) that's what I tried before with the floats, but I left out the %-2.1f and just did %f, which gave me the default 6 decimals. why does it need to be 2.1? I don't remember the exact reasons, but would like to understand every piece of code, I think that's the only piece I'm a little fuzzy on.

thank you again :)
@mebruler

It doesn't NEED to be 2.1 The '-' sign first tells it to add a - if the number is negative. Actually, it'll still show the negative sign if you leave it off. The .1, is to show one decimal place. A .2 would show 2 decimal places, etc. I used a .1 since a .2 always showed a 0 in the second place, as in, .40, so I used the .1 for only the .4.
Last edited on
makes sense, but where does the 2 come into play? "-2.1"
- = show negative
.1 = one decimal place
2 = ?
Actually, you can leave that off. You get the same results if it's a 2, 4 or any number. I was just used to writing it, I used it without thinking. Try it. Use just .1, or even 5.1, and they are the same. So, for now, leave it off if you wish. There may be use for it but for now, I'm not sure when.
Topic archived. No new replies allowed.