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