Engineering Problem Solving with C++, Etter

Slides:



Advertisements
Similar presentations
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 10.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Engineering Problem Solving With C++ An Object Based Approach Chapter 6 One-Dimensional Arrays.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 8 Arrays and Strings
Chapter 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
EGR 2261 Unit 9 Strings and C-Strings  Read Malik, pages in Chapter 7, and pages in Chapter 8.  Homework #9 and Lab #9 due next week.
1 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2012 Pearson Education, Inc. Chapter 7 One Dimensional Arrays.
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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Characters, Strings, And The string Class Chapter 10.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 2A Data Types Richard Gesick
Fundamentals of Characters and Strings
Computer Programming BCT 1113
Chapter 13 Applied Arrays: Lists and Strings
Chapter 2 Topics Programs Composed of Several Functions
Engineering Problem Solving with C++, Etter
Characters, C-Strings, and More About the string Class
Standard Version of Starting Out with C++, 4th Edition
Arrays … The Sequel Applications and Extensions
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.
Object Oriented Programming COP3330 / CGS5409
C Stuff CS 2308.
Array Data Structure Chapter 6
One Dimensional Arrays
Array Data Structure B.Ramamurthy 11/21/2018 B.Ramamurthy.
Remark: Data Type of Array Name
Pointers and Pointer-Based Strings
Chapter 9 One-Dimensional Arrays
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
String in C++.
Chapter 3: Input/Output
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++ An Object Based Approach
Array Data Structure Chapter 6
Standard Version of Starting Out with C++, 4th Edition
Lecture 2 Arrays & Pointers May 17, 2004
Chapter 13 Applied Arrays: Lists and Strings
C++ Programming Lecture 20 Strings
Today’s Objectives 28-Jun-2006 Announcements
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Lecture 2 Arrays & Pointers September 7, 2004
CS31 Discussion 1H Fall18: week 6
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.
Dr. Khizar Hayat Associate Prof. of Computer Science
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

One Dimensional Arrays Sorting Algorithms Searching Algorithms Character Strings The string Class. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Definition and Initialization Computation and Output Function Arguments Arrays 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Definition An array is a data structure for storing a contiguous block of data. All data elements in an array must be of the same type. Individual elements of the array are specified using the array name and an offset. In C++ the offset of the first element is always 0. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Definition and Initialization Syntax: data_type identifier[size] [= initialization list]; Note: size is an integer constant. Example: double m[8]; m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7] ? 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Initializing Arrays Initializing array elements (initialization=>declaration) char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; bool ansKey[] ={true, true, false, true, false, false}; char word[] = "Hello"; vowels 'a' 'e' 'i' 'o' 'u' ansKey true true false true false false word 'H' 'e' 'l' 'l' 'o' '\0' 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Accessing Array Elements Offsets are used to access individual elements of an array. General format: array_identifier[offset] Example for (int i=0; i<=7; ++i) m[i] = double(i) + 0.5; Integer expressions may be used as offsets. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Functions and arrays An array identifier, without subscripts, references the starting address(first element) of the array. In C++, arrays are passed by reference. ie the starting address is passed, no size information. Arrays in C++ to not know their size. Generally we specify an additional parameter representing the number of elements in the array. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Example #include <iostream> using namespace std; const int MAXSIZE=20; void ReadArr(double a[], int& count, istream& in); int FindMin(const double a[], int count); int main( ) { double darr[MAXSIZE]; int cnt=0, position=0; ReadArr(darr, cnt, cin); position = FindMin(darr, cnt); cout << "The smallest value in the array is " << darr[position] << endl; } 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber // This function inputs values into an array until EOF or array // limit reached void ReadArray(double a[], int& count, istream& in) { double temp; count = 0; in >> temp; while ( (count < MAXSIZE) && !in.eof() ) { a[count] = temp; ++count; } 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber //This function returns the offset of the smallest //value in an array int FindMin(const double a[], int size) { int offsetOfMin = 0; for (int i=1; i<size; ++i) { if (a[i] < a[offsetOfMin] ) { offsetOfMin = i; }//end if }//end for return offsetOfMin; }//end FindMin 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber selection sort Sorting Algorithms 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Sorting Algorithms Sorting algorithms arrange the data into either ascending or descending order, based on the values in the array. Sorting algorithms to be discussed Selection sort 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Selection Sort Algorithm Find minimum value, place it in the first position. Find next minimum value, place it in the second position. Continue doing this until you have placed the second to the largest value in the second to the last position. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Practice! Fill in the following table to show how the array is sorted into ascending order using the selection sort. arr[0] arr[1] arr[2] arr[3] arr[4] 29 45 18 51 36 swap min and arr[0] 18 29 45 51 36 18 29 36 51 45 18 29 36 45 51 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber unordered lists ordered lists Search Algorithms 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Searching Unordered Lists Simple Sequential Search Examine each element starting with the first one until: a match is found. end of the list is reached. Sequential search can be implemented as: a function which returns true if item in the list, false if item is not in the list. a function which returns the location of the item if found, or –1 if not found. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Searching Ordered Lists Modified Sequential Search: examine every element in the list until: item is found. list element is larger/smaller than the item you are searching for (search fails). Binary Search Examine middle element: if element equals item, item found, return location. if element is larger than item ignore bottom of list. if element is smaller than item ignore top of list. Repeat until: top and bottom cross over (search failed). 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Example of Binary Search for 48 5 11 14 22 28 37 43 56 59 70 arr[top] arr[mid] arr[bot] 37 43 56 59 70 mid is 7 arr[top] arr[mid] arr[bot] mid is 5 43 37 arr[top] arr[bot] mid is 6 43 arr[6] 43 arr[bot] arr[top] 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber C style strings functions defined in cstring Character Strings 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

