Page 1 of 2

Finding the biggest number in NXC

Posted: 25 Aug 2011, 19:14
by penguinplus
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

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 19:47
by h-g-t
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.

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 19:52
by penguinplus
So nobody knows how to find the max number out of the variables?

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 21:01
by spillerrec
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:

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
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 ">".

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 21:03
by penguinplus
So how would I use that in my code?

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 21:26
by muntoo
Here's my version:

Code: Select all

#define max(a,b) ((a)>(b)?(a):(b))

long lmax(long &arr[])
{
    unsigned int arr_len = ArrayLen(arr);

    if(!arr_len)
        return(LONG_MIN);

    long ret = arr[0];

    for(unsigned int i = 1; i < arr_len; ++i)
        ret = max(ret, arr[i]);

    return(ret);
}
EDIT: Realized you can do the same thing with 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);
}
With your code:

Code: Select all

task main()
{
    long L[5] = {42, -1, 96, 0xFFFFFFFF, 0x7FFFFFFF};

    NumOut(0, 0, lmax(L), 0);
    Wait(1000);
}

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 21:29
by penguinplus
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


    }

}

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 21:59
by muntoo
Here's the code:

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'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).

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 22:03
by penguinplus
I get this errorImage

Re: Finding the biggest number in NXC

Posted: 25 Aug 2011, 22:10
by muntoo
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.