Working With Arrays.

Slides:



Advertisements
Similar presentations
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Advertisements

Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Problem: Given an array of positive integers, find a pair of integers in the array that adds up to 10 (or indicate none exist). Your code should be as.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Array, Pointer and Reference ( V ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Reference parameters (6.2.3) We might need a function to return more than one value Find roots of a quadratic 2 return values. What are they? Get first.
Arrays in C++: Numeric Character (Part 2). Passing Arrays as Arguments in C++, arrays are always passed by reference (Pointer) whenever an array is passed.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Chapter 7 Arrays. Introductions Declare 1 variable to store a test score of 1 student. int score; Declare 2 variables to store a test score of 2 students.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Computer Skills2 / Scientific Colleges 1 Arrays Topics to cover: Arrays Data Types One-dimensional Arrays Two-dimensional Arrays.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
1 C++ Classes and Data Structures Course link…..
Sentinel Loops. while Syntax while(expression) statement.
Searching Arrays Linear search Binary search small arrays
Example 21 #include<iostream.h> int main() { char Letter = 0;
Data Structures and Algorithms
Passing Objects to Methods
CS Computer Science IA: Procedural Programming
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Programming fundamentals 2 Chapter 1:Array
Chapter 7: Arrays.
C++ Arrays.
לולאות קרן כליף.
CS 1430: Programming in C++.
Object-Oriented Programming (OOP) Lecture No. 32
CS 1430: Programming in C++.
Function Basics.
Arrays Part II Array Function Arguments
Array Data Structure Chapter 6
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Null-Terminated Character Arrays
Data type List Definition:
CS150 Introduction to Computer Science 1
Counting Loops.
The Run-Time Stack and Reference Parameters
Pass by Reference.
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Default Arguments.
Arrays Topics to cover: Arrays Data Types One-dimensional Arrays
Arrays Lecture 11.
CS150 Introduction to Computer Science 1
Array Data Structure Chapter 6
Function Overloading.
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.
Class StudentList class StudentList { private: int numStudents;
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
Presentation transcript:

Working With Arrays

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } [0] [1] [2] [3] [4]

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=0 [0] [1] [2] [3] [4]

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=0 [0] [1] [2] [3] [4] 12

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=1 [0] [1] [2] [3] [4] 12

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=1 [0] [1] [2] [3] [4] 12 15

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=2 [0] [1] [2] [3] [4] 12 15

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=2 [0] [1] [2] [3] [4] 12 15 31

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=3 [0] [1] [2] [3] [4] 12 15 31

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=3 [0] [1] [2] [3] [4] 12 15 31 18

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=4 [0] [1] [2] [3] [4] 12 15 31 18

Traversals const short SIZE=5; short ages[SIZE]={0}; short sum=0; short average=0; for(short i=0; i<SIZE; i++) { cout << “Person “ << i+1 << “, enter your age: “; cin >> ages[i]; } i=4 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=0, sum=0 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=0, sum=12 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=1, sum=12 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=1, sum=27 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=2, sum=27 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=2, sum=58 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=3, sum=58 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=3, sum=76 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=76 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=90 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=4, sum=90 average=18 ... for(short i=0; i<SIZE; i++) sum+=ages[i]; average=sum/SIZE; cout << “The average age is “ << average; i=4, sum=90 average=18 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=12 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=1 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=15 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=2 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=3 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=3 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=4 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; i=4 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals max=31 short max=ages[0]; for(short i=1; i<SIZE; i++) if(ages[i]>max) max=ages[i]; cout << “the max age in the array is “ << max; [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 31 18 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 31 18 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 18 18 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=31 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 18 31 14

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 31 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 14 14 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 swap=31 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1

Traversals i=1 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=4 [0] [1] [2] [3] [4] 12 15 18 14 31

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 18 14 31

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 18 14 31

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 18 14 31

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 18 14 31

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 18 14 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 14 14 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 swap=18 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1

Traversals i=2 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=3 [0] [1] [2] [3] [4] 12 15 14 18 31

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 15 14 18 31

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 14 18 31

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 14 18 31

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 15 14 18 31 j j+1

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 14 14 18 31 j j+1

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 swap=15 [0] [1] [2] [3] [4] 12 14 15 18 31 j j+1

Traversals i=3 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=2 [0] [1] [2] [3] [4] 12 14 15 18 31

Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31

Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=0 [0] [1] [2] [3] [4] 12 14 15 18 31 j j+1

Traversals i=4 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } j=1 [0] [1] [2] [3] [4] 12 14 15 18 31

Traversals i=5 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31

Traversals i=5 for(short i=1; i<SIZE; i++) for(short j=0; j<SIZE-i; j++) if(ages[j] > ages[j+1]) { int swap = ages[j]; ages[j] = ages[j+1]; ages[j+1] = swap; } [0] [1] [2] [3] [4] 12 14 15 18 31

Arrays as Parameters void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; } int main() const int SIZE=100; float my_array[SIZE]; ... print_array(my_array, SIZE);

Incorrect Argument Syntax void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; } int main() const int SIZE=100; float my_array[SIZE]; ... print_array(my_array[], SIZE);

Preconditions for Arrays as Parameters // Pre: parameter size is a positive integer // and the_array[0] -> the_array[size-1] is data. void print_array(const float the_array[], const int size) { for(int i = 0; i < size; i++) cout << the_array[i] << " "; return; }

Pass by Reference void bubble_sort(float the_array[], const int size) { for(short i=1; i<size; i++) for(short j=0; j<size-i; j++) if(the_array[j] > the_array[j+1]) swap(the_array[j],the_array[j+1]); int main() const int SIZE=100; float my_array[SIZE]; ... bubble_sort(my_array, SIZE);

Pass by Reference BEFORE void shift_right(int the_array[], const int size) { for(int i=size – 1; i>0; i++) the_array[i]=the_array[i-1]; return; } int main() const int SIZE=100; float my_array[SIZE]; ... shift_right(my_array, SIZE); BEFORE [0] [1] [2] [3] [4] 12 14 15 18 31

Pass by Reference BEFORE AFTER void shift_right(int the_array[], const int size) { for(int i=size – 1; i>0; i++) the_array[i]=the_array[i-1]; return; } int main() const int SIZE=100; float my_array[SIZE]; ... shift_right(my_array, SIZE); BEFORE [0] [1] [2] [3] [4] 12 14 15 18 31 AFTER [0] [1] [2] [3] [4] 12 12 14 15 18

Pass by Reference // The is_found() function will search the array passed for an instance of // the target and will return true if the target is found in the array; // false otherwise. // Pre: the_array[0] -> the_array[size] is valid data. // Post: true returned if target found in array; false otherwise. bool is_found (const char the_array[], const int size, const char target) { bool found = false; int i = 0; while(i < size && found == false) { if (target == the_array[i]) found = true; i++; } return found; }

Pass by Reference // The is_found() function will search the array passed for an instance of // the target and will return the first index position containing the target // value if the target is found in the array; -1 is returned otherwise. // Pre: the_array[0] -> the_array[size] is valid data. // Post: the first index position containing the target value is returned; if // the target is not found in the array, a -1 is returned. int is_found (const char the_array[], const int size, const char target) { int position_found = -1; int i = 0; while(i < size && position_found == -1) if (target == the_array[i]) position_found = i; i++; } return position_found;

End of Session