Find the largest and smallest element in array

Here given code implementation process.

//C Program 
//Find the largest and smallest element in array
#include <stdio.h>

void find_min_max(int arr[],int size)
{
  
  if(size<=1)
  {
    return;
  }

  else{

    //Get first element of array
    int smallest = arr[0];
    int largest  = arr[0];
    
    for (int i = 0; i < size; i++)
    {
      //compare the array element values
      if(smallest > arr[i])
      {
        //When get a new small element
        smallest = arr[i];
      }
      if(largest < arr[i])
      {
        //When get a new larger element
        largest = arr[i];
      }
    }
    
    //Display result
    printf("Smallest : %d\n",smallest);
 
    printf("Largest  : %d\n",largest);
    
  }
}


int main()
{

  //Define collection of array elements
  int arr[] ={10,3,44,8,3,2,1,-1,6,8,9,4,5,7};
  
  //Get the size of array
  int size=sizeof(arr) / sizeof(arr[0]);

  find_min_max(arr,size);

  return 0;
  
}

Output

Smallest : -1
Largest  : 44
/*
  C++ Program
  Find the largest and smallest element in array
*/
#include<iostream>
using namespace std;

class MyArray {
  public:
    void find_min_max(int arr[], int size) {
      if (size <= 1) {
        return;
      } else {
        //Get first element of array
        int smallest = arr[0];
        int largest = arr[0];
        for (int i = 0; i < size; i++) {
          //compare the array element values

          if (smallest > arr[i]) {
            //When get a new small element
            smallest = arr[i];
          }
          if (largest < arr[i]) {
            //When get a new larger element
            largest = arr[i];
          }
        }
        //Display result

        cout << "Smallest : " << smallest << "\n";
        cout << "Largest : " << largest << "\n";
      }
    }
};
int main() {
  MyArray obj = MyArray();
  int arr[] = {
    10,
    3,
    44,
    8,
    3,
    2,
    1,
    -1,
    6,
    8,
    9,
    4,
    5,
    7
  };
  //Get the size of array
  int size = sizeof(arr) / sizeof(arr[0]);
  obj.find_min_max(arr, size);
  return 0;
}

Output

Smallest : -1
Largest : 44
/*
  Java Program
  Find the largest and smallest element in array
*/

public class MyArray {
 
  public void find_min_max(int []arr,int size)
  {
    if(size<=1)
    {
      return;
    }
    else
    {
      //Get first element of array
      int smallest = arr[0];
      int largest  = arr[0];
      
      for (int i = 0; i < size; i++)
      {
        //compare the array element values
        if(smallest > arr[i])
        {
          //When get a new small element
          smallest = arr[i];
        }
        if(largest < arr[i])
        {
          //When get a new larger element
          largest = arr[i];
        }
      }
      
      //Display result
      System.out.print("Smallest : "+smallest+"\n");
   
      System.out.print("Largest  : "+largest+"\n");
      
    }
  }
  public static void main(String[] args) {

    MyArray obj = new MyArray();

    //Define collection of array elements
    int []arr = {10,3,44,8,3,2,1,-1,6,8,9,4,5,7};
    
    //Get the size of array
    int size = arr.length;

    obj.find_min_max(arr,size);
  }
}

Output

Smallest : -1
Largest : 44
/*
  C# Program
  Find the largest and smallest element in array
*/
using System;

public class MyArray {
  public void find_min_max(int[] arr, int size) {
    if (size <= 1) {
      return;
    } else {
      //Get first element of array
      int smallest = arr[0];
      int largest = arr[0];
      for (int i = 0; i < size; i++) {
        //compare the array element values

        if (smallest > arr[i]) {
          //When get a new small element
          smallest = arr[i];
        }
        if (largest < arr[i]) {
          //When get a new larger element
          largest = arr[i];
        }
      }
      Console.Write("Smallest : " + smallest + "\n");
      Console.Write("Largest : " + largest + "\n");
    }
  }
  public static void Main(String[] args) {
    MyArray obj = new MyArray();
    int[]
    //Define collection of array elements
    arr = {
      10,
      3,
      44,
      8,
      3,
      2,
      1,
      -1,
      6,
      8,
      9,
      4,
      5,
      7
    };
    //Get the size of array
    int size = arr.Length;
    obj.find_min_max(arr, size);
  }
}

Output

Smallest : -1
Largest : 44
<?php
/*
  Php Program
  Find the largest and smallest element in array
*/
class MyArray {
  public  function find_min_max($arr, $size) {
    if ($size <= 1) {
      return;
    } else {
      //Get first element of array
      $smallest = $arr[0];
      $largest = $arr[0];
      for ($i = 0; $i < $size; $i++) {
        //compare the array element values

        if ($smallest > $arr[$i]) {
          //When get a new small element
          $smallest = $arr[$i];
        }
        if ($largest < $arr[$i]) {
          //When get a new larger element
          $largest = $arr[$i];
        }
      }
      //Display result

      echo("Smallest : ". $smallest ."\n");
      echo("Largest : ". $largest ."\n");
    }
  }
}

