1 / 14

ICOM 4015 Advanced Programming

ICOM 4015 Advanced Programming. Lecture 3 Busqueda y Ordenamiento I Reading: LNN Chapters 9,10 & 16. Prof. Bienvenido Vélez. B ú squeda y Ordenamiento. Topic 1 why arrays? declaration/initialization/acess array parameters Topic 2 Searching algorithms Topic 3 Sorting algorithms.

Download Presentation

ICOM 4015 Advanced Programming

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. ICOM 4015 Advanced Programming Lecture 3 Busqueda y Ordenamiento I Reading: LNN Chapters 9,10 & 16 Prof. Bienvenido Vélez ICOM 4015

  2. Búsqueda y Ordenamiento • Topic 1 • why arrays? • declaration/initialization/acess • array parameters • Topic 2 • Searching algorithms • Topic 3 • Sorting algorithms ICOM 4015

  3. Static Arrays I Outline • Why arrays? • Array declaration, initialization and access • Array parameters • Examples of array usage: • statistics on arrays • array reversal ICOM 4015

  4. Why arrays? Want to represent groups of homogeneous objects BUT Do not want nor care to give each group member a different name. Solution: Access members by position ICOM 4015

  5. Statistics - main function // Standard C++ header files #include <cmath> #include <iostream> // Forward declarations of local functions template <class T> T sqr (T x); int readData(); void printReport(int N, float max, float min, float avg, float stdev); // Global definitions const int ArrayLength = 100; float numbers[ArrayLength]; // Main function int main() { // Read data into numbers array int counter = readData(); cout << "Read " << counter << " numbers" << endl; if (counter == 0) return 0; // Compute Max, Min and Avg in first pass float maxDatum = numbers[0]; float minDatum = numbers[0]; float sum = 0; for(int i=0; i<counter; i++) { sum += numbers[i]; maxDatum = (maxDatum > numbers[i]) ? maxDatum : numbers[i]; minDatum = (minDatum < numbers[i]) ? minDatum : numbers[i]; } float avg = sum / counter; // Compute Std deviation in second pass float sumDevs = 0; for(int i=0; i<counter; i++) { sumDevs += sqr(numbers[i] - avg); } float stdev = sqrt(sumDevs / counter); // Print report printReport(counter, maxDatum, minDatum, avg, stdev); } ICOM 4015 stats.cc

  6. StatisticsAuxiliary functions // Auxiliary local functions int readData() { int counter = 0; while(true) { cout << "Next datum: "; float nextDatum; cin >> nextDatum; if (cin.eof()) break; numbers[counter++] = nextDatum; } return counter; } void printReport(int N, float max, float min, float avg, float stdev) { cout << "Report:" << endl; cout << "N: " << N << endl; cout << "Max: " << max << endl; cout << "Min: " << min << endl; cout << "Avg: " << avg << endl; cout << "Std Dev: " << stdev << endl; } template <class T> T sqr (T x) { return x * x;}; stats.cc ICOM 4015

  7. Statistics Output [bvelez@amadeus] ~/icom4015/lec10 >>arrays Next datum: 3 Next datum: 4 Next datum: 5 Next datum: 6 Next datum: Read 4 numbers Report: N: 4 Max: 6 Min: 3 Avg: 4.5 Std Dev: 1.11803 [bvelez@amadeus] ~/icom4015/lec10 >> ICOM 4015

  8. ID[0] ID[1] ID[2] ID[3] ID[4] ID[dim-1] Anatomy of an Array Definition array name type ID [ dim ] element type number of cells starting from 0 Arrays are zero-offset in C++ ICOM 4015

  9. Array BasicsSummary of Concepts • Arrays are compound objects used to represent groups of homogeneous objects • Each cell of an array must have the same type • Array elements are accessed by position (index) • An array of n cells has elements indexed from 0 to n-1 • An attempt to access a cell beyond the boundaries of the array (below 0 or above n-1) may yield runtime errors that are difficult to detect and fix. Check your boundaries! ICOM 4015

  10. Anatomy of an Array Parameter Definition array name int f(int a[], int length) array dimension unspecified type of elements number of cells additional parameter ICOM 4015

  11. Reversing and arrayMain function // Standard C++ header files #include <cmath> #include <iostream> // Forward declarations of local functions template <class T> swap(T& a, T& b); template <class T> int readData(istream& source, T data[], int maxlen); template <class T> void printArray(ostream& sink, T a[], int length); template <class T> void reverseArray(T a[], int length); // Main function int main() { // Define local array to hold input data const int ArrayLength = 100; float numbers[ArrayLength] = { 0.0 }; // Inititalize all cells to 0 // Read data from standard input int counter = readData(cin, numbers, ArrayLength); cout << "Read " << counter << " numbers" << endl; if (counter == 0) return 0; cout << "Original Array:" << endl; printArray(cout, numbers, counter); reverseArray(numbers, counter); cout << "Reversed Array:" << endl; printArray(cout, numbers, counter); } array-params..cc ICOM 4015

  12. Reversing and ArrayAuxiliary Functions // Auxiliary local functions template <class T> int readData(istream& source, T data[], int maxlen) { int counter = 0; while(counter < maxlen) { cout << "Next datum: "; float nextDatum; source >> nextDatum; if (source.eof()) break; data[counter++] = nextDatum; } return counter; } template <class T> void printArray(ostream& sink, T a[], int length) { for (int i=0; i<length; i++) { sink << a[i] << endl; } } template <class T> void reverseArray(T a[], int length) { int iterations = length / 2; for (int i=0; i<iterations; i++) { swap(a[i], a[length - i - 1]); } } template <class T> swap(T& a, T& b) { T temp = a; a = b; b = temp; } ICOM 4015 array-params.cc

  13. Example 1 Output [bvelez@amadeus] ~/icom4015/lec11 >>array-params Next datum: 1 Next datum: 2 Next datum: 3 Next datum: 4 Next datum: 5 Next datum: 6 Next datum: 7 Next datum: 8 Next datum: 9 Next datum: 10 Next datum: Read 10 numbers Original Array: 1 2 3 4 5 6 7 8 9 10 Reversed Array: 10 9 8 7 6 5 4 3 2 1 [bvelez@amadeus] ~/icom4015/lec11 >> ICOM 4015

  14. Array Parameters Summary • Arrays are passed by reference by default • Compiler ignores array dimension in parameter declaration • Programmer typically passes array length as extra parameter. No way to tell array length from array itself. ICOM 4015

More Related