Thread: program that prints two tables showing...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question program that prints two tables showing...

    I don't have much experience in programming and i want to know if anyone can show how this can be done.


    Implement the following two functions:

    /* returns Celsius equivalent of a Fahrenheit temperature (ft) */
    double celsius(double ft);

    /* returns Fahrenheit equivalent of a Celsius temperature (ct) */
    double fahrenheit (double ct);

    Use these functions to write a program that prints two tables showing:

    (a) the Fahrenheit equivalent of all Celsius temperatures from 0 to 100 degrees with increment 2.
    (b) the Celsius equivalent of all Fahrenheit temperatures from 32 to 212 degrees with increment 4.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main ( void )
    {
    int i, celsius, farenheit,
    
    printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");
    
    
    
    for ( i = 0; i <= 100; i += 2 )
    {
    farenheit = ((9.0 / 5.0) * i) + 32;
    celsius = (5.0 / 9.0) * (i - 32.0);
    
    printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\ i, celsius, i, farenheit );
    
    }
    return EXIT_SUCCESS;

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I would think you'd need more variables and to use spaces instead of tabs. My approach to this would likely be having a base position for each, starting at your starting point. In each loop's round, add the required increment then put your converted number in the table. One method for lining things up is to use something like %12d which forces 12 characters to be reserved for the number display adding extra spaces as needed (use whatever is needed for nice alignments).

    I also see another problem - you are using integers when you are otherwise working with fractional values. Your math won't work out correctly every time. For this, use floats or doubles (floats should be adequate here).
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Within your forloop just call your two functions for the farenheit and celsius. I see you still have to create those. Then either add the stepping you need or set a static variable to increment.

    Then your printf statement will only need to call the functions with the stepping in place:

    Code:
     
     printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\ i, celsius() + celStep, i, farenheit() + ftStep );

    Is your real issue with how to create the function calls for these?

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Sorry, I may have read to quick on your use of the stepping you need. I see you are are after to print the farenheit every 2 degrees, then display the celsius after every 4 degrees. You will then need to align your table up to match the appropriate F-degrees with the C-degrees then.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Exclamation

    Quote Originally Posted by slingerland3g View Post
    Sorry, I may have read to quick on your use of the stepping you need. I see you are are after to print the farenheit every 2 degrees, then display the celsius after every 4 degrees. You will then need to align your table up to match the appropriate F-degrees with the C-degrees then.
    A friend helped me and we managed to do this but i don't know if it show what it should. because the windows closes. Can you all compile it and run if it opens and shows the right numbers please.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main ( void )
    {
    int i;
    float cel, farh;
    
    printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");
    
    
    
    for ( i = 0; i <= 100; i += 2 )
    {
    farh = ((9.0 / 5.0) * i) + 32;
    cel = (5.0 / 9.0) * (i - 32.0);
    printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\ 
    i, cel, i, farh );
    }
    return 0;
    }

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I think you should use the system("pause") function, but this isn't familiar territory for me. By the looks of your code, you are only getting steps of 2 for both, instead of 2 and 4 separately. Use another variable besides just "i" for this.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    37
    how can i make the steps of 2 and 4 separately?

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    37

    Question

    Quote Originally Posted by ulillillia View Post
    I think you should use the system("pause") function, but this isn't familiar territory for me. By the looks of your code, you are only getting steps of 2 for both, instead of 2 and 4 separately. Use another variable besides just "i" for this.
    how can i make the steps of 2 and 4 separately?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int
    main ( void )
    {
    int i, k;
    double Celsius, Fahrenheit;
    
    printf("\t\tFahrenheit<->Celsius Celsius<->Fahrenheit\n\n\n");
    
    
    
    for ( i = 0; i <= 100; i += 2)
    
    {
    Fahrenheit = ((9.0 / 5.0) * i) + 32;
    Celsius = (5.0 / 9.0) * (i - 32.0);
    printf ( "\t\t%3d\t - %6.2f \t%3d\t - %6.2f\n",\
    i, Celsius, i, Fahrenheit);
    }
    system("pause");
    
    }
    Last edited by Cyberman86; 04-08-2009 at 01:39 PM.

  9. #9
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    You forgot to change your printf statement. You're mentioning i twice in it. Fix that and you should be good. As a strong suggestion, use proper tabbing - it makes code much easier to read.

    Edit: In addition, you also forgot to change your for statement to using both i and k, as you call them. I often use more meaningful names like "CelsiusBase" and "FahrenheitBase" to distinguish, but you can call them whatever you want.
    Last edited by ulillillia; 04-08-2009 at 01:57 PM. Reason: Forgot to mention something
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM