1. Trang chủ
  2. » Giáo án - Bài giảng

C#: CHương 7

52 203 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 52
Dung lượng 412,5 KB

Nội dung

 2002 Prentice Hall. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays 7.4.1 Allocating an Array and Initializing Its Elements 7.4.2 Totaling the Elements of an Array 7.4.5 Using Arrays to Analyze Survey Results 7.4.3 Using Histograms to Display Array Data Graphically 7.4.4 Using the Elements of an Array as Counters 7.4.5 Using Arrays to Analyze Survey Results 7.5 Passing Arrays to Methods 7.6 Passing Arrays by Value and by Reference 7.7 Sorting Arrays 7.8 Searching Arrays: Linear Search and Binary Search 7.8.1 Searching an Array with Linear Search 7.8.2 Searching a Sorted Array with Binary Search 7.9 Multiple-Subscripted Arrays 7.10 for/each Repetition Structure  2002 Prentice Hall. All rights reserved. 2 7.1 Introduction • Data structures – Consist of data items of the same type – Static: remain the same size once created  2002 Prentice Hall. All rights reserved. 3 7.2 Arrays • A group of contiguous memory locations – Same name – Same type • Refer to particular element in the array by position number • Can refer to any element by giving the name of the array followed by the position number (subscript) of the element in square brackets ([]) • First element is the zeroth element – First element of array c is c[ 0 ]  2002 Prentice Hall. All rights reserved. 4 7.2 Arrays Fig. 7.1 A 12-element array. -45 6 0 72 1543 -89 0 62 -3 1 6453 -78c[ 11 ] c[ 10 ] c[ 9 ] c[ 8] c[ 7 ] c[ 4 ] c[ 3 ] c[ 2 ] c[ 1 ] c[ 0 ] c[ 6 ] c[ 5 ] Position number (index or subscript) of the element within array c Name of array (Note that all elements of this array have the same name, c)  2002 Prentice Hall. All rights reserved. 5 7.2 Arrays Operators Associativity Type () [] . ++ -- left to right highest (unary postfix) ++ -- + - ! ( type ) right to left unary (unary prefix) * / % left to right multiplicative + - left to right additive < <= > >= left to right relational == != left to right equality & left to right boolean logical AND ^ left to right boolean logical exclusive OR | left to right boolean logical inclusive OR && left to right logical AND || left to right logical OR ?: right to left conditional = += -= *= /= %= right to left assignment Fig. 7.2 Precedence and assoc iativity of the operators discussed so far.  2002 Prentice Hall. All rights reserved. 6 7.3 Declaring and Allocating Arrays • Programmer specifies the type of the elements of the array • new operator to allocate dynamically the number of elements in the array • Array declarations and initializations need not be in the same statement • In arrays of value types, each element contains one value of the declared type • In arrays of reference types, every element of the array is a reference to an object of the data type of the array  2002 Prentice Hall. All rights reserved. 7 7.3.1Allocating an Array and Initializing Its Elements • Arrays can be allocated using the word new to specify how many elements the array should hold • Arrays can be initialized with initializer lists – Allocate space for the array – number of elements in initializer list determines the size of array – Elements in array are initialized with the values in the initializer list  2002 Prentice Hall. All rights reserved. Outline 8 InitArray.cs 1 // Fig 7.3: InitArray.cs 2 // Different ways of initializing arrays. 3 4 using System; 5 using System.Windows.Forms; 6 7 class InitArray 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 string output = ""; 13 14 int[] x; // declare reference to an array 15 x = new int[ 10 ]; // dynamically allocate array and set 16 // default values 17 18 // initializer list specifies number of elements 19 // and value of each element 20 int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; 21 22 const int ARRAY_SIZE = 10; // named constant 23 int[] z; // reference to int array 24 25 // allocate array of ARRAY_SIZE (i.e., 10) elements 26 z = new int[ ARRAY_SIZE ]; 27 28 // set the values in the array 29 for ( int i = 0; i < z.Length; i++ ) 30 z[ i ] = 2 + 2 * i; 31 32 output += "Subscript\tArray x\tArray y\tArray z\n"; 33 Declare an integer array x Allocate x to be of size 10 Declare an integer array y and initialize it with values Declare a constant ARRAY_SIZE Declare an integer array z Initialize z to be of size ARRAY_SIZE Initialize the elements in z using a for loop  2002 Prentice Hall. All rights reserved. Outline 9 InitArray.cs Program Output 34 // output values for each array 35 for ( int i = 0; i < ARRAY_SIZE; i++ ) 36 output += i + "\t" + x[ i ] + "\t" + y[ i ] + 37 "\t" + z[ i ] + "\n"; 38 39 MessageBox.Show( output, 40 "Initializing an array of int values", 41 MessageBoxButtons.OK, MessageBoxIcon.Information ); 42 43 } // end Main 44 45 } // end class InitArray Add values in the arrays to output  2002 Prentice Hall. All rights reserved. Outline 10 SumArray.cs Program Output 1 // Fig. 7.4: SumArray.cs 2 // Computing the sum of the elements in an array. 3 4 using System; 5 using System.Windows.Forms; 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 Declare integer array a and initialize it Total the contents of array a [...]... dieLabel6 ); DisplayDie( dieLabel7 ); been rolled DisplayDie( dieLabel8 ); DisplayDie( dieLabel9 ); DisplayDie( dieLabel10 ); DisplayDie( dieLabel11 ); DisplayDie( dieLabel12 ); double total = 0; for ( int i = 1; i < 7; i++ ) total += frequency[ i ]; © 2002 Prentice Hall All rights reserved 15 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 displayTextBox.Text = "Face\tFrequency\tPercent\n";... " + "reference by reference"; outputLabel.Text += "\n\nContents of secondArray " + "before calling SecondDouble:\n\t"; © 2002 Prentice Hall All rights reserved 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 Outline // print contents of secondArray before method call for ( int i = 0; i < secondArray.Length; i++ ) outputLabel.Text += secondArray[... " + e; © 2002 Prentice Hall All rights reserved 22 70 71 72 73 74 75 Outline e *= 2; outputLabel.Text += "\nvalue calculated in ModifyElement: " + e; } } Multiply argument by two PassArray.cs This does not change value of element in original array, because the element was passed by value Program Output © 2002 Prentice Hall All rights reserved 23 24 7. 6 Passing Arrays by Value and by Reference • Variables...1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Outline // Fig 7. 5: Histogram.cs // Using data to create a histogram using System; using System.Windows.Forms; Declare an integer array n and initialize it class Histogram { // main entry point for application static void Main( string[] args ) { int[] n = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; string output... + "before calling FirstDouble:\n\t"; 26 © 2002 Prentice Hall All rights reserved 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 Outline // print contents of firstArray for ( int i = 0; i < firstArray.Length; i++ ) outputLabel.Text += firstArray[ i ] + " "; 27 ArrayReferenceTe st.cs // pass reference firstArray by value to FirstDouble FirstDouble(... ]++; } } // end class RollDie © 2002 Prentice Hall All rights reserved 16 Outline RollDie.cs Program Output © 2002 Prentice Hall All rights reserved 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 // Fig 7. 7: StudentPoll.cs // A student poll program using System; using System.Windows.Forms; Outline Declare and initialize integer array responses StudentPoll.cs... dieLabel6; dieLabel7; dieLabel8; dieLabel9; dieLabel10; dieLabel11; dieLabel12; Create a Random object private System.ComponentModel.Container components = Random randomNumber = new Random(); int[] frequency = new int[ 7 ]; Declare an integer array frequency and allocate it enough memory to hold null; 7 integers © 2002 Prentice Hall All rights reserved 14 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51... the answer that array frequency class StudentPoll { // main entry point for application static void Main( string[] args ) { int[] responses = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; int[] frequency = new int[ 11 ]; string output = ""; Output the number of times each response appeared // increment the frequency for each... All rights reserved 19 20 7. 5 Passing Arrays to Methods • Pass arrays as arguments to methods by specifying the name of the array (no brackets) • Arrays are passed by reference • Individual array elements are passed by value © 2002 Prentice Hall All rights reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 Outline // Fig 7. 8: PassArray.cs // Passing... set to null – May lead to methods modifying variable values and references in ways that are not desired © 2002 Prentice Hall All rights reserved 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Fig 7. 9: ArrayReferenceTest.cs // Testing the effects of passing array references // by value and by reference using System; Declare and initialize using System.Drawing; . rights reserved. 1 Chapter 7 - Arrays Outline 7. 1 Introduction 7. 2 Arrays 7. 3 Declaring and Allocating Arrays 7. 4 Examples Using Arrays 7. 4.1 Allocating an Array. Counters 7. 4.5 Using Arrays to Analyze Survey Results 7. 5 Passing Arrays to Methods 7. 6 Passing Arrays by Value and by Reference 7. 7 Sorting Arrays 7. 8 Searching

Ngày đăng: 03/09/2013, 03:10

Xem thêm

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN