Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find second highest number in c++

#include <iostream>
using namespace std;
int main(){
   int n, num[50], largest, second;
   cout<<"Enter number of elements: ";
   cin>>n;
   for(int i=0; i<n; i++){
      cout<<"Enter Array Element"<<(i+1)<<": ";
      cin>>num[i];
   }
   /* Here we are comparing first two elements of the
    * array, and storing the largest one in the variable
    * "largest" and the other one to "second" variable.
    */
   if(num[0]<num[1]){ 
      largest = num[1];
      second = num[0];
   }
   else{ 
      largest = num[0];
      second = num[1];
   }
   for (int i = 2; i< n ; i ++) {
      /* If the current array element is greater than largest
       * then the largest is copied to "second" and the element
       * is copied to the "largest" variable.
       */
      if (num[i] > largest) {
         second = largest;
         largest = num[i];
      }
      /* If current array element is less than largest but greater
       * then second largest ("second" variable) then copy the
       * element to "second"
       */
      else if (num[i] > second && num[i] != largest) {
         second = num[i];
      }
   }
   cout<<"Second Largest Element in array is: "<<second;
   return 0;
}
Comment

find second largest number in array c++

/*
quick explaination
basicly its searching for the largest UNSIGNED INT in an array of UNSIGNED INT where all the elements are the same size in bytes but the size in all the elements is a variable and can be changed

this method is finding the largest number but to find the second or third or even sort the array
i will create some sort of stack where each element is pushed but sense this is to find the 2nd largest number
it will just do that but you get the idea if you want to change the code to be something that fits you

suggestion : if you already know what size of numbers your array will be then its better to create
an easier and much effecient method in your case but this is a general one where the size isn't really knowen
*/

int GetLargestUnsignedIntElement(void* array_ptr, int array_length, int var_size)
{
	void* var = malloc(var_size); 
	int second_largest = 0; //to be returned
	int largest_index = 0;
    //both the integers above are more like a "stack" but much more effecient
    //than using a real stack class
	bool Larger = false;
	for (int x = 0; x < var_size; x++) 
	{
		*(unsigned char*)((int)var + x) = 0;
	}
	for (int index = 0, ptr = (int)array_ptr; index < array_length; index++, ptr = ((int)array_ptr) + (index * var_size))
	{
		for (int i = 0, iptr = ptr; i < var_size; i++, iptr++)
		{
			if (*(unsigned char*)iptr > *(unsigned char*)((int)var + i))
			{
				Larger = true;
				continue; 
			}
			else if (*(unsigned char*)iptr == *(unsigned char*)((int)var + i))
			{
				continue; 
			}
			else
			{
				Larger = false; 
				continue;
			}
		}
		if (Larger)
		{
			for (int x = ptr, y = (int)var; x < (ptr + var_size); x++, y++)
			{
				*(unsigned char*)y = *(unsigned char*)x;
			}
          	//push the result
			second_largest = largest_index;
			largest_index = index;
          
		}
		else
		{
			continue;
		}
	}

	free(var);
	return second_largest;
}
Comment

Find second largest number in an array

// Java program to find second largest
// element in an array
import java.util.*;
class GFG{
 
// Function to print the
// second largest elements
static void print2largest(int arr[],
                          int arr_size)
{
  int i, first, second;
 
  // There should be
  // atleast two elements
  if (arr_size < 2)
  {
    System.out.printf(" Invalid Input ");
    return;
  }
 
  // Sort the array
  Arrays.sort(arr);
 
  // Start from second last element
  // as the largest element is at last
  for (i = arr_size - 2; i >= 0; i--)
  {
    // If the element is not
    // equal to largest element
    if (arr[i] != arr[arr_size - 1])
    {
      System.out.printf("The second largest " +
                        "element is %d
", arr[i]);
      return;
    }
  }
 
  System.out.printf("There is no second " +
                    "largest element
");
}
 
// Driver code
public static void main(String[] args)
{
  int arr[] = {12, 35, 1, 10, 34, 1};
  int n = arr.length;
  print2largest(arr, n);
}
}
 
// This code is contributed by gauravrajput1
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to modify 2d array in function c++ 
Cpp :: long long vs long long int 
Cpp :: print all subsequences 
Cpp :: operator overloading c++ 
Cpp :: c++ char 
Cpp :: c++ custom printf 
Cpp :: unordered_map in c++ 
Cpp :: remove duplicates from sorted list solution in c++ 
Cpp :: visual studio code terminal keeps closing c++ 
Cpp :: C/C++ loop for 
Cpp :: java to puthon converter 
Cpp :: largest subarray with zero sum 
Cpp :: Restart the computer in c++ after the default time (30) seconds. (Windows) 
Cpp :: unambiguous 
Cpp :: check if a string is a prefix of another c++ 
Cpp :: c++ get microseconds since epoch 
Cpp :: log base 10 c+_+ 
Cpp :: nand in cpp 
Cpp :: cpp module 42 
Cpp :: long, long long 32 bit or 8 bit in c++ 
Cpp :: grepper users assemble 
Cpp :: accepting string with space on same line C++ 
Cpp :: hackerearth questions siemens 
Cpp :: C++ std::ofstream class members 
Cpp :: fishes code in assignment expert 
Cpp :: command loop ctrl D c++ 
Cpp :: converting a string to lowercase inbuld function in cpp 
Cpp :: pop back innstring 
Cpp :: cpp practice questions 
Cpp :: operazioni aritmetiche c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =