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

public class Test
{
	public static void Main()
	{
		Random myRandom = new Random(1, 1001);
        int A = new int[1000];
        int i = 0;

        While (i < 1001) 
        
        A[i] = myRandom.Next(1, 501);
        i ++;
        }
        
		int output1 = Math.Min(i); 
		Console.WriteLine("The minimum number is: " + output1);
		
		int output2 = Math.Max(i);
		Console.WriteLine("The maximum number is: " + output2);
		
		int output3 = Average(params int[] A);
		Console.WriteLine("The average of these numbers is: " + output3);
    
        }
	public static int Math.Min(int i) //To calculate the minimum//
	{
    	        int answer = i;
      	        return answer;
	}
	
	public static int Math.Max(int i) //To calculate the maximum//
	{
		int answer = i;
		return answer;
	}
	
	public decimal Average(params int[] A) //To calculate the average//
        {
    	 	
                int sum = Sum(A);
                decimal result = (decimal)sum / A.Length;
                return result;
     
        }
	}

}
Posted
Updated 23-Jul-15 10:26am
v5
Comments
[no name] 23-Jul-15 16:16pm    
Just dumping a bunch of unreadable unformatted code into a posting is not a question.

You possibly meant
C#
using System;

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);

  }
}



Please note you actually don't need to store the extracted numbers in the array in order to compute minimum, maximum and average.


[Update]
Quote:
Thanks for the advice, and the solution, really helpful. I was wondering how I would go about putting the commands for finding the minimum, maximum and average of the numbers in different methods?
Then the essential modification is you need to pass the array to the relevant method, e.g.

C#
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;
}


[/Update]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 23-Jul-15 22:39pm    
Amazing question. :-)
—SA
CPallini 24-Jul-15 2:41am    
See my updated soloution.
By the way, you are welcome.
Sergey Alexandrovich Kryukov 23-Jul-15 22:39pm    
5ed.
—SA
CPallini 24-Jul-15 2:44am    
Thank you, Sergey.
Member 11858958 25-Jul-15 5:06am    
What would need to be changed or omitted from the original solution in order to incorporate the recent update?
C#
using System;

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);

  }
    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;
    }

    public static int find_max(int[] a)
    {
        int max = int.MinValue;
        for (int n = 0; n < a.Length; ++n)
        {
        if (max < a[n]) max = a[n];
        }
        return max;
    }

    public static double find_sum(int[] a)
    {
        sum = 0;
        double avg = (double)sum / NUMBERS;
        for (int n = 0; n < a.Length; ++n)
        {
            sum += a[n];
        }
        return avg;
    }
}
 
Share this answer
 
C#
int average = 0;
int largestNumber = 0;
int smallestNumber = int.MaxValue;
int _avr = 0;
int index = 0;

while (index < 1000)
{
    byte[] ba = new byte[4];
    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
    rng.GetBytes(ba);
    int n2 = BitConverter.ToInt32(ba, 0);

    Random rd = new Random(n2);
    int n = rd.Next(1, 501);

    if (n > largestNumber)
        largestNumber = n;
    if (n < smallestNumber)
        smallestNumber = n;
    _avr = _avr + n;

    index++;
}

average = _avr / 1000;
 
Share this answer
 

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