get largest number in array C++ no matter the elements size
/*
function for getting largest number in array of any unsigned number no matter the size of each element
it will return index of that number in the array
========================================
the function takes 3 parameters :
-(void* array_ptr) : which is basicly the address of the first byte in the target array (the address of the array)
-(int array_length): the length of the array / how many elements are there in that array
-(int var_size) : the size of each element in that array
[note : it searchs for the largest (unsigned int) value no matter the size of the element or the array]
the first byte is always the smallest then comes the larger one
for example
int A = 0x0EFFD930 ; int A (4 bytes)
in ram it should be stored like this [30 D9 FF 0E]
if the order of bytes is reversed [0E FF D9 30] it will be considered as 0x30D9FF0E
[also note : the array elements must be equal in size and (var_size) is equal to how many bytes is each element
and the length of the array must be correct or else the function will compair trash to your array of numbers
and will never work perfectly and it will not damge your data / system untill you overwrite to the index returned.
make sure the index returned is not outside the array for safty or make sure the input parameters are always correct!]
simple methods of making sure its not outside of your array
======================
using knowen array size
if ((return_index * element_size) > array_size) //if true then its outside!
using knowen array length
if(return_index > array_length) //if true then its outside!
*/
#include <Windows.h> //required for using "BYTE"
int GetLargestUnsignedIntElement(void* array_ptr, int array_length, int var_size)
{
/*
how the function works :
makes variable which will store the largest element found (var) go throw the array element by element and compair it with var :
if var is larger
it will do nothing and continue to the next element
else if var is smaller
it will overwrite var with the new largest value and update the value of (return_index) to be pointing to the index of the new largest value
at the end it will delete var for less memory usage and return the index of the largest element
(in case if array_length is not correct : it will go outside the array and start compairing trash (it may cause crash if it was outside of the program memory)) //no damge for the data but can go outside the array
(in case if \var_size is not correct : it will ruin the whole thing and will compair wrong numbers together) //no damge for the data but still can go outside the array
*/
void* var = malloc(var_size); //used to store the largest value found and compair it with the next one
int return_index = 0; //returned index
bool Larger = false; //used for testing if the value is larger or not
for (int x = 0;x < var_size;x++) //fill with 0
{
*(BYTE*)((int)var + x) = 0;
}
for (int index = 0, ptr = (int)array_ptr;index < array_length;index++, ptr = ((int)array_ptr) + (index * var_size))
/*
index : is the currect pointed element index
ptr : the address of the first byte of the element selected (pointed by index)
*/
{
for (int i = 0, iptr = ptr;i < var_size;i++, iptr++)
/*
i : the index byte inside the selected element
iptr : the address of the indexed byte insiede the element
*/
{
if (*(BYTE*)iptr > *(BYTE*)((int)var + i))
//compairing the largest number in (var) with the currect selected number
{
Larger = true;//if the last byte is larger then it will overwrite
continue; //continue the loop
}
else if (*(BYTE*)iptr == *(BYTE*)((int)var + i))
{
continue; //if both bytes are equal do nothing
}
else
{
Larger = false; //if the last byte is smaller do not overwrite
continue;
}
}
if (Larger)
{
for (int x = ptr, y = (int)var;x < (ptr + var_size);x++, y++)
/*
coping the value of (array_ptr[index]) to (var)
*/
{
*(BYTE*)y = *(BYTE*)x; //overwrite bytes
}
return_index = index; //update the index that is returned at the end
}
else
{
continue; //if not larger continue to next item
}
}
free(var);//free up used memory space
return return_index; //return the largest selected item index
}