1 / 12

Arrays

Arrays. Arrays are data structures consisting of data items of the same type. Arrays are ‘static’ entities, in that they remain the same size once they are created MC++ provides manages arrays Managed arrays are actually objects of System::Array class. Array. Declaring and allocating arrays

shea
Télécharger la présentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays • Arrays are data structures consisting of data items of the same type. • Arrays are ‘static’ entities, in that they remain the same size once they are created • MC++ provides manages arrays • Managed arrays are actually objects of System::Array class

  2. Array • Declaring and allocating arrays • int c __gc[] ; • // declare a managed array • c= new int __gc[12]; • // allocate space for array • Arrays are allocated with new because arrays are objects and all objects must be created with new • Managed types are garbage collected and managed by the CLR part of the .NET framework that executes MC++ program • Managed arrays inherit from class System::Array.

  3. double array1 __gc[] = new double __gc[10]; • String *b[] = new String*[100], • *x[] = new String*[27]; • Double array[] = new Double[10]; • Note that we do not have to include keyword __gc, because String * represents a pointer to a managed type

  4. Using array • Every array in MC++ “knows” its own length. • c->Length • const int ARRAY_SIZE = 10; • int z __gc[]; • z = new int __gc[ARRAY_SIZE]; • for( int i = 0; i <z->Length; i++) • z[i] = 2 + 2*i; • For( int i = 0; i < ARRAY_SIZE;i++) • Console::Write(S”{0}\t\t”, z[i].ToString());

  5. Sorting Arrays • Sorting data is one of the most important computing applications. • Bubble sort:use nested loops to make several passes through the array. Each pass compares successive pairs of elements. If the pair is in decreasing order, the bubble sort swaps the values in the array • Array::Sort(a);

  6. Bubble sort void bubblesort(int b __gc[]) { int pass, I, hold; for (pass = 1; pass < b->Length; pass++) for( I = 0; I <b->Length –1; I++) if( b[I] > b[I+1] ) { hold = b[I]; b[I] = b[I+1]; b[I+1] = hold; } }

  7. Searching arrays • The process of locating a particular element value in an array is called searching • Often, programmers work with large amount of data stored in arrays and often need to determine whether an array contains a value that matches a certain key value. • Two searching techniques: • Simple linear search • Binary search

  8. Linear search • Function linear search uses a for loop to compare each element to the key value. If the key is found, the function return the subscript of the element ; if the the search key is not found, the function return –1 int linearsearch(int array __gc[], int key) { for (int I = 0; I <array->Length;I++) if( array[I] == key) return I; return –1; }

  9. Binary search • The linear search works well for small or unsorted array. However, for large arrays, linear searching is inefficient. • Since arrays can be stored as sorted. • High-speed binary search can be used on large sorted array

  10. Binary search algorithm • The algorithm locates the middle array element and compares it with the search key. If they are equal, the search key is found, and the subscript is returned; otherwise, the problem is reduced to searching half of the array. If the key is less than the middle element, the first half of the array is searched; otherwise, the second half of the array is searched. The search continues until the key is found or subarray consists one element that is not equal to the search key( the search key is not found)

  11. Binarysearch function int BinarySearch(int array __gc[], int key) { int low=0, high=array->Length-1, middle; while( low <= high ) { middle = (low+high)/2; if( key == array[middle] ) return middle; else if( key < array[middle]) high = middle-1; else low = middle +1; } return –1; }

  12. Practice Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commission basis. The salespeople receive $200 per week, plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount): • 200 – 299 300 – 399 400 – 499 • 500 – 599 600 – 699 700 – 799 • 800 – 899 900 – 999 1000 and over

More Related