using System; namespace PS2_Q3 { // // Problem Set 2, Question 3 // A Histogram Maker // Written by Paul Hudak // September 22, 2006 // class Class1 { // // Histogram Maker // [STAThread] static void Main(string[] args) { // initialize variables int x; int b0 = 0; int b1 = 0; int b2 = 0; int b3 = 0; int b4 = 0; int b5 = 0; int b6 = 0; int b7 = 0; int b8 = 0; int b9 = 0; // display welcome message Console.WriteLine("\nWelcome to the Histogram Maker!\n"); // get numbers from user and place in proper bin x = GetNumber(); while (x>=0) { if (x<=9) b0=b0+1; else if (x<=19) b1=b1+1; else if (x<=29) b2=b2+1; else if (x<=39) b3=b3+1; else if (x<=49) b4=b4+1; else if (x<=59) b5=b5+1; else if (x<=69) b6=b6+1; else if (x<=79) b7=b7+1; else if (x<=89) b8=b8+1; else if (x<=99) b9=b9+1; x = GetNumber(); } // Display histogram Console.WriteLine("\nHere is your Histogram:\n"); DisplayLine(b0, 0); DisplayLine(b1,10); DisplayLine(b2,20); DisplayLine(b3,30); DisplayLine(b4,40); DisplayLine(b5,50); DisplayLine(b6,60); DisplayLine(b7,70); DisplayLine(b8,80); DisplayLine(b9,90); 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(); } } }