简体   繁体   中英

Total sum/average of random numbers in c#

I am new to programming and i would be delighted if someone could help me with the following question: "Write a program that randomly selects integers whose values range from 0 to 9. The program will calculate the average of the random numbers and print how many of the random numbers are larger than the average. The program's user will specify how many numbers will be randomly generated". How do you get the total sum of random numbers so you can get the average? This is what i got so far:

int ChosenRandom;

Console.Write("Choose a number between 0-10; ");  

ChosenRandom = int.Parse(Console.ReadLine()); 

Random rnd = new Random();

int RandomNumber = rnd.Next(0, 10);

for (int i = 0; i < ChosenRandom; i++)

{
    Console.WriteLine("Random numbers: " + rnd.Next(0, 10));
}

int TotalRandom;

TotalRandom = ChosenRandom + (RandomNumber); 

Console.WriteLine("Total is:" + TotalRandom);

int avr;

avr = TotalRandom / ChosenRandom;     

Console.WriteLine("Average is: " + avr);

if (ChosenRandom > avr)    
{
    Console.WriteLine("Numbers larger than average" + ChosenRandom);
}

else   
{
    Console.WriteLine("All numbers under average");
}

simplest way is by using arrays,

  1. store the numbers in an array while generating them

  2. use the array elements to find the total and average

  3. traverse through array comparing each element with that of average

Check if this solution helps for you.

I have used linq to create average and find all numbers above the 'average'.

using System;
using System.Linq;

namespace Test
{
    class Program
    {
        static void Main()
        {
            int chosenRandom;
            Console.WriteLine("Choose a number between 0-10");
            chosenRandom = int.Parse(Console.ReadLine());

            Random rand = new Random();
            double[] randomNumbers = new double[chosenRandom];
            for(int i = 0; i < chosenRandom; i++)
            {
                Console.WriteLine("Random numbers: " + (randomNumbers[i] = rand.Next(0, 10)));
            }

            double average = randomNumbers.Average(t => t);

            var numbersAboveAverage = randomNumbers.Where(t => t > average);
            Console.WriteLine("Average of all random numbers - {0}", average);

            foreach(var number in numbersAboveAverage)
                Console.WriteLine(number);

        }
    }
}

Your program looks good. But, you understood the question in a wrong way! The question says that the value of random integers should be 0-9. Not the no of random numbers. The number of random numbers can be of any value as given by user.

Please find below the complete implementation.

    class Program
{
    static void Main(string[] args)
    {
        //Step 1. Get the no of random numbers (n) to be generated from user. 
        Console.WriteLine("Enter the no of Random numbers: ");
        int n = int.Parse(Console.ReadLine());  

        //Step 2. Generate 'n' no of random numbers with values rangeing from 0 to 9 and save them in an array.
        Random rand = new Random();
        int[] randCollection = new int[n];  
        for (int i = 0; i < n; i++)
        {
           randCollection[i] = rand.Next(9);
           Console.WriteLine("Random No {0} = {1}", i + 1, randCollection[i]); 
        }

        //Step 3. Compute Average
        double average = randCollection.Average();
        Console.WriteLine("Average = {0}", average); 

        //Step 4. Find out how many numbers in the array are greated than the average. 
        int count = 0; 
        foreach(int i in randCollection){
            if (i > average) count++; 
        }
        Console.WriteLine("No of random numbers above their average = {0}", count);
        Console.ReadLine(); 
    }
}

Hope this helps. Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM