0

I need a programm, where I can type in numbers and in the end it gives me the highest number. Why doesn't it work like that? What do I need to change?

public class Program
{
    public static void Main()
    {
        double[] input = new double[12];
        for (int i = 1; i <= 12; i++)
        {
            Console.Write(" Type in {0} number:", i);
            input = [Convert.ToInt32(Console.ReadLine())];
        } 

        Console.WriteLine("The highest number is {0}", input.Max(element => Math.Abs(element)));

        Console.ReadKey();
    }
}
Dmitry
  • 13,797
  • 6
  • 32
  • 48
aha364636
  • 365
  • 5
  • 23

4 Answers4

1

You need to make it so its converting to double and also setting to each individual element

 input[i] = Convert.ToDouble(Console.ReadLine());

and then change this because arrray starts at 0

for (int i = 0; i <= 11; i++)
Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19
0

As @Ashad Shanto said you must use Convert.ToDouble and you must use input[i] instead of input. So your code should look like:

public class Program
{
    public static void Main()
    {
        double[] input = new double[12];
        for (int i = 0; i < 12; i++)
        {
            Console.Write(" Type in {0} number:", i);
            input[i] = [Convert.ToDouble(Console.ReadLine())];
        } 

        Console.WriteLine("The highest number is {0}", input.Max(element => Math.Abs(element)));

        Console.ReadKey();
    }
}
Vishal
  • 6,238
  • 10
  • 82
  • 158
  • There's no need to convert to double instead of int, unless non-integer input is actually expected. Of course, in that case there's also no need for the array to be `double[]` instead of `int[]`, so who really knows what the OP actually needs. :) – Peter Duniho Oct 30 '14 at 18:25
0

As @artokai mentioned you don't need to store all entered numbers.

Try the following:

  double heighest = Double.MinValue;
    for (int i = 0; i < 12; i++)
    {
        Console.Write(" Type in {0} number:", i);
        double input = (Convert.ToDouble(Console.ReadLine());
        if (input > heighest)
            heighest = input

    }
    Console.WriteLine("The highest number is {0}", highest);
TGlatzer
  • 5,815
  • 2
  • 25
  • 46
ToasteR
  • 886
  • 7
  • 20
0

Is the requirement to have a Double or Int? Anyways, you can just simple store the highest number each time a new number is entered by doing a simple comparison.

public static void Main()
{
    var currentNumber = 0;
    for (var i = 1; i <= 12; i++)
    {
        Console.Write(" Type in {0} number: ", i);

        var number = Console.ReadLine();
        int result;

        if (int.TryParse(number, out result))
        {
            if (currentNumber < result)
            {
                currentNumber = result;
            }   
        }
    }

    Console.WriteLine("The highest number is {0}", currentNumber);
    Console.ReadKey();
}
d.moncada
  • 16,900
  • 5
  • 53
  • 82