Your program should define and call a function within_x_percent that takes as pa
ID: 3542119 • Letter: Y
Question
Your program should define and call a function within_x_percent that takes as parameters a reference value ref, a data value data, the percentage value x and returns 1, meaning true it data is within x percent of rep
Substance Normal boiling point () Water 100 Mercury 357 Copper 1187 Silver 2193 Gold 2660 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 and identifies the substance and 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, the percentage value x and returns 1, meaning true it data is within x percent of rep - - 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.Explanation / Answer
#include <stdio.h>
#include <string.h>
/*
#define water 100;
#define mercury 357;
#define copper 1187;
#define silver 2193;
#define gold 2660;
*/
int within_x_percent(int ref,int userdata,int pent){
int calcvalue = (ref * pent/100);
if(userdata > (ref-calcvalue) && userdata < (calcvalue+ref))
{
return 1;
}else{
return 0;
}
}
main()
{
int data;
printf("enter observed boiling point");
scanf("%d", &data);
// printf(" value .... %d",within_x_percent(357,data,10));
if(within_x_percent(100,data,5)){
printf("water");
}else if((within_x_percent(357,data,5))){
printf("mercury");
}
else if(within_x_percent(1187,data,5)){
printf("copper");
}
else if(within_x_percent(2193,data,5)){
printf("silver");
}else if(within_x_percent(2660,data,5)){
printf("gold");
}else{
printf("unknown");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.