Thread: Fahrenheit to Celsius converter

  1. #1
    Registered User Muzgi's Avatar
    Join Date
    May 2014
    Location
    Ireland
    Posts
    9

    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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.
    Last edited by anduril462; 05-28-2014 at 08:50 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you even using dynamic memory? Why not just create the arrays statically?
    Code:
    double celsius[16];
    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. #4
    Registered User Muzgi's Avatar
    Join Date
    May 2014
    Location
    Ireland
    Posts
    9
    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. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Muzgi View Post
    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.
    Quote Originally Posted by Muzgi View Post
    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. #6
    Registered User Muzgi's Avatar
    Join Date
    May 2014
    Location
    Ireland
    Posts
    9
    Quote Originally Posted by anduril462 View Post
    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. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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)?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Codus Conjectus spongefreddie's Avatar
    Join Date
    Sep 2010
    Location
    USA
    Posts
    86
    Muzgi, I had this problem in my first textbook! Ahh, memories...
    V8 Interceptor: KDE 5.25.5 on Manjaro Linux 22.0.0 "Sikaris"
    Steering wheel: gcc 12.2.0 in Kate
    Supercharger: NASM 2.15.05
    Engine: AMD Ryzen 7 1700
    Dashboard: NVIDIA GeForce GTX 1060 6GB
    Rusty old trailer for hauling 3% of my Steam catalog: Windows 7 Pro 64bit SP1
    3 Antique Ford Model T automobiles for vintage LAN gaming: Windows XP SP3
    Sturdy buckboard for DOS LAN gaming: DOSBox 0.74-3 on Windows XP SP3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Celsius to Fahrenheit, Fahrenheit to celsius.
    By boneyflesh in forum C Programming
    Replies: 7
    Last Post: 12-15-2013, 05:24 AM
  2. C Beginner - Fahrenheit and Celsius Converter
    By Spink85 in forum C Programming
    Replies: 4
    Last Post: 02-05-2013, 06:23 PM
  3. Fahrenheit to Celsius Converter
    By MariosX in forum C Programming
    Replies: 9
    Last Post: 09-02-2011, 04:36 PM
  4. Celsius in Fahrenheit converter - does not run
    By ApeWithGrape in forum C Programming
    Replies: 10
    Last Post: 03-06-2011, 11:01 PM
  5. celsius to fahrenheit conversion
    By chipster18 in forum C Programming
    Replies: 11
    Last Post: 03-26-2003, 07:38 AM