|
Thread: C
-
Dec 15th, 2000, 09:09 AM
#1
Thread Starter
New Member
Can anyone help I have a program to write
I need to write a pgm that converts a series of input temperatures in either degree Celsius or Fahrenheit and calculate the absolute temperature in degrees Kelvin.
So if the input was 32F the result would be 273K.Repeat this process for any number of input temperatures and scales.
The pgm should finish with the following message "Error - Negative Absolute Value do not assume the input will only be 'C' OR 'F'.
The required formulae are
K = C + 273 or K = (F-32) / 1.8 + 273
Can anyone help! I'me slowly going insane......
-
Dec 15th, 2000, 10:20 AM
#2
Frenzied Member
Well if you define the way you want the thing to work then this is an easy program. Something like this:
Code:
#include <stdio.h>
#include <stdlib.h>
void typeError();
void main()
{ int temp;
char type;
printf("What unit are you going to enter a temperature in? (C/F/K) ");
scanf("%c", &type);
printf("\n\nEnter an integer value for your temperature: ");
scanf("%d", &temp);
printf("\n\n");
switch(tolower(type))
{ case 'c':
temp = temp + 273;
break;
case 'f':
temp = (temp-32)*5/9 + 273;
break;
case 'k':
break;
default:
typeError();
}
printf("Which unit would you like this temperature in? (C/F/K) ");
scanf("%c", &type);
switch(tolower(type))
{ case 'c':
temp = temp - 273;
break;
case 'f':
temp = (temp-241)*9/5;
break;
case 'k':
break;
default:
typeError();
}
printf("%d %c\n\n", temp, toupper(type));
}
void typeError()
{ printf("Error: invalid unit; must be C, F or K\n\n");
exit(1);
}
Note that this is C code. C++ is easier but you said C, so you got C
Harry.
"From one thing, know ten thousand things."
-
Dec 15th, 2000, 10:54 AM
#3
'scuse me, but toupper() and tolower() are in ctype.h
-
Dec 15th, 2000, 12:41 PM
#4
Frenzied Member
Good call D, sorry 'bout that. My bad.
you'll need to add at the top:
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|