Answer to Question #259279 in C# for Kayla

Question #259279

Write a program that allows any number of values between 0 and 10 to be entered. When the user stops entering values, display a frequency distribution bar chart. Use asterisks to show the number of times each value was entered. If a given number is not entered, no asterisks should appear on that line. Your application should display error messages if a value outside the acceptable range is entered or if a non-numeric character is entered.

1
Expert's answer
2021-10-31T12:01:41-0400
using System;
using System.Collections.Generic;


namespace App
{






    class Program
    {


        static void Main(string[] args)
        {
            
            int[] counterValues = new int[11];




            int n = -1;
            while (n <=0)
            {
                Console.Write("Enter number of values >0: ");
                if (!int.TryParse(Console.ReadLine(), out n) || n <= 0)
                {
                    Console.WriteLine("Wrong number.");
                    n = -1;
                }
            }
            int[] values = new int[n];


            for (int i = 0; i < n; i++) {
                int value = -1;
                while (value < 0 || value > 10)
                {
                    Console.Write("Enter value between 0 and 10: ");
                    if (!int.TryParse(Console.ReadLine(), out value) || value < 0 || value > 10)
                    {
                        Console.WriteLine("Wrong value.");
                        value = -1;
                    }
                }
                values[i] = value;
            }
            




            for (int i = 0; i < values.Length; i++)
            {
                counterValues[values[i]]++;


            }


            for (int  i = 0; i <=10; i++)
            {
                Console.Write("{0} {1} ", i, counterValues[i]);
                for (int j = 0; j < counterValues[i]; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }




            Console.ReadLine();
        }
    }
}

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
APPROVED BY CLIENTS