Start New Discussion within our Software Development Community

If you are reading this post thanks for your help, I really do appreciate your help... Now on to my question I am trying to write a program that reads and echoes integers until the difference between two successive integers is greater than, or equal to, 10. [I'll need the abs() function to find the absolute value of the difference. but I dont know how to use that function, here is an example of how I would like it to work:
Sample Output:
Enter Some Numbers

1 3
6
20
14 is Greater than 10!


Note, The above is an example. You need to get the numbers from the user one at a time!

Specifics:
Create a function called diff that evaluates the absolute difference between the last two integers entered from the user. if the difference is greater than or equal to 10, the funcion will return false. Otherwise, it returns true.
Your main function should contain all other logic for the program. It should call the diff function each time a new integer is entered.
Pass the newly entered and previous integer values to the diff function which will compare the two.

I apoligize about the length of this post, but if you can point me into at least the right way to start this, I would greatly appreciate it. Thank you

Well first off, you can choose to use the abs function, or not as you wish. If you want to use the abs function, then do this

int a(93);
int b(102);
std::cout<<"difference == "<<abs(b-a)<<std::endl;

Note, that you will need to add #include <cmath> to your header.

You could also skip the abs function [by effectively coding your own] e.g.

int a(93),b(102);
diff=(b-a);
if (diff<0) diff*=-1;
std::cout<<"Diff == "<<diff<<std::endl;

You can also shorten the above with the ternary operator (? :), but that is not as readable to a beginner as the above (I think).

This article has been dead for over six months. Start a new discussion instead.