Programming Fundamentals/Arrays/C Sharp: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Creating |
(No difference)
|
Revision as of 14:26, 18 February 2017
arrays.cs
// 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 = new double[101];
double[] f = new double[213];
BuildC(c);
BuildF(f);
DisplayArray("C", c);
DisplayArray("F", f);
FindTemperature(c, f);
}
private static void BuildC(double[] c)
{
int index;
for (index = 0 ; index <= c.Length - 1 ; index += 1)
{
c[index] = (double) index * 9 / 5 + 32;
}
}
private static void BuildF(double[] f)
{
int index;
for (index = 0 ; index <= f.Length - 1 ; index += 1)
{
f[index] = (double) (index - 32) * 5 / 9;
}
}
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 = GetMinimumSize(c, f);
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 GetMinimumSize(double[] c, double[] f)
{
int size;
size = c.Length;
if (size > f.Length)
{
size = f.Length;
}
return size;
}
}
Try It
Copy and paste the code above into one of the following free online development environments or use your own C Sharp compiler / interpreter / IDE.