§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
C++ for Engineers and Scientists Third Edition
Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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.
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
UNIT-4 1. Arrays: Definition and declaration, Initialization, Accessing elements of arrays, Storing values in arrays, Inter-function Communication: Passing.
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 6 Arrays in C++ 2nd Semester King Saud University
Arrays Low level collections.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Array An “average.cpp” program
Chapter 7 Part 1 Edited by JJ Shepherd
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 7 Arrays Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Programming fundamentals 2 Chapter 1:Array
Arrays Part-1 Armen Keshishian.
New Structure Recall “average.cpp” program
C++ Arrays.
A solution to a list of records
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
While Loops.
Arrays Part II Array Function Arguments
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
One-Dimensional Array Introduction Lesson xx
Arrays Kingdom of Saudi Arabia
Data type List Definition:
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Sorting … and Insertion Sort.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Lecture 11.
Arrays I Handling lists of data.
CS150 Introduction to Computer Science 1
Chapter 9: Data Structures: Arrays
§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊
CS150 Introduction to Computer Science 1
Linked Lists.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
CS-161 Computer Programming Lecture 15 & 16: Arrays II
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
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.
Arrays Prepared By Paritosh Srivastava PGT (CS) KV NHPC Banbasa.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
ㅎㅎ Fourth step for Learning C++ Programming Call by value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Arrays

Input, Process, Overwrite § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Input, Process, Overwrite // input 20 temps and calculate an avg double sum = 0, temp, avg; for (int i=0; i<20; i++) { cout << "Enter a temp: "; cin >> temp; // overwrites the last sum += temp; // temp value entered } avg = sum / 20; There is only one temperature in memory at a time!

Input, Process, Overwrite § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Input, Process, Overwrite Question: How can we quickly create 20 temperature variables that are easy to access? (And so keep ALL temps in memory once they are entered)? Answer: Arrays

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Definition: A sequence of values in memory (called Elements) where: each Element is the same Data Type - the number of Elements (size) is constant

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Declaration Syntax: type identifier [ size ] ; where size is a Literal or Constant Integer. Examples: int score[15]; const int NUM_TEMPS = 20; float temp[NUM_TEMPS];

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Declaration Syntax: type identifier [ size ] ; size can NOT be a variable! This is a Syntax Error int numTemps = 20; float temp[numTemps];

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Declaration Semantics: type identifier [ num ] ; allocates a sequence of num locations of type type, indexed 0 to (num – 1)

Declaration Semantics: Example int a[5]; § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Declaration Semantics: Example int a[5];

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Accessing an element: identifier [ index ] where index is an integer between 0 and (numOfEle – 1) This can be used the same as a Variable of the declared Type.

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Accessing an element: identifier [ index ] The index is thought of as a subscript, so: list[3] is often translated into English as: "list sub 3"

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Example 1: int a[5]; a[1] = 12; a[4] = -3; // array index out of // bounds error! a[5] = 0;

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Example 2: double sum = 0, temp[20], avg; for (int i=0; i<20; i++) { cout << "Enter a temp: "; cin >> temp[i]; sum += temp[i]; } avg = sum / 20; And: all 20 temps entered are still in memory!

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Style (Best Practice): Use a constant for num. of elements const int NUM_TEMPS = 20; double sum=0, temp[NUM_TEMPS], avg; for (int i=0; i<NUM_TEMPS; i++) { cout << "Enter a temp: "; cin >> temp[i]; sum += temp[i]; } avg = sum / NUM_TEMPS;

§ § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Array Issue: At the time a program is written, the number of elements: - may not be known, or - may need to vary (a different number each time the program is used)

Solution: a Partial Array consists of § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Partial Array Solution: a Partial Array consists of an array where only the first n elements is in use, where n is 0 to (NumElements – 1) an integer variable that is the number of elements in use (n). [usually] an integer constant that is the MAX number of elements.

Partial Array Example: // partial array of temperatures, max 20 § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Partial Array Example: // partial array of temperatures, max 20 const int MAX_TEMPS = 20; int numTemps = 0;// start ”empty” double temp[MAX_TEMPS]; The program must ensure 0 <= numTemps < MAX_TEMPS

Partial Array Maximum Number of Elements: § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Partial Array Maximum Number of Elements: When the Maximum Number of Elements is not given in the problem, the programmer must make an ”intelligent guess”. Example: a list of test scores. What is the maximum number of tests any user would need for this program? Guess too small: your program is unusable. Guess too large: your program wastes memory.

