WeixiangYan/CodeTransOcean huggingface dataset:
Task: Check output device is a terminal

C: #include <unistd.h>   
#include <stdio.h>    

int main()
{
    puts(isatty(fileno(stdout))
          ? "stdout is tty"
          : "stdout is not tty");
    return 0;
}









COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal



C#: using System;

namespace CheckTerminal {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected);
        }
    }
}







COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal


C++: #if _WIN32
#include <io.h>
#define ISATTY _isatty
#define FILENO _fileno
#else
#include <unistd.h>
#define ISATTY isatty
#define FILENO fileno
#endif

#include <iostream>

int main() {
    if (ISATTY(FILENO(stdout))) {
        std::cout << "stdout is a tty\n";
    } else {
        std::cout << "stdout is not a tty\n";
    }

    return 0;
}








COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal







Python: from sys import stdout
if stdout.isatty():
    print 'The output device is a teletype. Or something like a teletype.'
else:
    print 'The output device isn\'t like a teletype.'



COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal








VB: Module Module1

    Sub Main()
        Console.WriteLine("Stdout is tty: {0}", Console.IsOutputRedirected)
    End Sub

End Module


COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal




Go: package main

import (
    "os"
    "fmt"
)

func main() {
    if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
        fmt.Println("Hello terminal")
    } else {
        fmt.Println("Who are you? You're not a terminal")
    }
}






COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Check output device is a terminal






PHP: if(posix_isatty(STDOUT)) {
    echo "The output device is a terminal".PHP_EOL;
} else {
    echo "The output device is NOT a terminal".PHP_EOL;
}
Task: Check output device is a terminal






PHP: if(posix_isatty(STDOUT)) {
    echo "The output device is a terminal".PHP_EOL;
} else {
    echo "The output device is NOT a terminal".PHP_EOL;
}






Ada: with Ada.Text_IO;          use Ada.Text_IO;
with Interfaces.C_Streams; use Interfaces.C_Streams;

procedure Test_tty is
begin
   if Isatty(Fileno(Stdout)) = 0 then
      Put_Line(Standard_Error, "stdout is not a tty.");
   else
      Put_Line(Standard_Error, "stdout is a tty.");
   end if;
end Test_tty;
Task: Check output device is a terminal






PHP: if(posix_isatty(STDOUT)) {
    echo "The output device is a terminal".PHP_EOL;
} else {
    echo "The output device is NOT a terminal".PHP_EOL;
}




COBOL:       
      
      
      
      
       identification division.
       program-id. istty.

       data division.
       working-storage section.
       01 rc usage binary-long.

       procedure division.
       sample-main.

       call "isatty" using by value 0 returning rc
       display "fd 0 tty: " rc

       call "isatty" using by value 1 returning rc
       display "fd 1 tty: " rc upon syserr

       call "isatty" using by value 2 returning rc
       display "fd 2 tty: " rc

       goback.
       end program istty.
Task: Averages_Median



C#: using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 };

            myArr = myArr.OrderBy(i => i).ToArray();
            

            int mid = myArr.Length / 2;
            double median;

            if (myArr.Length % 2 == 0)
            {
                
                median = (myArr[mid] + myArr[mid - 1]) / 2.0;
            }
            else
            {
                
                median = myArr[mid];
            }

            Console.WriteLine(median);
            Console.ReadLine();
        }
    }
}









Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median

C: #include <stdio.h>
#include <stdlib.h>

typedef struct floatList {
    float *list;
    int   size;
} *FloatList;

int floatcmp( const void *a, const void *b) {
    if (*(const float *)a < *(const float *)b) return -1;
    else return *(const float *)a > *(const float *)b;
}

float median( FloatList fl )
{
    qsort( fl->list, fl->size, sizeof(float), floatcmp);
    return 0.5 * ( fl->list[fl->size/2] + fl->list[(fl->size-1)/2]);
}

int main()
{
    static float floats1[] = { 5.1, 2.6, 6.2, 8.8, 4.6, 4.1 };
    static struct floatList flist1 = { floats1, sizeof(floats1)/sizeof(float) };

    static float floats2[] = { 5.1, 2.6, 8.8, 4.6, 4.1 };
    static struct floatList flist2 = { floats2, sizeof(floats2)/sizeof(float) };

    printf("flist1 median is %7.2f\n", median(&flist1)); 
    printf("flist2 median is %7.2f\n", median(&flist2)); 
    return 0;
}











Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median


C++: #include <algorithm>



template <typename Iterator>
double median(Iterator begin, Iterator end) {
  
  Iterator middle = begin + (end - begin) / 2;

  
  std::nth_element(begin, middle, end);

  if ((end - begin) % 2 != 0) { 
    return *middle;
  } else { 
    
    Iterator lower_middle = std::max_element(begin, middle);
    return (*middle + *lower_middle) / 2.0;
  }
}

#include <iostream>

