Answer to Question #235754 in C# for Rancho

Question #235754

Create a program that will generate twenty (20) random numbers from the range 1 to 9. The twenty random numbers shall be stored in a generic list. Print a horizontal histogram, representing the occurrence of each digit. Display also the generated combinations. 2. Refer to the sample output given below.


(Sample Ouput)

Welcome to my GENERIC LIST PROGRAM

The 20 random numbers are: 3 4 1 7 8 5 6 3 4 9 3 5 1 3 4 2 8 9 5 9

Histogram:

1 **

2 *

3 ****

4 ***

5 ***

6 *

7 *

8 **

9 ***


1
Expert's answer
2021-09-10T18:51:26-0400
 class Program
    {
        const int AMOUNT_OF_RANDOM_NUMBERS = 20;

        static int GenerateRandomNumber(int min, int max)
        {
            return new Random().Next(min, max + 1);
        }

        static List<int> GenerateListOfRandomNumbers(int min, int max)
        {
            List<int> randomNumbers = new List<int>();


            for(int i = 0; i < AMOUNT_OF_RANDOM_NUMBERS; i++)
            {
                int randomNumber = GenerateRandomNumber(min, max);
                randomNumbers.Add(randomNumber);
            }


            return randomNumbers;
        }


        static List<int> RemoveDuplicates(List<int> numbers)
        {
            HashSet<int> uniqueNumbers = new HashSet<int>(numbers);
            return new List<int>(uniqueNumbers);
            
        }
        static int CountOccurrencesOfTheNumber(int number, List<int> numbers)
        {
            int occurrences = 0;


            for(int i = 0; i < numbers.Count; i++)
            {
                if (numbers[i] == number)
                    occurrences++;
            }


            return occurrences;
        }
        static List<int> GetOccurencesForNumbers(List<int> uniqueNumbers, List<int> randomNumbers)
        {
            List<int> occurencesOfNumbers = new List<int>();


            foreach(int number in uniqueNumbers)
            {
                int occurences = CountOccurrencesOfTheNumber(number, randomNumbers);
                occurencesOfNumbers.Add(occurences);
            }


            return occurencesOfNumbers;
        }
        static void Main()
        {
            List<int> randomNumbers = GenerateListOfRandomNumbers(1, 9);


            Console.WriteLine("Welcome to my GENERIC LIST PROGRAM");
            Console.Write("The 20 random numbers are: ");
            randomNumbers.ForEach(number => Console.Write(number + " "));


            List<int> uniqueNumbers = RemoveDuplicates(randomNumbers);
            uniqueNumbers.Sort();
            List<int> occurrencesOfNumbers = GetOccurencesForNumbers(uniqueNumbers, randomNumbers);


            Console.WriteLine("\n\nHistogram:");
            for (int i = 0; i < uniqueNumbers.Count; i++)
            {
                Console.Write(uniqueNumbers[i] + " ");
                for (int j = 0; j < occurrencesOfNumbers[i]; j++)
                    Console.Write('*');


                Console.WriteLine();
            }


            Console.ReadKey();
        }
    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS