Write a C program that reads 43 numbers of values between 1
Write a C# 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
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
using System;
class IntSum
{
static void Main()
{
int n=0;
Console.Write(\"Enter 43 numbers in the range 1-10: \");
int [] arr = new int[43]; //array to store numbers
int [] freqs = new int[10]; //array to store numbers frequency
for(int i=0; i<43; i++){ //itearate the loop for 43 times
try{
n = int.Parse(Console.ReadLine()); //read a number
}catch (FormatException){
Console.WriteLine(\"{0} is not an integer\", n); //if the input is not a number, then print an error
}
while(!(n>=1 && n<=10)){ //if entered numer is not in the range read again till it is in range 1-10
Console.WriteLine(\"Value not in the range. Try again..\");
try{
n = int.Parse(Console.ReadLine());
}catch (FormatException){
Console.WriteLine(\"{0} is not an integer\", n);
}
}
if(freqs[n-1]==0) //if number is in range, then add the frequency. For the first occurance, frequency will be set to 1
freqs[n-1] = 1;
else
freqs[n-1] = freqs[n-1] + 1; //increment frequency for each occurance
}
Console.WriteLine(\"Frequency distribution bar chart: \"); //print the chart
for(int i=0; i<10; i++){
int f = freqs[i]; //get the frequency corresponding to the number i
Console.Write(\"{0}\",(i+1));
for(int j=1; j<=f; j++){
Console.Write(\"*\"); //print frequency times of *
}
Console.WriteLine(\"\");
}
}
}
----------------------------------------------------------------------------
OUTPUT:

