Question:
c programming assignment incorrect output display?
Sofi
2010-02-12 19:51:39 UTC
I'm new to C (and don't really get it). I'm having a difficult time writing a program for an assignment. It's to convert temperatures from f to c to K, and my output displays always comes up 0.00. What's wrong with my program? I've stared at this for hours now.

I just want help to fix my incorrect display, and not rewrite the program unless I'm doing something really really wrong...

note: calculateTemperature is a subfunction of the function conversionCalculations. There are to be more subfunctions under conversions but I'm working on this first. Also program is meant to be overmodulized on purpose.


----------------------
# include

void userInput (int* fahrenheit);
int conversions (void);
float calculateTemperature (int fahrenheit);
void display (int temperature, int celsius, int kelvin);

int main (void)
{
int fahrenheit;
int celsius, kelvin;

conversions( );
userInput(&fahrenheit);
display (fahrenheit, celsius, kelvin);

return 0;
}

void userInput (int* fahrenheit)
{

printf("\nEnter Fahrenheit temperature (integer) ");
scanf("%d", fahrenheit);
return;
}

int conversions (void)
{

float celsius, kelvin;
return;
}

float calculateTemperature (int fahrenheit)
{
float celsius, kelvin;

celsius = (fahrenheit - 32)/1.8;
kelvin = celsius + 273.15;
return;
}

void display (int fahrenheit, int celsius, int kelvin)
{
printf("\n%10s%5d", "Fahrenheit", fahrenheit);
printf("%10s%5.2f", "Celsius", celsius);
printf("%10s%5.2f", "Kelvin", kelvin);
printf("\n\n");
return;
}
---------------------
Three answers:
2010-02-12 20:22:37 UTC
Hi,



I have repaired your program and it works perfectly. Some of the corrections I did were:



1. You need to pass the addresses of variables to the functions so that the changes to them are reflected outside.



2. You never called calculateTemperature function.



3. No need of conversion function as it is not doing anything.



4. Use data type double and not int for kelvin and Celsius.



--------------------

#include



void userInput (int* fahrenheit);

void calculateTemperature (int fahrenheit, double* celcius, double* kelvin);

void display (int temperature, double celsius, double kelvin);



int main (void)

{

int fahrenheit = 0;

double celcius = 0.0, kelvin = 0.0;

userInput(&fahrenheit);

calculateTemperature (fahrenheit, &celcius, &kelvin);

display (fahrenheit, celcius, kelvin);

return 0;

}



void userInput (int* fahrenheit)

{

printf("\nEnter Fahrenheit temperature (integer) ");

scanf("%d", fahrenheit);

}



void calculateTemperature (int fahrenheit, double* celcius, double* kelvin)

{

*celcius = ((double)fahrenheit - 32.0)/1.8;

*kelvin = (*celcius + 273.15);

}



void display (int fahrenheit, double celsius, double kelvin)

{

printf("\n%10s%5d", "Fahrenheit ", fahrenheit);

printf("%10s%5.2f", "Celsius ", celsius);

printf("%10s%5.2f", "Kelvin ", kelvin);

printf("\n\n");

}

--------------------



Sample Input and Output:



Enter Fahrenheit temperature (integer) -40



Fahrenheit -40 Celsius -40.00 Kelvin 233.15



Press any key to continue . . .



Varun
The Phlebob
2010-02-12 20:26:37 UTC
Declaring a variable in a function always overrides a same-named global variable for the duration of that function. This is called the "scope" of the variable.



You define celsius and kelvin in most of your functions. The compiler will silently use those variables for any computations done in the function.



If you don't want to use global variables (and they're usually deprecated), pass in by-reference arguments ( int * myvariable ) for them instead or, if you return only a single value, return it as the function's return value ( int myfunction( ) ).



Hope that helps.
2010-02-12 19:56:35 UTC
You never assign the values for celcius and kelvin to the variables in main. They are discarded as soon as you exit the "calulateTemperate" function. Also, your "conversions" function doesn't do anything.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...