Temperature Conversion Table (with step size)

This is a discussion on Temperature Conversion Table (with step size) within the C Programming forums, part of the General Programming Boards category; Hello, I'm having two issues with a program that basically converts the inputted temperature from degrees Fahrenheit to degrees Celsius ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    Temperature Conversion Table (with step size)

    Hello, I'm having two issues with a program that basically converts the inputted temperature from degrees Fahrenheit to degrees Celsius (code attached).

    The first problem is the fact that it does not print out the last value of the temperature conversion table. For instance, if I were to convert from 20 to 25 degrees with a step size of 5, it would only show the value 20.00 with its Celsius companion, but not 25.00. (It does compute the correct number of computed temperatures.) How would I fix this?

    Also, how might I prevent the program from going to an infinite loop when the step size is of a smaller increment? For my assignment, when the step size is less than 0.001, no table will be printed and an error message will be shown. I've tried adding another if statement, but it prints the table (which is not what it's supposed to do).

    Much help would be appreciated. (Note, I am a beginner to programming and this is for an introductory course.) Thank you.
    Attached Files Attached Files

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I open it to find this madness:
    Code:
    {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf350
    {\fonttbl\f0\fswiss\fcharset0 Helvetica;}
    {\colortbl;\red255\green255\blue255;}
    \margl1440\margr1440\vieww9000\viewh8400\viewkind0
    \pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
    
    \f0\fs24 \cf0
    Your code file should NOT have RTF tags in it. The compiler will never understand those. You want to use a plain text editor. I can recommend one if you need it, (Notepad++) but you probably already have a decent one in your IDE (such as Code Blocks or Visual Studio) which you should use.

    Fix that and then actually put the code in your post, please. A temperature converter is never big enough to warrant attachments instead.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Oh my. I apologize. Here is the code to the program:

    Code:
    #include <stdio.h>
    
    float tocelsius(float F);
    int temptable(float start, float stop, float step);
    
    main()
    {  float F,
             F_start,
             F_stop,
             step,
             flag;
    
            /* Initialize loop condition */
            /* Get the temperatures and step size */
            printf("Enter start, stop, and step: ");
            flag = scanf("%f %f %f", &F_start, &F_stop, &step);
    
            /* While the temperature is within set values */
            while( F_start > -459 && F_stop > -459 && flag != EOF )
            {
                /* If the temperature is greater than -459 degrees F */
                if( step > 0 )
                {   /* Print the result */
                    printf("\n Fahrenheit \t Celsius\n");
                    temptable(F_start, F_stop, step);
                }
    
                else
                {
                    printf("\n Fahrenheit \t Celsius\n");
                    temptable(F_start, F_stop, -step);
                }
    
                /* Update loop condition */
                /* Get the temperatures and step size */
                printf("Enter start, stop, and step: ");
                flag = scanf("%f %f %f", &F_start, &F_stop, &step);
            }
    
            if( flag == EOF )
            {
            printf("\nProgram terminated\n");
            }
    
    }
    
    
    float tocelsius(float F)
    /*  Given: a temperature reading in degrees Fahrenheit
     *  Returns: the temperature in degrees Celsius
    */
    
    {  float tocelsius;
    
            /* Compute temperature into degree Celsius */
            tocelsius = (F-32) * 5.0 / 9.0;
    
            /* Return the value */
            return tocelsius;
    }
    
    int temptable(float F_start, float F_stop, float step)
    /*  Given: starting and ending temperatures in degrees Fahrenheit
               and a step size.
            
        The function prints a table of conversions from degrees
        Fahrenheit to degrees Celsius from start to at most stop
        in "step" degree F increments, one conversion per line.
       
        Returns: the number of table lines printed.
    */
    
    {   float F,
              C;
        int lines;
    
            F = F_start;
    
            while( F != F_stop )
            {
                 C = tocelsius(F);
                 printf("   %4.2f \t  %4.2f\n", F, C);
    
                 if( F > F_stop )
                 {
                    F = F - step;
                    lines = 1 + (F_start - F_stop) / step;
                 }
    
                 else
                 {
                    F = F + step;
                    lines = 1 - (F_start - F_stop) / step;
                 }
            }
    
            /* Print Table Lines */
            printf(" Computed %d temperatures\n\n", lines);
    
            return lines;
    }

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The problem with not printing the last value comes down to this i suspect.

    Code:
    while( F != F_stop )
    If F_stop is the last value you need to print, you could try: F <= F_stop instead to include it.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Well the problem is with your loop logic:
    Code:
    while( F != F_stop )
            {
                 C = tocelsius(F);
                 printf("   %4.2f \t  %4.2f\n", F, C);
    
                 if( F > F_stop )
                 {
                    F = F - step;
                    lines = 1 + (F_start - F_stop) / step;
                 }
    
                 else
                 {
                    F = F + step;
                    lines = 1 - (F_start - F_stop) / step;
                 }
            }
    Firstly, if you want F_stop to be included in the results, then F != F_stop will exit the loop when F == F_stop. That value will never be computed or be part of the output.

    I would rewrite the algorithm so that F always increases toward F_stop by step. From the looks of things, all you have to do is flip flop F_start and F_stop if the former is more than the latter, when you call the temptable function. Then you can control the loop like this:

    while ( F <= F_stop )

    and F_stop will be included in the results.

    I think having a valid step size is part of temptable's preconditions (the state stuff has to be in for temptable to work correctly). So if a user's input is smaller than .0001 or whatever, don't call temptable at all. Just print the error from main and return to exit the program.

    Just as an additional comment, EOF is an integer, not a float. While type promotion wouldn't make comparing a float to EOF wrong, it is rather absurd.

    HTH.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Both of these posts help tremendously.
    Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  2. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM
  3. Replies: 11
    Last Post: 03-25-2003, 04:13 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 06:45 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21