using System; namespace PS5_Q1 { // // Problem Set 5, Question 1 // A Histogram Maker (from PS2, Q3) Using Arrays // Written by Paul Hudak // October 16, 2006 // class Class1 { // // Histogram Maker // [STAThread] static void Main(string[] args) { // initialize array // this can be done in three ways: // int[] bins = new int[10]; // this works b/c the default value is 0 // int[] bins = {0,0,0,0,0,0,0,0,0,0}; // here we make the array elements explicit int[] bins = new int[10]; // here we use a loop to initialize the elements for (int i=0; i<10; i=i+1) bins[i] = 0; // display welcome message Console.WriteLine("\nWelcome to the Histogram Maker!\n"); // get numbers from user and place in proper bin int x = GetNumber(); while (x>=0) { int b = x/10; bins[b] = bins[b]+1; x = GetNumber(); } // Display histogram Console.WriteLine("\nHere is your Histogram:\n"); for (int i=0; i<10; i=i+1) DisplayLine(bins[i], 10*i); Console.WriteLine(); } public static int GetNumber() { // get a single number from user Console.Write("Please type a number: "); int x = int.Parse(Console.ReadLine()); // if the number is 100 or greater, ask for a different one while (x>99) { Console.Write("Number out of range; please try again: "); x = int.Parse(Console.ReadLine()); } return x; } public static void DisplayLine(int num, int low) { // display bin range in a uniform field Console.Write("{0,2}-{1,2}: ", low, low+9); // write "num" asterisks while (num>0) { Console.Write("*"); num=num-1; } // go to next line Console.WriteLine(); } } }