Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Linq;

public class Test
{
    public static void Main()
    {
        const int NUMBERS = 1000;
        Random myRandom = new Random();
        int [] a = new int[NUMBERS];

        int i = 0;
        int max = int.MinValue, min = int.MaxValue, sum = 0;


       while (i < NUMBERS)
        {
          a[i] = myRandom.Next(1, 501);
          if (max < a[i]) max = a[i];
          if (min > a[i]) min = a[i];
          sum += a[i];
          ++i;
        }
        double avg = (double)sum / NUMBERS;

        Console.WriteLine("The minimum number is: {0}", min );
        Console.WriteLine("The maximum number is: {0}", max);
        Console.WriteLine("The average of these numbers is: {0}", avg);

        int x = Convert.ToInt32(Console.ReadLine());

       if(a.Contains(x))
       {
        Console.WriteLine("Your number was one of the original 1000!");
       }
       else
       {
        Console.WriteLine("Your number was not one of the original 1000");
       }

  }
}
Posted
Updated 27-Jul-15 9:23am
v2

1 solution

Why not simply use LINQ Min[^] and Max[^] methods.

[Addition]

Consider the following code
C#
int[] myarray = new int[5] { 3, 5, 9, 2, 5 };
System.Diagnostics.Debug.WriteLine(string.Format("Minimum is {0}", myarray.Min()));
System.Diagnostics.Debug.WriteLine(string.Format("Maximum is {0}", myarray.Max()));
System.Diagnostics.Debug.WriteLine(string.Format("And somewhere between is {0}", myarray.Average()));

The output is
Minimum is 2
Maximum is 9
And somewhere between is 4,8
 
Share this answer
 
v3
Comments
Member 11858958 27-Jul-15 15:55pm    
Only new to c sharp, could you give an example in relation to this particular task?
Wendelius 27-Jul-15 15:59pm    
Have a look at the updated solution.
Member 11858958 27-Jul-15 16:05pm    
Thanks for the advice Mika. I was wondering how these commands could be placed in different methods?
Wendelius 27-Jul-15 16:07pm    
I'm not sure if I understand your question. Min Max and Average are different methods. Internally they loop through the array and do the calculation. Was this what you were after?
Member 11858958 27-Jul-15 16:11pm    
I was hoping to incorporate statements such as these, by passing the array to different methods:

public static int find_min(int[] a)
{
int min = int.MaxValue;

for (int n = 0; n < a.Length; ++n)
{
if (min > a[n]) min = a[n];
}
return min;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900