Programming in C# Write a program that reads 43 numbers of values between 1 and
ID: 3925978 • Letter: P
Question
Programming in C#
Write a program that reads 43 numbers of values between 1 and 10 from a data file or keyboard. Display a frequency distribution bar chart of those numbers. 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.
The contents of the data file are as follows:
7 8 9 4 5 6 3 2 3 3 2 2 6 5 4 9 8 7 4 5 6 7 8 9 3 2 3 3 2 3 4 5 6 7 8 9 7 7 8 8 9 7 9 -1
Explanation / Answer
Here is the C# code for you:
using System.IO;
using System;
class Program
{
static void Main()
{
int []digits = new int[11];
for(int i = 0; i < 43; i++)
digits[i] = 0;
Console.WriteLine("Enter the digits followed by a sentinal value -1:");
int count = 0;
int num = Convert.ToInt32(Console.ReadLine());
while(num != -1 && count < 43)
{
if(num > 10)
{
Console.WriteLine("Invalid number.");
break;
}
digits[num]++;
count++;
num = Convert.ToInt32(Console.ReadLine());
}
for(int i = 1; i <= 10; i++)
{
for(int j = 0; j < digits[i]; j++)
Console.Write("* ");
Console.WriteLine();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.