Chapter 9 One-Dimensional Arrays

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
C++ for Engineers and Scientists Third Edition
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
COSC 2006 Data Structures I Recursion II
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Applications of Arrays (Searching and Sorting) and Strings
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Chapter 9: Part 2: Vectors + Maps and STL Overview JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 4 Ming Li Department of Computer.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Recursion Version 1.0.
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Computer Programming BCT 1113
Chapter 13 Applied Arrays: Lists and Strings
Vectors Holds a set of elements, like an array
Tutorial 8 Pointers and Strings
Search Algorithms Sequential Search (Linear Search) Binary Search
Arrays in C.
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
10 – Iterators C++ Templates 4.6 The Iterator pgs
String in C++ A string is an array of characters which contains a non-printing null character ‘\0’ ( with ASCII value 0 ) marking its end. A string.
CS111 Computer Programming
C Stuff CS 2308.
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
INC 161 , CPE 100 Computer Programming
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Week 9 – Lesson 1 Arrays – Character Strings
Strings A collection of characters taken as a set:
Programming with ANSI C ++
String class and its objects
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
C++ File Structure and Intro to the STL
Chapter 8 – Searching and Sorting Arrays
Searching and Sorting 1-D Arrays
Recursive Linked List Operations
Standard Version of Starting Out with C++, 4th Edition
Given value and sorted array, find index.
Engineering Problem Solving with C++, Etter
Search algorithms for vectors
Lists - I The List ADT.
Lists - I The List ADT.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
Searching and Sorting Arrays
Chapter 13 Applied Arrays: Lists and Strings
C++ Programming Lecture 20 Strings
Strings #include <stdio.h>
C++ Programming: chapter 10 – STL
CS31 Discussion 1H Fall18: week 6
C++ Pointers and Strings
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

Chapter 9 One-Dimensional Arrays

Chapter 9 An array is an indexed data structure that is used to store data elements of the same data type.

Chapter 9 A one-dimensional array, or list, is a sequential list of storage locations that contain data elements that are located via indices.

Chapter 9 One-Dimensional Array Definition Format element data type array name[SIZE];

Chapter 9 Inserting an Array Element Using Direct Assignment array name [index] = element value;

Chapter 9 Inserting Array Elements Using a for Loop for(int index=0;index<array size; ++index) assign or read to array[index]

Chapter 9 Copying an Array Element Using Direct Assignment variable = array name[index];

Chapter 9 Displaying Array Elements Down the Screen Using a for Loop for(int index=0;index<array size; ++index) cout << array[index] << endl;

Chapter 9 C-stings are stored in character arrays where the last element of the string is always the ‘\0’ null terminator character.

Chapter 9 C-String Definition with a Specified Size char variable identifier[maximum size of string + 1] = “\0”;

Chapter 9 C-String Definition without a Specified Size char variable identifier[ ] = "string value";

Chapter 9 Reading C-Strings Using getline() cin.getline(array ident., array size, 'delimiting character');

Chapter 9 C-String Functions strcat() string.h Appends one string to another. strcmp() string.h Compares two strings. strlen() string.h Returns length of a string. strcpy() string.h Copies a string.

Sequential Search Set found = false. Chapter 9 Sequential Search Set found = false. Set index = first array index. While (element is not found) AND (index <= last array index) If (array[index] == element) Then Set found = true. Else Increment index. If (found == true) Return index. Return –1. .

Set i = second array index. Chapter 9 Insertion Sort Set i = second array index. While (i <= last array index) Set j = i. While ( (j > first array index) AND (array[j] < array[j – 1]) ) Exchange array[j] and array[j – 1]. Decrement j. Increment i.

Chapter 9 teleSearch() BEGIN If (the telephone book only contains one page) Look for the name on the page. Else Open the book to the middle. If (the name is in the first half) teleSearch(first half of the book for the name). teleSearch(second half of the book for the name). END.

Recursive Binary Search (initial algorithm) Chapter 9 Recursive Binary Search (initial algorithm) binSearch() If (the array has only one element) Determine if this the element. Else Find the midpoint of the array. If (the element is in the first half) binSearch(first half). binSearch(second half).

Recursive Binary Search (final algorithm) If (first > last) Chapter 9 Recursive Binary Search (final algorithm) If (first > last) Return –1. Else Set mid = (first + last) / 2. If (array[mid] == element) Return mid. If (the element is in the first half) binSearch(array, element, first, mid – 1). binSearch(array, element, mid + 1, last).

Chapter 9 Method binSearch():Searches a sorted array of integers for a given value. Accepts: An array of integers, an element for which to search, the first index of the array being searched, and the last index of the array being searched. Returns: The array index of the element, if found, or the value –1 if the element is not found. int binSearch(int[] array, int element, int first, int last)

Chapter 9

Chapter 9 int binSearch(int array [], int element, int first, int last) { int mid; //ARRAY MIDPOINT if (first > last) //IF ELEMENT NOT IN ARRAY return -1; //RETURN -1, ELSE CONTINUE else mid = (first + last) / 2; //FIND MIDPOINT OF ARRAY if (element == array[mid]) //IF ELEMENT IS IN ARRAY[MID] return mid; //RETURN MID else //ELSE SEARCH APPROPRIATE HALF if (element < array[mid]) return binSearch(array, element, first, mid - 1); return binSearch(array, element, mid + 1, last); } //END OUTER ELSE } //END binSearch()

Chapter 9 The C++ Standard Template Library, or STL, contains a class called vector that can be used to build and manipulate one-dimensional arrays.

Chapter 9 Defining a Vector Example //INTEGER VECTOR, SIZE 10, //INITIALIZED TO 0s vector<int> v1(10,0);

STL Vector Class Functions Chapter 9 STL Vector Class Functions void begin() Places iterator at beginning of the vector. vector data type back() Returns, but does not remove, the last element of the vector. bool empty() Returns true if vector is empty. void end() Places iterator at end of the vector. vector data type front() Returns, but does not remove, the first element of the vector. void pop_back() Removes, but does not return, the last element of the vector.

STL Vector Class Functions Chapter 9 STL Vector Class Functions void begin() Places iterator at beginning of the vector. vector data type back() Returns, but does not remove, the last element of the vector. bool empty() Returns true if vector is empty.

STL Vector Class Functions (continued) Chapter 9 STL Vector Class Functions (continued) void end() Places iterator at end of the vector. vector data type front() Returns, but does not remove, the first element of the vector. void pop_back() Removes, but does not return, the last element of the vector.

STL Vector Class Functions (continued) Chapter 9 STL Vector Class Functions (continued) void push_back(element) Places element at the end of the vector. int size() Returns the number of elements in the vector.