function main() {
  $obj = new MyArray();
  //Define collection of array elements
  $arr = array(10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7);
  //Get the size of array
  $size = count($arr);
  $obj->find_min_max($arr, $size);

}
main();

Output

Smallest : -1
Largest : 44
/*
  Node Js Program
  Find the largest and smallest element in array
*/
class MyArray {
  find_min_max(arr, size) {
    if (size <= 1) {
      return;
    } else {
      //Get first element of array
      var smallest = arr[0];
      var largest = arr[0];
      for (var i = 0; i < size; i++) {
        //compare the array element values

        if (smallest > arr[i]) {
          //When get a new small element
          smallest = arr[i];
        }

        if (largest < arr[i]) {
          //When get a new larger element
          largest = arr[i];
        }
      }

      //Display result

      process.stdout.write("Smallest : " + smallest + "\n");
      process.stdout.write("Largest : " + largest + "\n");
    }
  }
}

function main(args) {
  var obj = new MyArray();
  //Define collection of array elements
  var arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7];
  //Get the size of array
  var size = arr.length;
  obj.find_min_max(arr, size);
}

main();

Output

Smallest : -1
Largest : 44
# Python 3 Program
# Find the largest and smallest element in array
class MyArray :
  def find_min_max(self, arr, size) :
    if (size <= 1) :
      return
    else :
      smallest = arr[0]
      largest = arr[0]
      i = 0
      while (i < size) :
        # compare the array element values
        if (smallest > arr[i]) :
          # When get a new small element
          smallest = arr[i]
        
        if (largest < arr[i]) :
          # When get a new larger element
          largest = arr[i]
        
        i += 1
      
      print("Smallest : ", smallest ,"\n", end = "")
      print("Largest : ", largest ,"\n", end = "")
    
  

def main() :
  obj = MyArray()
  # Define collection of array elements
  arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7]
  # Get the size of array
  size = len(arr)
  obj.find_min_max(arr, size)


if __name__ == "__main__":
  main()

Output

Smallest :  -1
Largest :  44
# Ruby Program
# Find the largest and smallest element in array
class MyArray 
  def find_min_max(arr, size) 
    if (size <= 1) 
      return
    else 
      smallest = arr[0]
      largest = arr[0]
      i = 0
      while (i < size) 
         # compare the array element values

        if (smallest > arr[i]) 
           # When get a new small element
          smallest = arr[i]
        end
        if (largest < arr[i]) 
           # When get a new larger element
          largest = arr[i]
        end
        i += 1
      end
      print("Smallest  : ", smallest ,"\n")
      print("Largest  : " , largest ,"\n")
    end
  end
end
def main() 
  obj = MyArray.new()
   # Define collection of array elements
  arr = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7]
   # Get the size of array
  size = arr.length
  obj.find_min_max(arr, size)
end
main()

Output

Smallest  : -1
Largest  : 44
/*
  Scala Program
  Find the largest and smallest element in array
*/
class MyArray {
  def find_min_max(arr: Array[Int], size: Int): Unit = {
    if (size <= 1) {
      return;
    } else {
      var smallest: Int = arr(0);
      var largest: Int = arr(0);
      var i: Int = 0;
      while (i < size) {
        //compare the array element values

        if (smallest > arr(i)) {
          //When get a new small element
          smallest = arr(i);
        }
        if (largest < arr(i)) {
          //When get a new larger element
          largest = arr(i);
        }
        i += 1;
      }
      print("Smallest : " + smallest + "\n");
      print("Largest : " + largest + "\n");
    }
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val obj: MyArray = new MyArray();

    //Define collection of array elements
    val arr: Array[Int] = Array(10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7);

    //Get the size of array
    val size: Int = arr.length;
    obj.find_min_max(arr, size);
  }
}

Output

Smallest : -1
Largest : 44
/*
  Swift Program
  Find the largest and smallest element in array
*/
class MyArray {
  func find_min_max(_ arr: [Int], _ size: Int) {
    if (size <= 1) {
      return;
    } else {
      var smallest: Int = arr[0];
      var largest: Int = arr[0];
      var i: Int = 0;
      while (i < size) {
        //compare the array element values

        if (smallest > arr[i]) {
          //When get a new small element
          smallest = arr[i];
        }
        if (largest < arr[i]) {
          //When get a new larger element
          largest = arr[i];
        }
        i += 1;
      }
      print("Smallest : ", smallest ,"\n", terminator: "");
      print("Largest : ", largest ,"\n", terminator: "");
    }
  }
}
func main() {
  let obj: MyArray = MyArray();
  //Define collection of array elements
  let arr: [Int] = [10, 3, 44, 8, 3, 2, 1, -1, 6, 8, 9, 4, 5, 7];
  //Get the size of array
  let size: Int = arr.count;
  obj.find_min_max(arr, size);
}
main();

Output

Smallest :  -1
Largest :  44


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved