I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!
help | c++ Question
Moderator: Coders of Rage
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: help | c++ Question
if you have just a regular array you could do something like this...Avishaiozeri wrote:I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!
note: you need to know the size of the array ahead of time (you can almost always know this)...
Code: Select all
int position_Of_Largest_Number = 0;
int temp = 0;
for(int i = 0;i<arraySize;i++)
{
if(myArray[i] > temp)
{
position_Of_Largest_Number= i;
temp = myArray[i];
}
}
edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Something like this:
Code: Select all
if( (float1 - float2) <= EPSILON)
{
// they are the same number, do something
else
{
// they are not the same number
Last edited by short on Wed Mar 31, 2010 11:09 am, edited 3 times in total.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: help | c++ Question
Thanks! i'll try it!short wrote:if you have just a regular array you could do something like this...Avishaiozeri wrote:I need help! I'm a beginner, and i was in the middle of writing a simple console combat simulator for practice.
Is there any way in c++ to check wich is the bigger from a list of numbers? i have an array of numbers, and i need to check wich of the numbers in the it is bigger. Please help! i'm stuck!
note: you need to know the size of the array ahead of time (you can almost always know this)...
This is definitely not the fastest way, but it will work!Code: Select all
int position_Of_Largest_Number = 0; int temp = 0; for(int i = 0;i<arraySize;i++) { if(myArray[i] > temp) { position_Of_Largest_Number= i; temp = myArray[i]; } }
edited: At first i didn't understand it, but now i do. thanks, it works!
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: help | c++ Question
Actually your first method will work fine, because you're using "greater than", not checking equality.short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary
Re: help | c++ Question
cant believe im giving up my 1337 for this...RyanPridgeon wrote:Actually your first method will work fine, because you're using "greater than", not checking equality.short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary
the C standard library has a header called float.h, and in it there it a define, FLT_EPSILON or something like that. its the smallers number where a+b != a is false
i think its something like 1.192E-7 for single precision and 2.22E-16 for double precision. but machine epsilon is a real term for indicating this principal.
if you ever develop something that has to be IEEE compliant you HAVE to know this shit.
edit: http://en.wikipedia.org/wiki/Machine_epsilon
ps: that wiki i think is wrong, its not 2^-24 and -53, its -23 and -52, i cant find a IEEE document to validate, but my book "Numerical mathematics and computation"
http://www.amazon.com/Numerical-Mathema ... 0534389937
says it is.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: help | c++ Question
I learned something from itcant believe im giving up my 1337 for this...
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
-
- Chaos Rift Cool Newbie
- Posts: 85
- Joined: Wed Mar 17, 2010 4:32 pm
Re: help | c++ Question
I got it working! thank you all .
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: help | c++ Question
Was I right that you don't need it for the > comparison though?avansc wrote:cant believe im giving up my 1337 for this...RyanPridgeon wrote:Actually your first method will work fine, because you're using "greater than", not checking equality.short wrote: edit: while this would work with integers, it does not ALWAYS work with floating point numbers (doubles, floats, etc...) because of how the data for those data types are stored in memory.. you need to set an epsilon value, say 0.0000001 or so and check if the two floating point numbers are within that range one another.
Also kinda funny you picked 0.0000001, because that number cannot be stored exactly in floating point binary
the C standard library has a header called float.h, and in it there it a define, FLT_EPSILON or something like that. its the smallers number where a+b != a is false
i think its something like 1.192E-7 for single precision and 2.22E-16 for double precision. but machine epsilon is a real term for indicating this principal.
if you ever develop something that has to be IEEE compliant you HAVE to know this shit.
edit: http://en.wikipedia.org/wiki/Machine_epsilon
ps: that wiki i think is wrong, its not 2^-24 and -53, its -23 and -52, i cant find a IEEE document to validate, but my book "Numerical mathematics and computation"
http://www.amazon.com/Numerical-Mathema ... 0534389937
says it is.
Unless you do something like
Code: Select all
if (x > y + FLT_EPSILON) {
Edit: sorry for kinda stealing the thread here D: