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

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 :: sort array using stl 
Cpp :: copy constructor in c++ questions 
Cpp :: c++ sigabrt 
Cpp :: ue_log example 
Cpp :: c++ unordered set count 
Cpp :: c++ FAILED: objekt aufruf : symbol(s) not found for architecture x86_64 
Cpp :: run a c++ file in terminal 
Cpp :: python Difference Array | Range update query in O(1) 
Cpp :: c++ ide online 
Cpp :: convert "c++ to c" code online 
Cpp :: Initialize Vector Iterator with begin() function 
Cpp :: determining whether a array is a subsequence of another array 
Cpp :: how to find total numbe of distinct characters in a string in c 
Cpp :: how to fixed how many digit will be after point in c++ 
Cpp :: What will be the values of variables p, q and i at the end of following loop? int p = 5; int q = 18; for(int i=1;i&lt;5;i++) p++; --q; 
Cpp :: hwo to send token on redirection in passport 
Cpp :: c++ n in regex 
Cpp :: c++ string not printing 
Cpp :: return value optimization example 
Cpp :: inversed priority queue 
Cpp :: intage1 was not declared in this scope C++ 
Cpp :: c++ find string in string 
Cpp :: fenwick tree 
Cpp :: who made c++ 
Cpp :: passare un array a una funzione 
Cpp :: sinonimo de tratar 
C :: powershell search files for string 
C :: check dns server in linux 
C :: scanf ignore new line 
C :: yourkill071 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =