कंप्यूटर प्रोग्रामिंग/ऐरे/पीएचपी

विकिविश्वविद्यालय से

arrays.php[सम्पादन | स्रोत सम्पादित करें]

<?php

// This program uses arrays to display temperature conversion tables
// and temperature as an array subscript to find a given conversion.

function build_c($size)
{
    $c = array();

    for ($index = 0; $index <= $size; $index++)
    {
        $c[$index] = $index * 9 / 5 + 32;
    }
    
    return $c;
}

function build_f($size)
{
    $f = array();

    for ($index = 0; $index <= $size; $index++)
    {
        $f[$index] = ($index - 32) * 5 / 9;
    }
    
    return $f;
}

function display_array($name, $array)
{
    for ($index = 0; $index < count($array); $index++)
    {
        echo($name . "[" . $index . "] = " . $array[$index] ."\n");
    }
}

function find_temperature($c, $f)
{
    $size = minimum(count($c), count($f));
    do
    {
        echo("Enter a temperature between 1 and " . ($size - 1) . "\n");
        $temp = (int)readline();
    } while ($temp < 1 or $temp > $size - 1);
    echo($temp . "° Celsius is " . $c[$temp] . "° Fahrenheit\n");
    echo($temp . "° Fahrenheit is " . $f[$temp] . "° Celsius\n");
}

function minimum($value1, $value2)
{
    if($value1 < $value2)
    {
        $result = $value1;
    }
    else
    {
        $result = $value2;
    }
        
    return $result;
}

function main()
{
  $c = build_c(100);
  $f = build_f(212);
  display_array("C", $c);
  display_array("F", $f);
  find_temperature($c, $f);
}

main();

?>

कोशिश करो[सम्पादन | स्रोत सम्पादित करें]

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें[सम्पादन | स्रोत सम्पादित करें]