Thread: celsius to fahrenheit conversion

  1. #1
    chipster18
    Guest

    Question celsius to fahrenheit conversion

    Can someone show me the code on how to print the numbers for a temperature conversion across the screen? Here is an example:
    Fahr Cels | Fahr Cels | Fahr Cels
    32 0 | 33 val | 34 val
    35 val | 36 val | 37 val

    This program will start at 32 degrees and go up to 212 degrees in one degree increments. I have never taken a programming class and this is something that has kinda fallen into my lap and needless to say, I do not know anything about programming at all. I am trying to help my son with a program and I wanted to see if someone may be able to help me on this one. If I am not mistaken I believe he will have to have 8 columns and 20 rows on each screen because I do not think that all of this will fit onto one screen. The user will not input anything at all. My son showed me what was called the executable program and it looks like the screen will have about 20 rows of data and then you press enter and a new screen comes up with more info and at the very end, the user is asked if they would like to see the information again (Y/N?) I am very appreciative of anyone who can help me out on this problem.

    Thanks,
    Tracie
    Email: [email protected]

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'll give you an idea.

    Formulas

    f to c : ( f temp - 32 ) / 1.8
    c to f: 32.0 + c temp * 1.8

    const double START = 32.0
    const double STOP = 212.0

    loop

    for(START; START <= STOP; START++)

    display

    printf("%.0f F = %.0f C", START, f to c)

    check to see if user wants to see it again

    end loop
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    that could give him 1 pair, but he asked for 8.

    im probally thinking about this to much, but would he have to use a really big array( like array[21][21][21]...... ). using alot of loops just sounds alittle insane.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I'm not sure, I did try.. this is what I get
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MIN 32
    #define MAX 212
    
    int main(void) {
    	double arr[1000];
    	int fahr,j,i;
    
    	for (fahr=MIN, j = 0; fahr<=MAX; fahr++,j++)
    		arr[j] =  (5.0/9.0) * (fahr-MIN);
    
    
    	for (fahr=MIN,i=0; fahr != MAX+1 ; ) {
    		for (j=0;j<3; j++) { //how many numbers per-line
    			if (fahr == MAX+1)
    				break;
    			printf("|%d %.2f|",fahr++,arr[i++]);
    		}
    	printf("\n");
    	}
    
    	return 0;
    }
    Last edited by Vber; 03-23-2003 at 03:37 PM.

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hmm.. I don't believe that you'll be able to get all of that in 8 cols unless the font is size 6

    I don't have time to play with this anymore, but it *might* offer something.

    Code:
    #include <stdio.h>
    
    double fTOc(double ftemp) { return ( ftemp - 32 ) / 1.8; }
    
    int main()
    {
       const char TABLE[] = "  Fahr = Cels  |  Fahr = Cels  |  Fahr = Cels  |"
                            "  Fahr = Cels  |  Fahr = Cels";
                            
       
       double start, stop;
       int count;
       
       start = 32.0;
       stop = 99.0;
       count = 1;
       
       printf("%s\n\n", TABLE);
       
       for(start; start <= stop; start++)
       {
           if(count == 6)
           {
              printf("\n");
              count = 1;
           }
                  
           if(fTOc(start) < 10.0)       
           	 printf("  %.1f   %.1f    ", start, fTOc(start)); 
           else
            printf("  %.1f   %.1f   ", start, fTOc(start));
             
           count += 2;
       }   
       
       return 0;
     }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    chipster18
    Guest

    reply on fahrenheit to celsius program

    I would like to thank everyone for their help, it has been tremendous. I think I may have misinterpreted the information from my son on his project. The results do not have to all show up on one screen. For example, if we start at 32 degrees with the celsius conversion, then I think the screen will go to about 66 or 67 degrees. Then, you are suppose to hit enter and another screen will display starting at 68 degrees and then once that screen is full, press enter once again and another screen is displayed until 212 degrees is displayed and then the user is prompted at the bottom of the screen if they would like to see the results again and the user can either select (Y/ N).

    Thanks again for everyones help.

    Tracie
    [email protected]

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I don't understand what you want to much, but try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define START 32 //from what fahr temperature to start
    #define MAX 212 //maximum temperature
    #define WHEN 54 //when to pause (every time a number mod 54 it will ask to pause)
    #define NPERLINE 3 //numbers per-line
    
    double ftoc(int fahr) {
    	return ((5.0/9.0) * (fahr - 32)); //formula to convert form fahr to celsius
    }
    
    int ask(void) {
    	char ch;
    	
    	printf("\nPress any key to display more, Q to exit\n");
    	ch = getch(); //get the use input and check to see if it's Q
    		
    	if(ch == 'Q') //if Q exit the program
    		exit(1);
    
    	system("cls"); //clear the screen
    	return 1;
    }
    
    int main(void) {
    	int i,j;
    
    	for (i=START; i<=MAX;) {
    		for (j=0; j<NPERLINE; j++) {
    			if(i%WHEN == 0 && !ask())
    			break;
    			if (i != MAX+1) //if it's not 213, if it's break
    			printf("%3d %5.2f ",i++,ftoc(i));
    		}
    		printf("\n"); //put a new line
    	}
    	return 0;
    }

  8. #8
    chipster18
    Guest

    Question fahrenheit to celsius conversion

    Again, I apologize if I did not do that good of a job explaining this project. I found out how to copy and paste so this is what the file that is called the executable is suppose to look like: The instructor did not do a very good job of spelling the information.

    Conversion from Farenhite to Celecus
    Screen # 1

    | 0.00= 32.00 | 0.56= 33.00 | 1.11= 34.00 | 1.67= 35.00 | 2.22= 36.00 |

    | 2.78= 37.00 | 3.33= 38.00 | 3.89= 39.00 | 4.44= 40.00 | 5.00= 41.00 |

    | 5.56= 42.00 | 6.11= 43.00 | 6.67= 44.00 | 7.22= 45.00 | 7.78= 46.00 |

    | 8.33= 47.00 | 8.89= 48.00 | 9.44= 49.00 | 10.00= 50.00 | 10.56= 51.00 |

    | 11.11= 52.00 | 11.67= 53.00 | 12.22= 54.00 | 12.78= 55.00 | 13.33= 56.00 |

    | 13.89= 57.00 | 14.44= 58.00 | 15.00= 59.00 | 15.56= 60.00 | 16.11= 61.00 |

    | 16.67= 62.00 | 17.22= 63.00 | 17.78= 64.00 | 18.33= 65.00 | 18.89= 66.00 |

    | 19.44= 67.00 | 20.00= 68.00 | 20.56= 69.00 | 21.11= 70.00 | 21.67= 71.00 |

    | 22.22= 72.00 | 22.78= 73.00 | 23.33= 74.00 | 23.89= 75.00 | 24.44= 76.00 |

    | 25.00= 77.00 | 25.56= 78.00 | 26.11= 79.00 | 26.67= 80.00 | 27.22= 81.00 |

    |----> Press a key to continue list...!

    The next screen would be screen 2 and the conversions, the next screen after that would be screen 3, the final screen would look like this and this is screen 4:

    Conversion from Farenhite to Celecus
    Screen # 4

    | 83.33=182.00 | 83.89=183.00 | 84.44=184.00 | 85.00=185.00 | 85.56=186.00 |

    | 86.11=187.00 | 86.67=188.00 | 87.22=189.00 | 87.78=190.00 | 88.33=191.00 |

    | 88.89=192.00 | 89.44=193.00 | 90.00=194.00 | 90.56=195.00 | 91.11=196.00 |

    | 91.67=197.00 | 92.22=198.00 | 92.78=199.00 | 93.33=200.00 | 93.89=201.00 |

    | 94.44=202.00 | 95.00=203.00 | 95.56=204.00 | 96.11=205.00 | 96.67=206.00 |

    | 97.22=207.00 | 97.78=208.00 | 98.33=209.00 | 98.89=210.00 | 99.44=211.00 |

    |100.00=212.00 |


    Again.....(Y/N).?




    Could someone please explain how to you go from screen 1 to 2 to 3 to 4? Again, I apologize for not explaining the information that well.

    Thanks,
    Tracie
    [email protected]

  9. #9
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    this should help, it works, i dont know if its what you want. ouput is just how you want it.
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define TRUE 1
    
    int main ()
    {
    	int userInput, blah, i= 0;
    	float cTemp = 0.0;
    	float fTemp = 32.0;
    	
    	//infinite loop, user will decide to break or not.
    	while (TRUE){
    		while (fTemp <= 81.00) {
    			printf("| %.2f=%.2f ", ((fTemp - 32) / 1.8), fTemp);
    			fTemp++;
    			i++;
    			if (i == 5){
    				printf("|\n");
    				i = 0;
    			}
    		}
    		printf("\nPress any key to continue list.\n\n");
    		userInput = getch();
    		while (fTemp <= 134.00) {
    			printf("| %.2f=%.2f ", ((fTemp - 32) / 1.8), fTemp);
    			fTemp++;
    			i++;
    			if (i == 5){
    				printf("|\n");
    				i = 0;
    			}
    		}
    		printf("\nPress any key to continue list.\n\n");
    		userInput = getch();
    		while (fTemp <= 187.00) {
    			printf("| %.2f=%.2f ", ((fTemp - 32) / 1.8), fTemp);
    			fTemp++;
    			i++;
    			if (i == 5){
    				printf("|\n");
    				i = 0;
    			}
    		}
    		printf("\nPress any key to continue list.\n\n");
    		userInput = getch();
    		while (fTemp <= 212.00) {
    			printf("| %.2f=%.2f ", ((fTemp - 32) / 1.8), fTemp);
    			fTemp++;
    			i++;
    			if (i == 5){
    				printf("|\n");
    				i = 0;
    			}
    		}
    		printf("\nWould you like to reprint this list?\n");
    		printf("1 - Yes, 2 - No: ");
    		scanf("%d", &blah);
    		if (blah == 2){
    			break;
    		} else {
    			fTemp = 32.00;
    		}
    	}
    }
    Last edited by stumon; 03-25-2003 at 10:16 PM.

  10. #10
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I just realized, you do not need cTemp, it is not used. Oh well, its quick and works, there are some bad designs through, specially the infinite loop.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I can't believe yall were duped into doing someone's homework...

    These boards do have a homework policy.

    gg

  12. #12
    Registered User Dev's Avatar
    Join Date
    Mar 2003
    Posts
    59
    I think the policy is to not ask for Homework and this messageboard is not setup to do someone else's homework.

    But I would like to mention here one thing. That helping someone in doing his homework has two effects.

    1 : The one who asks for homework is not getting any more practice on programming. So his knowledge level might not improve that much.

    2 : The one who is helping is getting more and more into programming.

    So It's better for us to help others. BUT NOT FOR SIMPLE STUPID questions like SWAPPING TWO NUMBERS and such other's.

    But As we get more and more into writing code we are sharping our own knowledge.

    Atleast that's what I think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion from Fahrenheit to Celsius problem
    By -Syntax in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2008, 08:56 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM