# Thread: Fahrenheit to Celsius converter

1. ## Fahrenheit to Celsius converter

Hello everybody I am relatively new to C programming, and at the very start I have encountered probably a simple problem to solve, but yet I'm stuck over it for a good few days...

I was going through tutorial questions that I got from college, and one of the questions requires writing up a code, that converts Fahrenheit scale to Celsius scale.

The relation between temperature in ◦ C and ◦F is given by the formula:
◦C = 5/9 . ( ◦F - 32 )

Write a program that prints a table (just two columns without any borders) with temperature in ◦F and ◦C for temperatures between 0 and 300 ◦F in steps of 20◦. Compile and run your program.
I wanted to approach this problem via arrays and for loops, and I wrote up this

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

int main()
{ // begin main()

// units
double[] celsius = new double[ 16 ];
double[] fahrenheit = new double[ 16 ];

// variables
int i, j, k;

// for loop to fill in cells of fahrenheit array
for( i = 0; i < 15; i++)
{ // begin for()
fahrenheit[ i ] = i * 20;
} // end for()

// for loop to convert from fahrenheit to celsius
for( j = 0; j < 15; j++)
{ // begin for()
celsius[ j ] = 5/9 * (fahrenheit[ j ] - 32);
} // end for()

printf("Fahrenheit   Celsius\n" );

// for loop to display results
for( k = 0; k < 15; k++ )
{ // begin for()
printf( "%2f             %2f\n", fahrenheit[ k ], celsius[ k ] );
} // end for()

return EXIT_SUCCESS;
} // end main()```
Now when I'm trying to compile that, the compailer throws an error which makes absolutely no sense to me.

Code:
```fahrenheitCelsius.c: In function ‘main’:
fahrenheitCelsius.c:18:9: error: expected identifier or ‘(’ before ‘[’ token
double[] celsius = new double[ 16 ];
^
fahrenheitCelsius.c:19:9: error: expected identifier or ‘(’ before ‘[’ token
double[] fahrenheit = new double[ 16 ];
^
fahrenheitCelsius.c:27:7: error: ‘fahrenheit’ undeclared (first use in this function)
fahrenheit[ i ] = i * 20;
^
fahrenheitCelsius.c:27:7: note: each undeclared identifier is reported only once for each function it appears in
fahrenheitCelsius.c:33:7: error: ‘celsius’ undeclared (first use in this function)
celsius[ j ] = 5/9 * (fahrenheit[ j ] - 32);
^```
Can anyone tell me what is going on there? I wrote the same program in Java and it worked just fine. Thanks  2. The new operator doesn't exist in C, it's a C++ or Java thing. You need to decide which language you're using.

If you're wanting to write C, find a textbook or basic C tutorial (e.g. here) and read the section on arrays. Notice how arrays are declared in C, and compare that to your current code. Just because the syntax is similar, doesn't mean you can put Java/C++ into a C compiler and have it just work. 3. Why are you even using dynamic memory? Why not just create the arrays statically?
Code:
`double celsius;`
And why the magic number 16? Your loops are only setup for a size of 15.

Also watch out for integer division, there are no fractions in integer division. You need to insure your constants are floating point constants not integral constants.

Jim 4. The new operator doesn't exist in C, it's a C++ or Java thing. You need to decide which language you're using.

If you're wanting to write C, find a textbook or basic C tutorial (e.g. here) and read the section on arrays. Notice how arrays are declared in C, and compare that to your current code. Just because the syntax is similar, doesn't mean you can put Java/C++ into a C compiler and have it just work.
Thanks, I'll have a good look at the tutorials. I used the new operator because I mixed up C and C# tutorials online...

And why the magic number 16? Your loops are only setup for a size of 15.
Because when I coded the program in Java and made an array of length 15, my Fahrenheit values went only up to 280, so I extended the array by an extra cell, but I forgot to change the loop set up as well, thanks for heads up  5. Originally Posted by Muzgi Thanks, I'll have a good look at the tutorials. I used the new operator because I mixed up C and C# tutorials online...
Try to avoid learning multiple languages at once, it will more likely confuse you and make you a poor programmer of many languages, instead of making you a decent programmer in at least one language. Originally Posted by Muzgi Because when I coded the program in Java and made an array of length 15, my Fahrenheit values went only up to 280, so I extended the array by an extra cell, but I forgot to change the loop set up as well, thanks for heads up This is a perfect example of why you should not use magic numbers...EVER! Use a constant with a sensible name, and use it everywhere you need to reference that value. Then, if you want to change the size of an array, all your loops will still work correctly. Like so:
Code:
```#define NUM_TEMPS 16
double celsius[NUM_TEMPS];

for (i = 0; i < NUM_TEMPS; i++) {
celsius[i] = ...;
}``` 6. Originally Posted by anduril462 Try to avoid learning multiple languages at once, it will more likely confuse you and make you a poor programmer of many languages, instead of making you a decent programmer in at least one language.

This is a perfect example of why you should not use magic numbers...EVER! Use a constant with a sensible name, and use it everywhere you need to reference that value. Then, if you want to change the size of an array, all your loops will still work correctly. Like so:
Code:
```#define NUM_TEMPS 16
double celsius[NUM_TEMPS];

for (i = 0; i < NUM_TEMPS; i++) {
celsius[i] = ...;
}```
Thanks for the advice. It actually makes it neater too. Well anyway, I got my program working, so thanks a lot for your help and time I'm posting the code in case anyone wants to do a similar thing

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

int main( void )
{ // begin main()

// constants
int KEY_LENGTH = 16;
float constant = 0.5556;

// units
float celsius[ KEY_LENGTH ];
float fahrenheit[ KEY_LENGTH ];

// variables
int i, j, k;

// for loop to fill in cells of array
for( i = 0; i < KEY_LENGTH; i++)
{ // begin for()
fahrenheit[ i ] = i * 20;
} // end for()

// for loop to convert from fahrenheit to celsius
for( j = 0; j < KEY_LENGTH; j++)
{ // begin for()
celsius[ j ] = constant * (fahrenheit[ j ] - 32);
} // end for()

printf("Fahrenheit   Celsius\n" );

// for loop to display results
for( k = 0; k < KEY_LENGTH; k++ )
{ // begin for{}
printf( "%.2f         %.2f\n", fahrenheit[ k ], celsius[ k ] );
} // end for()

return EXIT_SUCCESS;
} // end main()``` 7. FWIW, arrays are not required at all for this program, there is no requirement I see that your values need to be stored anywhere for any purpose - unless there is something I'm missing. A single/simple loop with a print statement and some calculations in the loop body would suffice.
Code:
```const int START_FAHRENHEIT = 0;
const int STOP_FAHRENHEIT = 300;
const int STEP_SIZE = 20;

...

for( i = START_FAHRENHEIT; i <= STOP_FAHRENHEIT; i += STEP_SIZE )
{
// Print fahrenheit/celsius values directly, no need for an array to store values
}```
...does "between 0° and 300°" mean inclusive [0,300] or exclusive (0,300)? 8. Muzgi, I had this problem in my first textbook! Ahh, memories...  Popular pages Recent additions 