int main() {
  double a[] = {4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2};
  double b[] = {4.1, 7.2, 1.7, 9.3, 4.4, 3.2};

  std::cout << median(a+0, a + sizeof(a)/sizeof(a[0])) << std::endl; 
  std::cout << median(b+0, b + sizeof(b)/sizeof(b[0])) << std::endl; 

  return 0;
}










Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    fmt.Println(median([]float64{3, 1, 4, 1}))    
    fmt.Println(median([]float64{3, 1, 4, 1, 5})) 
}

func median(a []float64) float64 {
    sort.Float64s(a)
    half := len(a) / 2
    m := a[half]
    if len(a)%2 == 0 {
        m = (m + a[half-1]) / 2
    }
    return m
}








Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median





Java: 
public static double median(List<Double> list) {
    Collections.sort(list);
    return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2;
}







Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median







Python: def median(aray):
    srtd = sorted(aray)
    alen = len(srtd)
    return 0.5*( srtd[(alen-1)//2] + srtd[alen//2])

a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)
a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)





Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median








VB: Private Function medianq(s As Variant) As Double
    Dim res As Double, tmp As Integer
    Dim l As Integer, k As Integer
    res = 0
    l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1)
        If l Then
            res = quick_select(s, k)
            If l Mod 2 = 0 Then
                tmp = quick_select(s, k + 1)
                res = (res + tmp) / 2
            End If
        End If
    medianq = res
End Function
Public Sub main2()
    s = [{4, 2, 3, 5, 1, 6}]
    Debug.Print medianq(s)
End Sub




Ada: with Ada.Text_IO, Ada.Float_Text_IO;

procedure FindMedian is

    f: array(1..10) of float := ( 4.4, 2.3, -1.7, 7.5, 6.6, 0.0, 1.9, 8.2, 9.3, 4.5 );
    min_idx: integer;
    min_val, median_val, swap: float;

begin
    for i in f'range loop
        min_idx := i;
        min_val := f(i);
        for j in i+1 .. f'last loop
            if f(j) < min_val then
                min_idx := j;
                min_val := f(j);
            end if;                
        end loop;
        swap := f(i); f(i) := f(min_idx); f(min_idx) := swap;
    end loop;      

    if f'length mod 2 /= 0 then
        median_val := f( f'length/2+1 );
    else
        median_val := ( f(f'length/2) + f(f'length/2+1) ) / 2.0;
    end if;
    
    Ada.Text_IO.Put( "Median value: " );
    Ada.Float_Text_IO.Put( median_val );
    Ada.Text_IO.New_line;    
end FindMedian;
Task: Averages_Median

C: #include <stdio.h>
#include <stdlib.h>

typedef struct floatList {
    float *list;
    int   size;
} *FloatList;

int floatcmp( const void *a, const void *b) {
    if (*(const float *)a < *(const float *)b) return -1;
    else return *(const float *)a > *(const float *)b;
}

float median( FloatList fl )
{
    qsort( fl->list, fl->size, sizeof(float), floatcmp);
    return 0.5 * ( fl->list[fl->size/2] + fl->list[(fl->size-1)/2]);
}

int main()
{
    static float floats1[] = { 5.1, 2.6, 6.2, 8.8, 4.6, 4.1 };
    static struct floatList flist1 = { floats1, sizeof(floats1)/sizeof(float) };

    static float floats2[] = { 5.1, 2.6, 8.8, 4.6, 4.1 };
    static struct floatList flist2 = { floats2, sizeof(floats2)/sizeof(float) };

    printf("flist1 median is %7.2f\n", median(&flist1)); 
    printf("flist2 median is %7.2f\n", median(&flist2)); 
    return 0;
}
Task: Averages_Median



C#: using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 };

            myArr = myArr.OrderBy(i => i).ToArray();
            

            int mid = myArr.Length / 2;
            double median;

            if (myArr.Length % 2 == 0)
            {
                
                median = (myArr[mid] + myArr[mid - 1]) / 2.0;
            }
            else
            {
                
                median = myArr[mid];
            }

            Console.WriteLine(median);
            Console.ReadLine();
        }
    }
}
Task: Averages_Median


C++: #include <algorithm>



template <typename Iterator>
double median(Iterator begin, Iterator end) {
  
  Iterator middle = begin + (end - begin) / 2;

  
  std::nth_element(begin, middle, end);

  if ((end - begin) % 2 != 0) { 
    return *middle;
  } else { 
    
    Iterator lower_middle = std::max_element(begin, middle);
    return (*middle + *lower_middle) / 2.0;
  }
}

#include <iostream>

int main() {
  double a[] = {4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2};
  double b[] = {4.1, 7.2, 1.7, 9.3, 4.4, 3.2};

  std::cout << median(a+0, a + sizeof(a)/sizeof(a[0])) << std::endl; 
  std::cout << median(b+0, b + sizeof(b)/sizeof(b[0])) << std::endl; 

  return 0;
}
Task: Averages_Median





