Chapter 7 Arrays 243 6 7 class SumArray (Web design software)

Chapter 7 Arrays 243 6 7 class SumArray 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 int[] a = { 1,2, 3, 4,5,6, 7, 8,9,10 }; 13 int total = 0; 14 15 for ( int i = 0; i < a.Length; i++ ) 16 total += a[ i ]; 17 18 MessageBox.Show( "Total of array elements: " + total, 19 "Sum the elements of an array", 20 MessageBoxButtons.OK, MessageBoxIcon.Information ); 21 22 } // end Main 23 24 } // end class SumArray Fig. 7.4 Computing the sum of the elements of an array. (Part 2 of 2.) 7.4.3 Using Histograms to Display Array Data Graphically Many programs present data to users in a graphical manner. For example, numeric values often are displayed as bars in a bar chart. In such a chart, longer bars represent larger numeric values. One simple way to display numeric data graphically is with a histogram that shows each numeric value as a bar of asterisks (*). Our next application (Fig. 7.5) reads numbers from an array and graphs the information in the form of a bar chart, or histogram. The program displays each number followed by a bar consisting of a corresponding number of asterisks. The nested for loops (lines 18 24) append the bars to the string that will be displayed in the MessageBox. Note the loop continuation condition of the inner for structure on line 22 (j<=n[i]). Each time the program reaches the inner for structure, the loop counts from 1to n[i], using a value in array n to determine the final value of the control variable j and the number of asterisks to display. 1 // Fig. 7.6: Histogram.cs 2 // Using data to create a histogram. 3 4 using System; 5 using System.Windows.Forms; Fig. 7.5 Program that prints histograms. (Part 1 of 2.)
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Leave a Reply