I'm writing this program the long way with "if" statements instead of using an array. I know, there is an easier way of doing this but I want to understand the long way before the shortway. Anyways, I'm writing a C program for the following problem:
The table below shows the normal boiling points of several substances. Write a program that prompts the user for the observed boiling point of a substance in Celsius degrees and identifies the substance if the observed boiling point is within 5% of the expected boiling point. If the data input is more than 5% higher or lower than any of the boiling points in the table, the program should output the message “Substance unknown.” Your program should define and call a function “within_x_percent” that takes as parameters a reference value ref, a data value data, and a percentage value x and returns 1 meaning true if data is within x percent of ref –that is,
(ref – x% * ref) < data < (ref + x% * ref). Otherwise “within_x_percent would return zero, meaning false. For example, the call “within_x_percent(357, 323, 10) would return true, since 10% of 357 is 35.7, and 323 falls between 321.3 and 392.7.
Table:
Substance / Normal boiling point (celcius)
Water / 100
Mercury / 357
Copper / 1187
Silver / 2193
Gold / 2660
The problem I'm having is with my declarations of the substances (water, mercury,copper,gold). When I compile, I get the error, "warning: overflow in implicit constant conversion" and "warning: character constant is too long for its type." I'm not too familiar with strings yet. Can I use int instead of char? Can someone tell me the proper way of declaing these? I'm sure I may have more wrong with my code but I would like to get by this problem first before I tweak anything else.
Here is my code:
Code:
/*
Filename: range.c
Description: Displays a substance if the observed boiling point is
within 5% of the expected boiling point.
*/
# include <stdio.h>
void introduction (void);
int within_x_percent (double ref, double data, double x);
int main()
{
/*Allocate memory for data*/
introduction ();
double ref,data,x;
char s1 = 'Water';
char s2 = 'Mercury';
char s3 = 'Copper';
char s4 = 'Silver';
char s5 = 'Gold';
/*Prompt user for data*/
printf ("Enter the observed boiling point in degrees celcius:\n");
scanf ("%f",&data);
/*Processing and display results*/
if (data = within_x_percent(100,data,x)
printf("%d is within 5% of %c.\n",data,s1);
else if (data = within_x_percent(357,data,x)
printf("%d is within 5% of %c.\n",data,s2);
else if (data = within_x_percent(1187,data,x)
printf("%d is within 5% of %c.\n",data,s3);
else if (data = within_x_percent(2193,data,x)
printf("%d is within 5% of %c.\n",data,s4);
else if (data = within_x_percent(2660,data,x)
printf("%d is within 5% of %c.\n",data,s5);
else
printf("Substance Unknown\n");
return 0;
}
void introduction (void)
{
printf ("\nThis program identifies a substance if the observed");
printf ("is within 5% of the expected boiling point. If the");
printf ("data is more than 5% higher or lower, the program will");
printf ("display the message substance unknown.\n");
}
int within_x_percent (double ref, double data, double x)
{
x=.05;
(ref-x*ref) < data < (ref+x*ref);
}