Thread: C Program Create Table

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    36

    C Program Create Table

    Hello,

    This is my first C Program ever. I need to build a table with a conversion table from Fahrenheit to Celsius. My program works as intended (after a few hours and some worthy research) I am not 100% confident with my program specially the part of the printf directives; they look too long and too bulky; and let's don't mention the amount of "magic numbers" I have in there for the indentations (to try to make it look pretty).

    In other words is there a better way to build this table
    ? A more dynamic one without embedded numbers?

    All comments are more than welcome. Feel free to comment my code: industry standards, better functions, style. This is my first time with C so I have a lot to learn.

    This is my source code:

    Code:
    #include <stdio.h>
    
    
    /* program defines */
    #define TRUE 1            /* boolean true */
    #define FALSE 0            /* boolean false */
    #define ERRORS 1        /* errors found */
    #define NOERRORS 0        /* no errors found */    
    #define TEMP_MIN -20.0  /* min. temperature for table*/
    #define TEMP_MAX 280.0  /* max. temperature for table*/
    
    
    /* function prototypes */
    float convertToCelsius(float fahr);
    float convertToFahr(float celsius);
    int isValidInput(int input);
    
    
    
    
    int main(int argc, char *argv[])
    {
        /* Declare variables and initialize */
        int step = 0;
        float temp = 0;
        float fahr = 0;
        float celsius = 0;
        
        /* Prompt the user to enter step */
        printf("%s\n","Please enter the the conversion step:");
        scanf("%d",&step);
        
            
        if(isValidInput(step) == TRUE){
        
            printf("\n%s\t%s\t\t%s\t%s\n","Fahrenheit","Celsius","Fahrenheit","Celsius");
            printf("%s\n","-----------------------------------------------------------");
            
            temp = TEMP_MIN;
            while(temp < TEMP_MAX){
                
                fahr = convertToFahr(temp);
                celsius = convertToCelsius(temp);
                
                printf("%8.1f\t%6.1f\t%15.1f\t%14.1f\n",temp,celsius,temp,fahr);
                
                temp += step;        
            }
        }
            
        return NOERRORS;
    }
    
    
    /***************************************************
     Purpose    : Convert from fahrenheit to celsius.
    ***************************************************/
    float convertToCelsius(float fahr){    
        return (((fahr - 32) * 5) / 9);
    }
    
    
    /***************************************************
     Purpose    : Convert from celsius to fahrenheit.
    ***************************************************/
    float convertToFahr(float celsius){    
        return (((celsius * 9) / 5) + 32);
    }
    
    
    
    
    /***************************************************
     Purpose    : Convert from celsius to fahrenheit.
    ***************************************************/
    int isValidInput(int input){    
        if((input >= 1) && (input <= 9)){
            return TRUE;
        }else {
            return FALSE;
        }
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Actually, your program looks really good. You have great indentation, you're using named constants, and you're using functions right.

    If you're converting from Fahrenheit to Celsius, you only need two columns (note that currently, the fourth column is wrong). You also don't need "temp" - just set "fahr" to TEMP_MIN and run through the loop using "fahr" instead.

    This also means you don't need the "convertToFahr()" function (unless you want to get fancy and convert Celsius back to Fahrenheit).

    A few additional comments:


    • Code:
      printf("%s\n","Please enter the the conversion step:");
      
      // ...
      
      printf("\n%s\t%s\t\t%s\t%s\n","Fahrenheit","Celsius","Fahrenheit","Celsius");
      You don't need to use "%s" for these print statements - they can just be:

      Code:
      printf("Please enter the the conversion step:\n");
      
      // ...
      
      printf("\nFahrenheit\tCelsius\t\tFahrenheit\tCelsius\n");

    • Code:
      return NOERRORS;
      There is nothing wrong with this, but it is more common to just return 0 or EXIT_SUCCESS (from "stdlib.h"). Also you're not using the constant ERRORS anywhere.
    • You should also use named constants for the min/max values of valid input (to be used in the "isValidInput()" function).
    • If the input is invalid, you should print a message saying so, so the user know why the program has not produce the expected output.
    • Unless you have specific reasons for using floats, you should use double instead.


    Overall, great job!

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Hi Matticus,

    Thanks a lot for the great help and feedback. It allowed me to learn a lot from the code.

    The reason why I have four columns in my table is because the assignment is like that; which also explains why I am using "temp"; the question is not about converting from unit the other. It's just go through a range of numbers assuming that is Fahrenheit and convert to Celsius; and then do the same assuming its Celsius. (it's a little weird but I am replicating the output of the assignment to the letter )

    I fixed the values for printf() with only strings. Also I added named constants to the validation.

    For the NOERRORS I am using it because the professor provided us with a small sample of variables to use and I just copied this. Also I left ERRORS in because it seemed wrong not to put if I had the other one. I have created a template that I copy over my work so I do not have to spend too much time setting this up. For what I have read I can expand functionalities by using libraries (you suggested stdlib.h and someone else I read about stdbool.h) but I wan to learn a little bit about the basics also show my professor that I am doing it little by little.

    I finished coding this last night and I totally forgot to guide the users to what a good input it; I added it now.

    I am using floats because that's what has been used so far in my book; no reason in specific. Perhaps I am moved by the "%f" in the printf() so I can associate a little

    Thanks a lot for your help again. This is the final reviewed code.

    Code:
    #include <stdio.h>
    
    
    /* program defines */
    #define TRUE 1				/* boolean true */
    #define FALSE 0				/* boolean false */
    #define ERRORS 1			/* errors found */
    #define NOERRORS 0			/* no errors found */	
    #define TEMP_MIN -20.0  	/* min. temperature for table*/
    #define TEMP_MAX 280.0  	/* max. temperature for table*/
    #define VALID_INPUT_MIN 1	/* min. number acceptable input */
    #define VALID_INPUT_MAX 9 	/* min. number acceptable input */
    
    
    /* function prototypes */
    float convertToCelsius(float fahr);
    float convertToFahr(float celsius);
    int isValidInput(int input);
    
    
    
    
    int main(int argc, char *argv[])
    {
    	/* Declare variables and initialize */
    	int step = 0;
    	float temp = 0;
    	float fahr = 0;
    	float celsius = 0;
    	
    	/* Prompt the user to enter step */
    	printf("Please enter the the conversion step:\n");
    	scanf("%d",&step);
    	
    		
    	if(isValidInput(step) == TRUE){
    	
    		printf("\nFahrenheit\tCelsius\t\tFahrenheit\tCelsius\n");
    		printf("-----------------------------------------------------------\n");
    		
    		temp = TEMP_MIN;
    		while(temp < TEMP_MAX){
    			
    			fahr = convertToFahr(temp);
    			celsius = convertToCelsius(temp);
    			
    			printf("%8.1f\t%6.1f\t%15.1f\t%14.1f\n",temp,celsius,temp,fahr);
    			
    			temp += step;		
    		}
    	}else{
    		printf("\nInvalid input, make sure you are entering a single digit number 1 - 9\n");
    	}
    	    
    	return NOERRORS;
    }
    
    
    /***************************************************
     Purpose	: Convert from fahrenheit to celsius.
    ***************************************************/
    float convertToCelsius(float fahr){	
    	return (((fahr - 32) * 5) / 9);
    }
    
    
    /***************************************************
     Purpose	: Convert from celsius to fahrenheit.
    ***************************************************/
    float convertToFahr(float celsius){	
    	return (((celsius * 9) / 5) + 32);
    }
    
    
    
    
    /***************************************************
     Purpose	: Convert from celsius to fahrenheit.
    ***************************************************/
    int isValidInput(int input){	
    	if((input >= VALID_INPUT_MIN) && (input <= VALID_INPUT_MAX)){
    		return TRUE;
    	}else {
    		return FALSE;
    	}
    }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by fredlo2010 View Post
    The reason why I have four columns in my table is because the assignment is like that; which also explains why I am using "temp"; the question is not about converting from unit the other. It's just go through a range of numbers assuming that is Fahrenheit and convert to Celsius; and then do the same assuming its Celsius. (it's a little weird but I am replicating the output of the assignment to the letter )
    Ah, I see what you're trying to do there. Something still isn't lining up, though.

    Code:
    printf("\nFahrenheit\tCelsius\t\tFahrenheit\tCelsius\n");
    
    printf("%8.1f\t%6.1f\t%15.1f\t%14.1f\n",temp,celsius,temp,fahr);
    The fourth column header says "Celsius", but the fourth argument you're printing is "fahr". You should ensure that your variables contain values that match the name (i.e. "fahr" should not contain a value in Celsius).

    I don't think it makes much sense to convert from some unitless value to a temperature. I would suggest following my original advice and using "fahr" in lieu of "temp". Then use this value to find Celsius (celsius = convertToCelsius(fahr)). Then use the new value of "celsius" to convert back to Fahrenheit (3 columns total). This is also a good way to visually confirm the conversions (columns 1 and 3 would be basically the same value). This way just makes more sense to me, but you can approach it however you'd like.

    Quote Originally Posted by fredlo2010 View Post
    Also I added named constants to the validation.
    Very good. However, the idea of using named constants is so, if you want to change all of those values in the program, you just have to change the #define. Therefore, I would change this:

    Code:
    printf("\nInvalid input, make sure you are entering a single digit number 1 - 9\n");
    ... to this:

    Code:
    printf("\nInvalid input, make sure you are entering a single digit number %d - %d\n", VALID_INPUT_MIN, VALID_INPUT_MAX);
    Quote Originally Posted by fredlo2010 View Post
    I am using floats because that's what has been used so far in my book; no reason in specific. Perhaps I am moved by the "%f" in the printf() so I can associate a little
    "%f" in "printf()" is used for floats and doubles. But floats are fine.

    If you really want to be explicit, you can declare your constants as floats by appending with an 'f':

    #define TEMP_MAX 280.0f
    float fahr = 0.0f;
    etc

    Quote Originally Posted by fredlo2010 View Post
    Thanks a lot for your help again. This is the final reviewed code.
    You're very welcome.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    36
    Quote Originally Posted by Matticus View Post
    The fourth column header says "Celsius", but the fourth argument you're printing is "fahr".
    Great catch. Yes you are totally right I had the headers flipped.

    As per the four or three columns I have no option (See image)

    C Program Create Table-capture-jpg



    Quote Originally Posted by Matticus View Post
    Very good. However, the idea of using named constants is so, if you want to change all of those values in the program, you just have to change the #define.
    Great, and yes I changed it. If anything changes later, I only have to do it in one place.


    Quote Originally Posted by Matticus View Post
    f you really want to be explicit, you can declare your constants as floats by appending with an 'f'
    Good to know.

    Again thanks a lot for the help to make my C experience better.
    Last edited by fredlo2010; 01-21-2016 at 03:53 PM. Reason: misspell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-29-2013, 07:56 AM
  2. Multiplication Table Program
    By jamesallen4u in forum C Programming
    Replies: 3
    Last Post: 10-17-2011, 07:48 AM
  3. including a table in my program
    By kingjames in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2007, 02:38 AM
  4. question on create a crc16 table
    By mycount in forum Tech Board
    Replies: 3
    Last Post: 08-29-2006, 08:03 PM
  5. Replies: 0
    Last Post: 03-15-2002, 02:07 PM