Partial Array Example: const int MAX_TEMPS = 20; § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Partial Array Example: const int MAX_TEMPS = 20; int numTemps = 0; // start ”empty” double temp[MAX_TEMPS]; cout << ”How many temps? (0-20)”; cin >> numTemps; //should validate 0-20 for (int i = 0; i < numTemps; i++) { ...temp[i]...// process the temps }

General Algorithm for Arrays § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ General Algorithm for Arrays For most problems involving an array, use a for loop const int MAX_ELE = whatever; int numEle; type a[MAX_ELE]; for (int i = 0; i < numEle; i++) { ... // do something with a[i] }

Common Algorithms Initialize const int MAX = 100; double a[MAX]; § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Initialize const int MAX = 100; double a[MAX]; int num = 0; // for Partial Array // set ALL elements to "unused"/"empty" for (int i=0; i < MAX; i++) { a[i] = 0.0; }

Common Algorithms Populate (User Input) § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Populate (User Input) do { // validate number of values used cout << "How many values? "; cin >> num; } while (num < 0 || num > MAX); for (int i=0; i < num; i++) { cout << "Enter a value: "; cin >> a[i]; }

Common Algorithms Sum // sum of all values in the array § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Sum // sum of all values in the array double sum = 0.0; for (int i=0; i < num; i++) { sum += a[i]; }

Common Algorithms Max // find the highest value § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Max // find the highest value double m = a[0]; // start with first ele for (int i=1; i < num; i++) { if (a[i] > m) m = a[i]; }

Common Algorithms Min // find the lowest value § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Min // find the lowest value double m = a[0]; // start with first ele for (int i=1; i < num; i++) { if (a[i] < m) m = a[i]; }

Common Algorithms Min // find the lowest value § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Min // find the lowest value double m = a[0]; // start with first ele for (int i=1; i < num; i++) { if (a[i] < m) m = a[i]; else // common mistake! m = something else; }

Common Algorithms Print // print all values in an array § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Print // print all values in an array for (int i=0; i < num; i++) { cout << a[i] << ” ”; }

Append to a Partial Array § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Append to a Partial Array // add a new value to the end of the list double newValue; ... if (num < MAX) {// check if room available a[num] = newValue; num ++; } // else array is full, can not append

Remove from Partial Array § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Remove from Partial Array

Remove from a Partial Array § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Remove from a Partial Array // remove r’th ele from the list & "save" it int r; // index of the element to remove double rem; // copy of removed value ... if (r>=0 && r<num) { // ensure r is "in range" rem = a[r]; // save a copy (optional) num--; // one less item in list for (int i = r; i < num; i++) a[i] = a[i+1]; a[num] = EMPTY_VALUE; // optional } // else r is out of range, can't remove

Common Algorithms Linear Search § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Linear Search // find INDEX where value found; -1=not found double searchValue; ... int found = -1; // assume not found for (int i=0; i<num; i++) { if (a[i] == searchValue) found = i; // found in a[i] } // after loop: found = -1 means "not found" // found != -1 means "found in this element"

Common Algorithms Linear Search § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Linear Search // find INDEX where value found; -1=not found double searchValue; ... int found = -1; // assume not found for (int i=0; i<num; i++) { if (a[i] == searchValue) found = i; else // common mistake! found = -1; }

(Easy to Memorize) Bubble Sort § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms (Easy to Memorize) Bubble Sort for (int i=0; i<num-1; i++) { for (int j=i+1; j<num; j++) if (a[i] > a[j]) { // swap a[i] with a[j] double t = a[i]; a[i] = a[j]; a[j] = t; }

Bubble Sort: Decreasing Order (Hi to Low) § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Common Algorithms Bubble Sort: Decreasing Order (Hi to Low) for (int i=0; i<num-1; i++) { for (int j=i+1; j<num; j++) if (a[i] < a[j]) { // change > to < // swap a[i] with a[j] double t = a[i]; a[i] = a[j]; a[j] = t; }

Vocabulary Array Element Index Partial Array Append Populate Sort § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § § ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ ◊ Vocabulary Term Definition Array An indexed sequence of values in memory where each value is the same data type and the number of values is constant Element One of the items (values) in an array. Index An integer that references an element of an array. Its value must be between 0 and the array’s size minus one. Partial Array An array where only the first n elements is in use, with 0 <= n < size of the array. Append To add a new item to the end of a list of items. Populate To enter data into a variable, array, or other data structure. Sort To order a list by values. Ex: 8 4 6 9 sorted is 4 6 8 9; increasing or decreasing