Finding the biggest number in NXC
-
- Posts: 12
- Joined: 16 Nov 2010, 01:29
Finding the biggest number in NXC
I have 5 variables:
L1
L2
L3
L4
L5
And they all start out a 0. At the end of the program they are different numbers. I need to find the biggest variable at the end. How do I do that? Please help.
-Penguinplus
L1
L2
L3
L4
L5
And they all start out a 0. At the end of the program they are different numbers. I need to find the biggest variable at the end. How do I do that? Please help.
-Penguinplus
I take the place of Penguinplus from the old forums.
Re: Finding the biggest number in NXC
Couldn't find any inbuilt functions for min or max, which surprise me.
As a non-programmer, I would write something like
max = 0;
if (L1>max) max = L1;
if (L2>max) max = L2;
and so on.
Only works if the numbers are all positive.
As a non-programmer, I would write something like
max = 0;
if (L1>max) max = L1;
if (L2>max) max = L2;
and so on.
Only works if the numbers are all positive.
Last edited by h-g-t on 25 Aug 2011, 21:19, edited 1 time in total.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
-
- Posts: 12
- Joined: 16 Nov 2010, 01:29
Re: Finding the biggest number in NXC
So nobody knows how to find the max number out of the variables?
I take the place of Penguinplus from the old forums.
-
- Posts: 358
- Joined: 01 Oct 2010, 06:37
- Location: Denmark
- Contact:
Re: Finding the biggest number in NXC
h-t-g's method should be used if you want to get the biggest value hold by those variables.
If you want the name of that variable, use a variant:biggest_var_id will indentify which variable it is. It is good practice to use a special ID for invalid IDs so that you can do error checking.
Notice the "INT_MIN" constant. This will be replaced by the smallest value that variable type can hold so that this also will work with negative numbers. There is also "INT_MAX" which holds the largest value, and variants for every other integer type. Check the documentation.
Another thing to remember is that several variables might contain the same value and that you therefore might have several variables which contains the biggest value. You can do some more advanced stuff if you want to check for that.
Also note that I used ">=" instead. In case all variables contain INT_MIN, biggest_var_id would have stayed as '0' if you had used ">".
If you want the name of that variable, use a variant:
Code: Select all
int biggest_var_id = 0;
int biggest_value = INT_MIN; //Smallest value an int type can hold
if( L1 >= biggest_value ){
biggest_var_id = 1; //i.e. L1
biggest_value = L1;
}
if( L2 >= biggest_value ){
biggest_var_id = 2; //i.e. L2
biggest_value = L2;
}
//Repeat for each variable
Notice the "INT_MIN" constant. This will be replaced by the smallest value that variable type can hold so that this also will work with negative numbers. There is also "INT_MAX" which holds the largest value, and variants for every other integer type. Check the documentation.
Another thing to remember is that several variables might contain the same value and that you therefore might have several variables which contains the biggest value. You can do some more advanced stuff if you want to check for that.
Also note that I used ">=" instead. In case all variables contain INT_MIN, biggest_var_id would have stayed as '0' if you had used ">".
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
-
- Posts: 12
- Joined: 16 Nov 2010, 01:29
Re: Finding the biggest number in NXC
So how would I use that in my code?
I take the place of Penguinplus from the old forums.
Re: Finding the biggest number in NXC
Here's my version:
EDIT: Realized you can do the same thing with
EDIT: For "ids":
With your code:
arr_max = ArrayMax(arr, NA, NA);
EDIT: For "ids":
Code: Select all
#define max(a,b) ((a)>(b)?(a):(b))
#define ID unsigned int
ID lmax(long &arr[])
{
unsigned int arr_len = ArrayLen(arr);
ID ret = 0;
if(!arr_len)
return(ret);
for(unsigned int i = 1; i < arr_len; ++i)
ret = arr[ret] > arr[i] ? ret : i;
return(ret);
}
Code: Select all
task main()
{
long L[5] = {42, -1, 96, 0xFFFFFFFF, 0x7FFFFFFF};
NumOut(0, 0, lmax(L), 0);
Wait(1000);
}
Last edited by muntoo on 25 Aug 2011, 22:00, edited 3 times in total.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 12
- Joined: 16 Nov 2010, 01:29
Re: Finding the biggest number in NXC
I still don't get it. Could you add it straight with this code and just find which variable is the biggest in the end? Here is the code.
Code: Select all
task main()
{
while(true)
{
ClearScreen(); //Clear the screen
SetSensorLight(IN_3); //Set the light sensor
TextOut(0,LCD_LINE1,"The screen works"); //Test the screen
L1 = 0; //Light sensor reading #1
L2 = 0; //Light sensor reading #2
L3 = 0; //Light sensor reading #3
L4 = 0; //Light sensor reading #4
L5 = 0; //Light sensor reading #5
L1 = Sensor(IN_3); // Set the variable
TextOut(0,LCD_LINE2, "L1="); // Screen debugging
NumOut(20,LCD_LINE2, L1 ); // Screen debugging
Wait(500); // Wiat to read the screen
RotateMotor(OUT_C, 75,360);
RotateMotor(OUT_A, 75,-355);
L2 = Sensor(IN_3); // Set the variable
TextOut(0,LCD_LINE3, "L2="); // Screen debugging
NumOut(20,LCD_LINE3, L2 ); // Screen debugging
Wait(500); // Wiat to read the screen
RotateMotor(OUT_C, 75,360);
RotateMotor(OUT_A, 75,-355);
L3 = Sensor(IN_3); // Set the variable
TextOut(0,LCD_LINE4, "L3="); // Screen debugging
NumOut(20,LCD_LINE4, L3 ); // Screen debugging
Wait(500); // Wiat to read the screen
RotateMotor(OUT_C, 75,360);
RotateMotor(OUT_A, 75,-355);
L4 = Sensor(IN_3); // Set the variable
TextOut(0,LCD_LINE5, "L4="); // Screen debugging
NumOut(20,LCD_LINE5, L4 ); // Screen debugging
Wait(500); // Wiat to read the screen
RotateMotor(OUT_C, 75,360);
RotateMotor(OUT_A, 75,-355);
L5 = Sensor(IN_3); // Set the variable
TextOut(0,LCD_LINE6, "L5="); // Screen debugging
NumOut(20,LCD_LINE6, L5 ); // Screen debugging
Wait(5000); // Wiat to read the screen
}
}
I take the place of Penguinplus from the old forums.
Re: Finding the biggest number in NXC
Here's the code:
I've left a few comments. If you need any more help, just ask.
I suspect you're not familiar with arrays - read up on them here. I recommend reading this C++ Tutorial (most of it; you can skip the last few Advanced ones).
Code: Select all
task main()
{
SetSensorLight(S3);
int L[5];
int L_max;
int y;
while(true)
{
// Set things up.
ClearScreen();
y = LCD_LINE2;
// Determine and display L0, L1, L2, L3, L4.
for(unsigned int i = 0; i < 5; ++i)
{
// Get sensor value.
L[i] = Sensor(S3);
// Convert L and i to string.
string szL = NumToStr(L[i]);
string szi = NumToStr(i);
// Display.
TextOut(0, y, "L[" + szi + "] = " + szL);
Wait(500);
// Rotate Motors.
RotateMotor(OUT_C, 75, 360);
RotateMotor(OUT_A, 75, -355);
// Move display down one line.
y -= 8;
}
// NEW CODE!
// Determine largest L element value.
L_max = ArrayMax(L, NA, NA);
NumOut(0, y, L_max, 0);
Wait(4500);
}
}
I suspect you're not familiar with arrays - read up on them here. I recommend reading this C++ Tutorial (most of it; you can skip the last few Advanced ones).
Last edited by muntoo on 25 Aug 2011, 22:09, edited 2 times in total.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
-
- Posts: 12
- Joined: 16 Nov 2010, 01:29
Re: Finding the biggest number in NXC
I get this error
I take the place of Penguinplus from the old forums.
Re: Finding the biggest number in NXC
Download the latest test release. (It's this one, at the moment.) More info here.
Don't forget to download the latest firmware onto your NXT brick as well.
Don't forget to download the latest firmware onto your NXT brick as well.
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Who is online
Users browsing this forum: No registered users and 0 guests