thankgodforthissite
thankgodforthissite

Reputation: 25

Rewrite the temperature conversion program

I'm trying to rewrite a temperature conversion program into one that uses a function for conversion:

The first program is this:

#include <stdio.h>

int main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%d\t%d\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Now this is the program that I made with a function as the conversion:

#include <stdio.h>

int convert(int fahr, int celsius);

// test convert function

main()
{

    int fahr;
    int celsius;

    for(fahr = 0 ; fahr <= 300 ; fahr = fahr + 20){
        printf("%d %d\n", fahr, convert(fahr,celsius));
    }
    return 0;
}

// convert: Fahrenheit to Celsius

int convert(int fahr, int celsius)
{
    int c;
    while (fahr <= 300) 
        c = (5.0/9.0) * (fahr-32.0);

    return c;
}

The problem I am having is that when I try to execute the program, it just continually loads in the command line and I cant type in any characters. Can anyone point out what I am doing wrong? Thanks.

My output is suppose to look like this:

0   -17
20  -6
40  4
60  15
80  26
100 37
120 48
140 60
160 71
180 82
200 93
220 104
240 115
260 126
280 137
300 148              

Upvotes: 0

Views: 941

Answers (3)

Matthew
Matthew

Reputation: 9

I did something more simple, utilizing the code provided. This way the main will be less cluttered.

#include <stdio.h>
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */

float temp(float lowerIn, float upperIn, float stepIn);

int main() {
    printf("%6.2f", temp(0, 100, 10));
return 0;

}
float temp(float lowerIn, float upperIn, float stepIn){
    float fahr, celsius;

    float lower = lowerIn;
    float upper = upperIn;
    float step = stepIn;

    fahr = lower;
    while (fahr <= upper) {
        celsius = (5.0/9.0) * (fahr-32.0);
        printf("%3.0f %6.1f\n", fahr, celsius);
        fahr = fahr + step;
    }
}

Upvotes: 0

Semyon Usachev
Semyon Usachev

Reputation: 1

#include <stdio.h>
float tem(int);

main()
{
    int i;

    printf("Fahrenheit-Celsius table\n\n");

    for (i = 0; i <= 300; i += 20)
        printf("%3d\t%6.2f\n", i, tem(i));
    getchar();
    return 0;
}

float tem(int fahr)
{
    float cel;
    cel = (5.0 / 9.0) * (fahr - 32.0);
    return cel;
}

Upvotes: 0

igavriil
igavriil

Reputation: 1021

This line of code in your function

while (fahr <= 300) 

will never exit as your input always satisfies the inequality.

Also there is no reason defining the function as:

int convert(int fahr, int celsius)

because the second argument is never used.
If you want the variable celsius to change you should define the function to void and pass a reference of that variable.

#include <stdio.h>

void convert(int fahr, int *celsius);

main()
{
    int fahr;
    int celsius;

    for(fahr = 0 ; fahr <= 300 ; fahr += 20){
        convert(fahr, &celsius);
        printf("%d %d\n", fahr, celsius);
    }
    return 0;
}

void convert(int fahr, int *celsius)
{    
    *celsius = (5.0/9.0) * (fahr-32.0);
    return;
}

Upvotes: 3

Related Questions