Thread: program that prints two tables showing...

1. program that prints two tables showing...

I don't have much experience in programming and i want to know if anyone can show how this can be done.

Implement the following two functions:

/* returns Celsius equivalent of a Fahrenheit temperature (ft) */
double celsius(double ft);

/* returns Fahrenheit equivalent of a Celsius temperature (ct) */
double fahrenheit (double ct);

Use these functions to write a program that prints two tables showing:

(a) the Fahrenheit equivalent of all Celsius temperatures from 0 to 100 degrees with increment 2.
(b) the Celsius equivalent of all Fahrenheit temperatures from 32 to 212 degrees with increment 4.

Code:
```#include <stdio.h>
#include <stdlib.h>

int
main ( void )
{
int i, celsius, farenheit,

printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");

for ( i = 0; i <= 100; i += 2 )
{
farenheit = ((9.0 / 5.0) * i) + 32;
celsius = (5.0 / 9.0) * (i - 32.0);

printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\ i, celsius, i, farenheit );

}
return EXIT_SUCCESS;```

2. I would think you'd need more variables and to use spaces instead of tabs. My approach to this would likely be having a base position for each, starting at your starting point. In each loop's round, add the required increment then put your converted number in the table. One method for lining things up is to use something like %12d which forces 12 characters to be reserved for the number display adding extra spaces as needed (use whatever is needed for nice alignments).

I also see another problem - you are using integers when you are otherwise working with fractional values. Your math won't work out correctly every time. For this, use floats or doubles (floats should be adequate here).

3. Within your forloop just call your two functions for the farenheit and celsius. I see you still have to create those. Then either add the stepping you need or set a static variable to increment.

Then your printf statement will only need to call the functions with the stepping in place:

Code:
```
printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\ i, celsius() + celStep, i, farenheit() + ftStep );```

Is your real issue with how to create the function calls for these?

4. Sorry, I may have read to quick on your use of the stepping you need. I see you are are after to print the farenheit every 2 degrees, then display the celsius after every 4 degrees. You will then need to align your table up to match the appropriate F-degrees with the C-degrees then.

5. Originally Posted by slingerland3g
Sorry, I may have read to quick on your use of the stepping you need. I see you are are after to print the farenheit every 2 degrees, then display the celsius after every 4 degrees. You will then need to align your table up to match the appropriate F-degrees with the C-degrees then.
A friend helped me and we managed to do this but i don't know if it show what it should. because the windows closes. Can you all compile it and run if it opens and shows the right numbers please.

Code:
```#include <stdio.h>
#include <stdlib.h>

int
main ( void )
{
int i;
float cel, farh;

printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");

for ( i = 0; i <= 100; i += 2 )
{
farh = ((9.0 / 5.0) * i) + 32;
cel = (5.0 / 9.0) * (i - 32.0);
printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\
i, cel, i, farh );
}
return 0;
}```

6. I think you should use the system("pause") function, but this isn't familiar territory for me. By the looks of your code, you are only getting steps of 2 for both, instead of 2 and 4 separately. Use another variable besides just "i" for this.

7. how can i make the steps of 2 and 4 separately?

8. Originally Posted by ulillillia
I think you should use the system("pause") function, but this isn't familiar territory for me. By the looks of your code, you are only getting steps of 2 for both, instead of 2 and 4 separately. Use another variable besides just "i" for this.
how can i make the steps of 2 and 4 separately?

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
main ( void )
{
int i, k;
double Celsius, Fahrenheit;

printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");

for ( i = 0; i <= 100; i += 2)

{
Fahrenheit = ((9.0 / 5.0) * i) + 32;
Celsius = (5.0 / 9.0) * (i - 32.0);
printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\
i, Celsius, i, Fahrenheit);
}
system("pause");

}```

9. You forgot to change your printf statement. You're mentioning i twice in it. Fix that and you should be good. As a strong suggestion, use proper tabbing - it makes code much easier to read.

Edit: In addition, you also forgot to change your for statement to using both i and k, as you call them. I often use more meaningful names like "CelsiusBase" and "FahrenheitBase" to distinguish, but you can call them whatever you want.