C programming from problem analysis to program design

This preview shows page 10 - 23 out of 39 pages.

C# Programming: From Problem Analysis to Program Design 10 Array Initializers Compile-time initialization: General form of initialization follows: type[ ] identifier = new type[ ] {value1, value2, … valueN}; Values are separated by commas Values must be assignment compatible to the element type Implicit conversion from int to double Declare and initialize elements in one step
Image of page 10

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 11 Array Initializers ( continued ) Array length determined by number of initialization values placed inside curly braces Examples int [ ] anArray = {100, 200, 400, 600}; char [ ] grade = new char [ ] {'A', 'B', 'C', 'D', 'F'}; double [ ] depth = new double [2] {2.5, 3}; No length specifier is required
Image of page 11
C# Programming: From Problem Analysis to Program Design 12 Array Access Specify which element to access by suffixing the identifier with an index enclosed in square brackets score[0] = 100; Length – special property of Array class Last valid index is always the length of the array minus one
Image of page 12

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 13 Example 7-6: Create and Use an Array int total = 0; double avg, distance; string inValue; int [ ] score = new int [10]; for ( int i = 0; i < score.Length ; i++) { Console.Write("Enter Score{0}: ", i + 1); inValue = Console.ReadLine( ); if ( int .TryParse(inValue, out score[i] ) == false ) Console.WriteLine("Invalid data entered - " + "0 stored in array"); }
Image of page 13
C# Programming: From Problem Analysis to Program Design 14          // Values are summed for ( int i = 0; i < score.Length ; i++) { total += score[i] ; } avg = ( double ) total / score.Length ; Console.WriteLine("Average: {0}", avg.ToString("F0")); Console.WriteLine( ); Example 7-6 Create and Use an Array
Image of page 14

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 15 foreach Loop with Arrays Used to iterate through an array Read-only access General format foreach (type identifier in expression) statement ; Identifier is the iteration variable Expression is the array Type should match the array type
Image of page 15
C# Programming: From Problem Analysis to Program Design 16 Using foreach string [ ] color = {"red", "green", "blue"}; foreach ( string val in color) Console.WriteLine (val); Iteration variable, val represents a different array element with each loop iteration No need to increment a counter (for an index) Displays red, blue, and green on separate lines
Image of page 16

Subscribe to view the full document.

Using foreach with Arrays ( continued ) int total = 0; double avg; foreach ( int val in score) { total += val; } Console.WriteLine("Total: " + total); avg = ( double )total / scoreCnt; Console.WriteLine("Average: " + avg.ToString("F0")); C# Programming: From Problem Analysis to Program Design 17 val is the iteration variable. It represents a different array element with each loop iteration.
Image of page 17
C# Programming: From Problem Analysis to Program Design 18 Array Class Base array class More power is available with minimal programming Methods Copy ( ), Clear ( ), Sort( ), IndexOf ( ), GetValue ( )
Image of page 18

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 19 Table 7-1 System.Array methods
Image of page 19
C# Programming: From Problem Analysis to Program Design 20 Table 7-1 System.Array methods ( continued )
Image of page 20

Subscribe to view the full document.

C# Programming: From Problem Analysis to Program Design 21 Table 7-1 System.Array methods
Image of page 21
C# Programming: From Problem Analysis to Program Design 22 Table 7-1 System.Array methods
Image of page 22

Subscribe to view the full document.

Image of page 23
  • Winter '17
  • Qizhi Dai
  • Program design, Array

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern