ICOM 4015 Advanced Programming

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Computer Science 1620 Arrays. Problem: Given a list of 5 student grades, adjust the grades so that the average is 70%. Program design: 1. read in the.
C++ for Engineers and Scientists Third Edition
FALL 2001ICOM Lecture 21 ICOM 4015 Advanced Programming Lecture 2 Procedural Abstraction Reading: LNN Chapter 4, 14 Prof. Bienvenido Velez.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter 5 Arrays Copyright © 2016 Pearson, Inc. All rights reserved.
L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
FALL 2001ICOM Lecture 11 ICOM 4015 Advanced Programming Lecture 1 Computer/Human Interaction Readings: LMM 2.3 & 3.3 Prof. Bienvenido Velez.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
L what is a void-function? l what is a boolean function? l is it possible for a function to have no parameters? l what is program stack? function frame?
C++ Lesson 1.
Repetitive Structures
I/O Streams File I/O 2-D array review
Dr. Shady Yehia Elmashad
Chapter 1.2 Introduction to C++ Programming
Computer Programming BCT 1113
Engineering Problem Solving with C++, Etter
C++ Programming Lecture 15 Arrays – Part II
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
Pointers and Pointer-Based Strings
Dr. Shady Yehia Elmashad
Chapter 6: Functions Starting Out with C++ Early Objects
Dr. Shady Yehia Elmashad
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
C++ Programming Lecture 15 Arrays – Part II
Building Java Programs Chapter 7
One-Dimensional Array Introduction Lesson xx
Previous Lecture Review
Arrays Skill Area 315 Part A
Chapter 17 Templates. Chapter 17 Templates Overview 17.1 Templates for Algorithm Abstraction 17.2 Templates for Data Abstraction.
Arrays Kingdom of Saudi Arabia
Screen output // Definition and use of variables
Chapter 8 Arrays Objectives
Data type List Definition:
Chapter 4 Implementing Free Functions
Object Oriented Programming Using C++
Topics discussed in this section:
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Arrays of Two-Dimensions
Chapter 8 Arrays Objectives
Pointers and Pointer-Based Strings
Suggested self-checks: Section 7.11 #1-11
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS250 Introduction to Computer Science II
COMS 261 Computer Science I
CS150 Introduction to Computer Science 1
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
Class StudentList class StudentList { private: int numStudents;
CS-161 Computer Programming Lecture 15 & 16: Arrays II
ICOM 4015 Advanced Programming
Pointers and dynamic objects
Class rational part2.
ICOM 4015 Advanced Programming
Data Structure(s) A way of storing and organizing data in a computer so that it can be used efficiently. e.g. Arrays Linked Lists stacks Queues Trees.
ICOM 4015 Advanced Programming
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

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

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

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

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

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

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

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 >> 5/31/2019 ICOM 4015

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

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

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

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

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

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: [bvelez@amadeus] ~/icom4015/lec11 >> 5/31/2019 ICOM 4015

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