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

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

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

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

def build_c(size)
    c = Array.new(size)
    for index in (0..size)
        c[index] = index * 9.0 / 5 + 32
    end
    return c
end

def build_f(size)
    f = Array.new(size)
    for index in (0..size)
        f[index] = (index - 32) * 5.0 / 9
    end
    return f
end

def display_array(name, array)
    for index in (0..array.size - 1)
        puts name + "[" + (index).to_s + "] = " + (array[index]).to_s
    end
end

def find_temperature(c, f)
    size = minimum(c.length, f.length)
    begin
        puts "Enter a temperature between 0 and " + ((size - 1)).to_s
        temp = gets.chomp.to_i
    end while (temp < 0 || temp > size - 1)
    puts (temp).to_s + "° Celsius is " + (c[temp]).to_s + "° Fahrenheit"
    puts (temp).to_s + "° Fahrenheit is " + (f[temp]).to_s + "° Celsius"
end

def minimum(value1, value2)
    if value1 < value2
        result = value1
    else
        result = value2
    end
        
    return result
end

def main()
    c = build_c(100)
    f = build_f(212)
    display_array("C", c)
    display_array("F", f)
    find_temperature(c, f)
end

main()

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

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

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