कंप्यूटर प्रोग्रामिंग/ऐरे/स्विफ्ट

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

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

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

import Foundation

func buildC(size: Int) -> [Double]
{
    var c = [Double]()

    for index in 0...size
    {
        c.append(Double(index * 9) / 5 + 32)
    }
    
    return c
}

func buildF(size: Int) -> [Double]
{
    var f = [Double]()

    for index in 0...size 
    {
        f.append(Double((index - 32) * 5) / 9)
    }
    
    return f
}

func displayArray(name: String, array: inout [Double])
{
    for index in 0...array.count - 1
    {
        print(name + "[" + String(index) + "] = " + String(array[index]))
    }
}

func findTemperature(c: inout [Double], f: inout [Double])
{
    var temp: Int
    var size: Int
    
    size = minimum(value1:c.count, value2:f.count)
    repeat 
    {
        print("Enter a temperature between 0 and " + String((size - 1)))
        temp = Int(readLine(strippingNewline: true)!)!
    } while temp < 0 || temp > size - 1
    print(String(temp) + "° Celsius is " + String(c[temp]) + "° Fahrenheit")
    print(String(temp) + "° Fahrenheit is " + String(f[temp]) + "° Celsius")
}

func minimum(value1: Int, value2: Int) -> Int
{
    var result: Int
    
    if value1 < value2
    {
        result = value1
    }
    else
    {
        result = value2
    }
    
    return result
}

func main()
{
    var c = buildC(size:100)
    var f = buildF(size:212)
    displayArray(name:"C", array:&c)
    displayArray(name:"F", array:&f)
    findTemperature(c:&c, f:&f)
}

main()

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

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

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