C Style Character Strings A C style strings is defined as a sequence of characters, terminated by the null character. When declaring a character array to store a C style string, memory must be allocated for the null character ('\0'). Literal string constants are enclosed within double quote marks: "a string". 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber C Style String Input Recall that the input operator (>>) skips whitespace . To input strings with embedded whitespace , the getline() function can be used as illustrated: char phrase[SIZE]; cin.getline(phrase, SIZE); The getline() function reads up to SIZE-1 characters from the input stream and will insert the null character. getline() is a member function of what class? 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

C STYLE STRING FUNCTIONS The Standard C++ library contains a set of predefined functions that operate on C style strings. These functions are defined in the header file: cstring Commonly used string functions: strlen() strcpy() strcat() strcmp() 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Example: C Style Strings #include <iostream> #include <cstring> //strcmp(), strcpy(), strcat() uses namespace std; int main(){ char str1[30] = "John", str2[30] = "Johnson"; char phrase[20] = "'s shirt was green", sentence[30]; if (strcmp(str1,str2) < 0) strcpy (sentence, str1); // puts "John" into sentence else strcpy (sentence,str2); // puts "Johnson into sentence strcat(sentence, phrase); cout << "Sentence is: " << sentence << endl; return 0; } Prints Sentence is: John's shirt is green 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber functions defined in string The string class 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The string class The string class implements the concept of a character string. A string object can increase and decrease its size dynamically. Numerous operators and functions are defined in the string class. 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Common Functions Defined in string size( ) empty( ) substr (int start, int len) c_str() 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Overloaded Operators Defined in string relational operators < > == <= >= concatenation + += assignment = 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Example: string class #include <iostream> #include <string> uses namespace std; int main(){ string str1 = "John", str2 = "Johnson"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1; // assign "John" to sentence else sentence = str2; // assign "Johnson to sentence sentence += phrase; // append phrase to sentence cout << "Sentence is: " << sentence << endl; return 0; } Prints Sentence is: John's shirt is green 2/23/2019 Engineering Problem Solving with C++, Second Edition, J. Ingber