Java: 
public static double median(List<Double> list) {
    Collections.sort(list);
    return (list.get(list.size() / 2) + list.get((list.size() - 1) / 2)) / 2;
}
Task: Averages_Median







Python: def median(aray):
    srtd = sorted(aray)
    alen = len(srtd)
    return 0.5*( srtd[(alen-1)//2] + srtd[alen//2])

a = (4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)
a = (4.1, 7.2, 1.7, 9.3, 4.4, 3.2)
print a, median(a)
Task: Averages_Median








VB: Private Function medianq(s As Variant) As Double
    Dim res As Double, tmp As Integer
    Dim l As Integer, k As Integer
    res = 0
    l = UBound(s): k = WorksheetFunction.Floor_Precise((l + 1) / 2, 1)
        If l Then
            res = quick_select(s, k)
            If l Mod 2 = 0 Then
                tmp = quick_select(s, k + 1)
                res = (res + tmp) / 2
            End If
        End If
    medianq = res
End Function
Public Sub main2()
    s = [{4, 2, 3, 5, 1, 6}]
    Debug.Print medianq(s)
End Sub
Task: Averages_Median




Go: package main

import (
    "fmt"
    "sort"
)

func main() {
    fmt.Println(median([]float64{3, 1, 4, 1}))    
    fmt.Println(median([]float64{3, 1, 4, 1, 5})) 
}

func median(a []float64) float64 {
    sort.Float64s(a)
    half := len(a) / 2
    m := a[half]
    if len(a)%2 == 0 {
        m = (m + a[half-1]) / 2
    }
    return m
}
Task: Averages_Median



C#: using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            double[] myArr = new double[] { 1, 5, 3, 6, 4, 2 };

            myArr = myArr.OrderBy(i => i).ToArray();
            

            int mid = myArr.Length / 2;
            double median;

            if (myArr.Length % 2 == 0)
            {
                
                median = (myArr[mid] + myArr[mid - 1]) / 2.0;
            }
            else
            {
                
                median = myArr[mid];
            }

            Console.WriteLine(median);
            Console.ReadLine();
        }
    }
}








Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median


C++: #include <algorithm>



template <typename Iterator>
double median(Iterator begin, Iterator end) {
  
  Iterator middle = begin + (end - begin) / 2;

  
  std::nth_element(begin, middle, end);

  if ((end - begin) % 2 != 0) { 
    return *middle;
  } else { 
    
    Iterator lower_middle = std::max_element(begin, middle);
    return (*middle + *lower_middle) / 2.0;
  }
}

#include <iostream>

int main() {
  double a[] = {4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2};
  double b[] = {4.1, 7.2, 1.7, 9.3, 4.4, 3.2};

  std::cout << median(a+0, a + sizeof(a)/sizeof(a[0])) << std::endl; 
  std::cout << median(b+0, b + sizeof(b)/sizeof(b[0])) << std::endl; 

  return 0;
}









Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test
Task: Averages_Median

C: #include <stdio.h>
#include <stdlib.h>

typedef struct floatList {
    float *list;
    int   size;
} *FloatList;

int floatcmp( const void *a, const void *b) {
    if (*(const float *)a < *(const float *)b) return -1;
    else return *(const float *)a > *(const float *)b;
}

float median( FloatList fl )
{
    qsort( fl->list, fl->size, sizeof(float), floatcmp);
    return 0.5 * ( fl->list[fl->size/2] + fl->list[(fl->size-1)/2]);
}

int main()
{
    static float floats1[] = { 5.1, 2.6, 6.2, 8.8, 4.6, 4.1 };
    static struct floatList flist1 = { floats1, sizeof(floats1)/sizeof(float) };

    static float floats2[] = { 5.1, 2.6, 8.8, 4.6, 4.1 };
    static struct floatList flist2 = { floats2, sizeof(floats2)/sizeof(float) };

    printf("flist1 median is %7.2f\n", median(&flist1)); 
    printf("flist2 median is %7.2f\n", median(&flist2)); 
    return 0;
}










Fortran: program Median_Test

  real            :: a(7) = (/ 4.1, 5.6, 7.2, 1.7, 9.3, 4.4, 3.2 /), &
                     b(6) = (/ 4.1, 7.2, 1.7, 9.3, 4.4, 3.2 /)

  print *, median(a)
  print *, median(b)

contains

  function median(a, found)
    real, dimension(:), intent(in) :: a
      
      
      
    logical, optional, intent(out) :: found
    real :: median

    integer :: l
    real, dimension(size(a,1)) :: ac

    if ( size(a,1) < 1 ) then
       if ( present(found) ) found = .false.
    else
       ac = a
       
       
       
       call sort(ac)

       l = size(a,1)
       if ( mod(l, 2) == 0 ) then
          median = (ac(l/2+1) + ac(l/2))/2.0
       else
          median = ac(l/2+1)
       end if

       if ( present(found) ) found = .true.
    end if

  end function median

end program Median_Test