सामग्री पर जाएँ

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

विकिविश्वविद्यालय से
// This program uses arrays to display temperature conversion tables
// and temperature as an array subscript to find a given conversion.

using System;

public class MainClass
{
    public static void Main(String[] args)
    {
        double[] c;
        double[] f;
        
        c = BuildC(100);
        f = BuildF(212);
        DisplayArray("C", c);
        DisplayArray("F", f);
        FindTemperature(c, f);
    }

    private static double[] BuildC(int size)
    {
        double[] c = new double[size + 1];
        int index;
        
        for (index = 0; index <= size; index += 1)
        {
            c[index] = (double) index * 9 / 5 + 32;
        }
        
        return c;
    }

    private static double[] BuildF(int size)
    {
        double[] f = new double[size + 1];
        int index;
        
        for (index = 0; index <= size; index += 1)
        {
            f[index] = (double) (index - 32) * 5 / 9;
        }
        
        return f;
    }

    private static void DisplayArray(string name, double[] array)
    {
        int index;
        
        for (index = 0 ; index <= array.Length - 1 ; index += 1)
        {
            Console.WriteLine(name + "[" + index + "] = " + array[index]);
        }
    }

    private static void FindTemperature(double[] c, double[] f)
    {
        int temp;
        int size;
        
        size = Minimum(c.Length, f.Length);
        do
        {
            Console.WriteLine("Enter a temperature between 0 and " + (size - 1));
            temp = Convert.ToInt32(Console.ReadLine());
        } while (temp < 0 || temp > size - 1);
        Console.WriteLine(temp.ToString() + "° Celsius is " + c[temp] + "° Fahrenheit");
        Console.WriteLine(temp.ToString() + "° Fahrenheit is " + f[temp] + "° Celsius");
    }

    private static int Minimum(int value1, int value2)
    {
        int result;
        
        if (value1 < value2)
        {
            result = value1;
        }
        else
        {
            result = value2;
        }
        
        return result;
    }
}

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