Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICOM 4015 Advanced Programming

Similar presentations


Presentation on theme: "ICOM 4015 Advanced Programming"— Presentation transcript:

1 ICOM 4015 Advanced Programming
Lecture 3 Busqueda y Ordenamiento I Reading: LNN Chapters 9,10 & 16 Prof. Bienvenido Vélez 5/31/2019 ICOM 4015

2 Búsqueda y Ordenamiento
Topic 1 why arrays? declaration/initialization/acess array parameters Topic 2 Searching algorithms Topic 3 Sorting algorithms 5/31/2019 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 5/31/2019 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 5/31/2019 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; sumDevs += sqr(numbers[i] - avg); float stdev = sqrt(sumDevs / counter); // Print report printReport(counter, maxDatum, minDatum, avg, stdev); 5/31/2019 ICOM 4015 stats.cc

6 Statistics Auxiliary 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 5/31/2019 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: Max: 6 Min: 3 Avg: Std Dev: ~/icom4015/lec10 >> 5/31/2019 ICOM 4015

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

9 Array Basics Summary 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! 5/31/2019 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 5/31/2019 ICOM 4015

11 Reversing and array Main 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; } array-params..cc 5/31/2019 ICOM 4015

12 Reversing and Array Auxiliary 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; void printArray(ostream& sink, T a[], int length) for (int i=0; i<length; i++) { sink << a[i] << endl; 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; } 5/31/2019 ICOM 4015 array-params.cc

13 Example 1 Output ~/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: ~/icom4015/lec11 >> 5/31/2019 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. 5/31/2019 ICOM 4015


Download ppt "ICOM 4015 Advanced Programming"

Similar presentations


Ads by Google