Ok...here's the deal. I am struggling on a code and its part of my homework so i was gonna post it into the homework section, but then I feel that i wouldn't have a great deal of response. So, maybe i could get a better response here. Its basic C programming. I kind of understand the problem. I know their is a if, else...but the problem I am having is trying to get to do what I want. Here, I'll show a simple version of what I have so far... Thats all I have so far. Its not complete yet. I know that I have to brute force the if/else area but i have trouble trying to do the function. Can anyone help?
I understand how to write functions. I just don't know what to do with all the reference numbers running through the equations
Ok, first of all, the function definition should be: Code (Text): bool within_x_percent(int ref, int data, double x) Then the function would be: Code (Text): if( (ref - x*ref)<= data || data <= (ref + x*ref) ) return true; else return false;
It may be late but here a full solution. Code (Text): #include <stdio.h> /* Prototype */ bool within_x_percent(double SubtanceA, double SubtancesB, double percentage); // Constants const double Water = 100.0; const double Mercury = 357.0; const double Copper = 1187.0; const double Silver = 2193.0; const double Gold = 2660.0; int main() { /* declared variables */ double subtanceA=0,percentage =0; int subtanceB=0.0; /* Prompt the user */ //we get the subtanece printf("Enter subtance : "); scanf("%d", &subtanceB); //here the tolerance printf("Enter percentage : "); scanf("%d", &percentage); //here an example how to use it if(within_x_percent(Water,subtanceB,10 )) printf("the subtance is water ! : "); return 0; } bool within_x_percent(double SubtanceA, double SubtancesB, double percentage) { double range = SubtanceA * (percentage/100) ; double topRange = SubtanceA + range ; double bottomRange = SubtanceA - range ; if(SubtancesB >= bottomRange && SubtancesB <= topRange ) { return true; } else return false; }