Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an arrays of integer type and i would like to find the difference between two values in it.

For eg:

if score[0] = -10, and score[1] = 15

Then

The difference between score[0] and score[1] should give "25" as an answer.

and The difference between score[1] and score[0] should give the same answer too.

This is somewhat like the calculation in a x-y graph.

I am just an amateur and i don't know whether there exists any pre-defined methods in C#.NET that can solve this problem easily.

Any help would be much appreciated. Thanks
Posted

You are looking for the Math.Abs function. This will give you the difference in absolute terms between the two array elements.

HTH
 
Share this answer
 
Comments
steersteer 12-Sep-11 9:59am    
Thanks.
The following lines of code will give the same result:

C#
int diff = Math.Abs(score[0] - score[1]);
int diff2 = Math.Abs(score[1] - score[0]);


Honest opinion - you're asking too many basic questions. Learn how to use google and save yourself some time.
 
Share this answer
 
v2
Comments
steersteer 12-Sep-11 9:57am    
Thanks for the answer and advice, but I prefer learning this way. I Write in forums because it gives me an opportunity to converse with the Gurus directly. Of course my questions are basic to anyone with decent knowledge in programming, but it is a critical question for a beginner like me. Every solution I get for my questions here is a grain of knowledge to me. In my case, Google has sometimes ended up as a pandora's box. Just like when an EFL student uses an english dictionary to know the synonym of a word, where the meaning itself contains too many new words that the learner does not know what it means. Learning is subjective and I have my own views about it. I am not compelling you to reply me. I am just humbly asking for help. If it bothers you then i am sorry and you can ignore my questions, but please don't give any such deterrent statements. I don't mind spending time here when I have the eagerness to learn something new. Thanks once again for the answer.
#realJSOP 12-Sep-11 10:12am    
But actually *looking it up* has the side effect of teaching you something else that may or may not be related to your original question. Using google would have introduced you to the entire Math namespace, and would most certainly prevent you from having to ask further questions about it.
BobJanova 12-Sep-11 10:39am    
JSOP is right, when programming against a framework the skill of how to search to find out if something is there is very valuable and is worth much more than each individual small answer you might get here.
steersteer 12-Sep-11 10:40am    
I agree with you, but my problem at that moment was to find the "difference" value. Having got introduced to Abs() method by you, now I am starting to play around with other methods in math namespace using intellisense. Feeling a bit more confident as well. From now, I am sure I can find the answers by myself regarding math namespace. Actually, this is the type of learning methodology I prefer. A solution to my current problem and a hint of recommendation from the expert himself for further reading and understanding using which I can solve similar problems by myself in the future. Btw, I am using google and reading from already written articles too. Thanks
C++
int diff = (score[0] > score[1]) ? (score[0] - score[1]) : (score[1] - score[0]);
 
Share this answer
 
Comments
steersteer 12-Sep-11 10:10am    
Thanks
BobJanova 12-Sep-11 10:39am    
Nice.
Mohammad A Rahman 13-Sep-11 0:47am    
Simple, 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900