Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ find largest number in array

#include <iostream>

int largestNumber(int nVariables, int numbers[]) //pass number of variables and number array
{
    int largest = numbers[0]; //initiate largest as the number at index 0
    
    for (int i = 0; i <= nVariables; i++) { //for loop to iterate through the numbers array that is nVariables long
        largest = (largest<numbers[i]?numbers[i]:largest);
      	//^ternary operator same as:
      	/*
        	if(largest < numbers[i]){
            	largest = numbers[i]; //sets largest as numbers[current iteration of i]
            }
            else{
            	largest = largest; // stays same
            }
        */
    }
    
    return largest;
}

int main()
{
    int nOfVariables;
    std::cout << "Input number of variables: ";
    std::cin >> nOfVariables;
    
    int variables[nOfVariables - 1];
    
    for (int i = 0; i < nOfVariables; i++) {
        std::cout << "Enter Variable #" << i + 1 << ": ";
        std::cin >> variables[i];
    }
    std::cout << std::endl;
    
    std::cout << std::endl << largestNumber(nOfVariables, variables) << std::endl;
}
Comment

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
}
Comment

find largest number in each row in array c++ using function

#include<iostream>
using namespace std;
void ro(int matrix1[][4]);
int main()
{
	int matrix1[6][4] = {                           //Always First Periorty Row & Second Periorty Colums
						  {42, 32, 42, 52 },       //Initilize 2D Array 6 Rows & 4 Colums 
						  {43, 34, 23, 53 },
						  {23, 54, 24,  3 },
						  {3,  5,  6,  55 },
						  {34, 6, 45,  56 },
						  {45, 56, 56, 57 }
                       	};
	ro(matrix1);

	return 0;
}
void ro(int matrix1[][4])
{

	for (int MainRowIndex = 0; MainRowIndex < 6; MainRowIndex++)                    // Main loop which decided which number of row check
	{
		int ColumnIndex = 0, MainRowIndexTeller = 0, ColoumsIndexTeller = 0;      // Decalaration && Inilization for Find Row & Colum Index in Every Main Row

		for (ColumnIndex = 0; ColumnIndex < 4; ColumnIndex++)
		{

			int max = matrix1[MainRowIndex][0];                              //Here We assign Every First element of row equal to Maximum Number 

			if (matrix1[MainRowIndex][ColumnIndex] > max)

			{

				matrix1[MainRowIndex][0] = matrix1[MainRowIndex][ColumnIndex];     //Here Condition When  first row Main Element is Less than Any other Element of this Specific row Than We assign the Maximum Element to first row Main Element. 

				MainRowIndexTeller = MainRowIndex;  ColoumsIndexTeller = ColumnIndex; //Here We Note the Running Index
			}

		}
		if (MainRowIndex == 0)
		{
			cout << "In Ist Row::  Maximum Value is " << matrix1[MainRowIndex][0] << "	Which Row&Colum No " << (1 + MainRowIndexTeller) << (1 + ColoumsIndexTeller) << "

";
		}

		if (MainRowIndex == 1)
		{
			cout << "In 2nd Row::  Maximum Value is " << matrix1[MainRowIndex][0] << "	Which Row&Colum No  " << (1 + MainRowIndexTeller) << (1 + ColoumsIndexTeller) << "

";
		}
		else if (MainRowIndex == 2)
		{
			cout << "In 3rd Row::  Maximum Value is " << matrix1[MainRowIndex][0] << "	Which Row&Colum No  " << (1 + MainRowIndexTeller) << (1 + ColoumsIndexTeller) << "

";
		}
		else if (MainRowIndex > 2)
		{
			cout << "In " << (1 + MainRowIndex) << "th Row::  Maximum Value is " << matrix1[MainRowIndex][0] << " 	Which Row & Colum No  " << (1 + MainRowIndexTeller) << (1 + ColoumsIndexTeller) << "

";
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: Array sum in c++ stl 
Cpp :: print linked list recursively c++ 
Cpp :: c++ print every element in array 
Cpp :: how to make a hello world program in c++ 
Cpp :: float max value c++ 
Cpp :: default access modifier in c++ in struct 
Cpp :: c++ main function 
Cpp :: tarray ue4 c++ 
Cpp :: iterating string in cpp 
Cpp :: cpp split string by space 
Cpp :: in c++ ++ how to write if without if 
Cpp :: push front vector cpp 
Cpp :: double to string c++ 
Cpp :: how to convert int to string c++ 
Cpp :: convert int to enum c++ 
Cpp :: copy array c++ 
Cpp :: max of two elements c++ 
Cpp :: random number cpp 
Cpp :: c++ string to int conversion 
Cpp :: elements of set c++ 
Cpp :: c++ add object to array 
Cpp :: vector fin element c++ 
Cpp :: c++ typing animation 
Cpp :: c++ programming language 
Cpp :: c++ vector extend vector 
Cpp :: c++ default parameters 
Cpp :: hello world program in c++ 
Cpp :: are strings mutable in c++ 
Cpp :: map declaration c++ 
Cpp :: C++ break with for loop 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =