Contents
出力結果
3
プログラム例
C
#include <stdio.h> static int maxValue(const int* array, int size) { int i; int max; max = array[0]; for (i = 1; i < size/sizeof(array[0]); ++i) { if (max < array[i]) { max = array[i]; } } return max; } int main(void) { const int array[] = {3, 1, 2}; printf("%i\n", maxValue(array, sizeof(array)) ); }
C++
#include <iostream> using namespace std; int main() { int array []= {3, 1, 2}; int largest = array[0] ; for ( int i=1; i < sizeof(array)/sizeof(array[0]); ++i ) { if ( array[i] > largest ) { largest = array[i] ; } } cout << largest << '\n' ; return 0; }
Java
public class Max { public static void main(String[] args) { int[] myList = {3, 1, 2}; int max = getMax(myList); System.out.println(max); } public static int getMax(int[] inputArray){ int maxValue = inputArray[0]; for(int i=1;i<inputArray.length;i++){ if(inputArray[i] > maxValue){ maxValue = inputArray[i]; } } return maxValue; } }
JavaScript
let array = [3, 1, 2] console.log(Math.max(...array))
Perl
use List::Util 'max'; my @array = (3, 1, 2); print max(@array);
PHP
<?php $array = [3, 1, 2]; print(max($array));
Python
array = [3, 1, 2] print(max(array))
Ruby
array = [3, 1, 2] print(array.max)
Shell
#!/bin/sh array=(3 1 2) max=${array[0]} for i in ${array[@]} do if [ ${i} -gt $max ]; then max=${i} fi done echo $max;