Hi, I'm pretty new to the whole c++ language. I have been asked to:
Jot down a function to check whether a figure lies inside a variety of values. You will pass in the numerical value to confirm, the low end of the range and the high end of the range to the function. You must return 3 if the number is in the specified range, else 4. Include a driver program
I was wondering if someone could give me a hand with this? As I'm not very experienced with functions.
I would appreciate any help you could give me.
Thanks
C++ programming help- Function?
#include%26lt;iostream.h%26gt;
#include%26lt;conio.h%26gt;
int check(int value,int low,int high)
{if(value%26gt;=low%26amp;%26amp;value%26lt;=high)
return 3;
else return 4;
}
void main()
{int val,l,h,chk;
clrscr();
cout%26lt;%26lt;"Enter low and high values ";
cin%26gt;%26gt;l%26gt;%26gt;h;
clrscr();
cout%26lt;%26lt;"Range: "%26lt;%26lt;l%26lt;%26lt;"to"%26lt;%26lt;h;
cout%26lt;%26lt;"\nEnter no ";
cin%26gt;%26gt;val;
chk=check(val,l,h);
if(chk==3)
cout%26lt;%26lt;val%26lt;%26lt;" is in the given range ";
else
cout%26lt;%26lt;val%26lt;%26lt;" is not in the range ";
getch();
}
//Ok
Reply:Sure, This is quite easy.
First, you'd want a function like:
int RangeTest( int value, int lowEnd, int highEnd )
{
if ( value %26gt;= lowEnd %26amp;%26amp; value %26lt;= highEnd ) { return 3; }
else { return 4; }
}
Now you just need to call this function in the main function.
......
int result = RangeTest( userNumber, 0, 100 )
......
In the example above, you should replace the 0 and 100 with variables so they can be changed during runtime by the user, so they can do multiple tests.
Hope this helps.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment