One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.

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.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1 Overview 8.1 An Array Type for Strings 8.2 The Standard string.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Chapter 10.
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 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
ACS 168 Structured Programming Using the Computer Spring 2002 Joaquin Vila Prepared by Shirley White.
1 Chapter 10 Characters, Strings, and the string class.
Chapter 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CHAPTER 8 CHARACTER AND STRINGS
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
Chapter 8 Strings and Vectors (8.1 and 8.2). An Array of characters Defined as: char firstName[20]; char firstName[] = {‘T’, ‘i’, ‘m’}; // an array of.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CSC 270 – Survey of Programming Languages
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
 2003 Prentice Hall, Inc. All rights reserved. 5.11Function Pointers Pointers to functions –Contain address of function –Similar to how array name is.
C++ STRINGS ● string is part of the Standard C++ Library ● new stuff: ● cin : standard input stream (normally the keyboard) of type istream. ● >> operator.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter Characters, Strings, and the string class 10.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Lecture 2A Data Types Richard Gesick
Chapter 13 Applied Arrays: Lists and Strings
Engineering Problem Solving with C++, Etter
Characters, C-Strings, and More About the string Class
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
One Dimensional Arrays
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++.
Engineering Problem Solving with C++, Etter
Standard Version of Starting Out with C++, 4th Edition
Chapter 12: More on C-Strings and the string Class
Presentation transcript:

One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1

2 Sorting Algorithms Sorting algorithms arrange the data into either ascending or descending order, based on the values in the array. Sorting algorithms: Selection sort

3 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.

4 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] swap min and arr[0]

#include using namespace std; void display(int list[],int size); void sort(int list[],int size); int main(){ int list[ ]={5,3,12,8,1,9}; int n=6; cout<<"List before sorting: "<<endl; display(list, n); sort(list, n); cout<<"List after sorting: "<<endl; display(list, n); return 0; }//main //Display list void display(int list[],int size) { for(int i=0;i<size;i++) cout<<list[i]<<" "; cout<<endl; }//display //Sort the list void sort(int list[],int size) { int min,hold; for(int i=0;i<size-1;i++){ //find the index "min" for the element //with lowest value min=i; for(int k=i+1;k<size;k++) if(list[min]>list[k]) min= k; //Exachage list[min] with list[i] hold = list[min]; list[min] = list[i]; list[i] = hold; //Display sorting steps – to be removed cout<<"List in sort fun: i="<<i<<endl; display(list, size); }//for }//sort 5

int list[]={5,3,12,8,1,9};int list[]={29,45,18,51,36}; 6

7 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.

//Search function returns position if element found #include using namespace std; int search(int value, int list[], int size); int main( ){ int list[6]={5,3,12,8,1,9}; int element, index; cout<<"Enter an element"<<endl; cin>>element; index= search(element,list,6); if(index != -1) cout<<"Element "<<element<<" is in index "<<index<<endl; else cout<<"Element " <<element <<" is not in the list "<<endl; return 0; } int search(int value, int list[], int size){ for (int i=0;i<size;i++) if(value==list[i]) return i; //value is found in list at position i return -1; //value was not found in the list } 8

//search function using returns true if element was found otherwise returns false #include using namespace std; bool search(int value, int list[], int size, int& pos); int main( ){ int list[6]={5,3,12,8,1,9}; int element, index (-1); cout<<"Enter an element"<<endl; cin>>element; if(search(element,list,6, index)) cout<<"Element "<<element<<" is in index "<<index<<endl; else cout<<"Element " <<element <<" is not in the list "<<endl; return 0; }//main bool search(int value, int list[], int size, int& pos){ bool found=false; for (int i=0;i<size && !found;i++) if(value==list[i]){ found=true; pos=i; //value is found in list at position i } return found; }//search 9

10 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".

11 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 istream class

#include using namespace std; int main() { char line[100]; ifstream fin; fin.open("xp.txt"); if(fin.fail()){ cout<<"Error opening file xp.txt"<<endl; return 1; } while (fin) { fin.getline(line,100); cout<<line<<endl; }//while return 0; }//main 12

C STYLE STRINGS In C++, we can define C-Style strings as array of characters. C-Style string represents a string as an array of character ending with the null character ‘\0’ that indicates the end of the string elements. Examples C++: String std=“Ahmed”, col=“Engineering”; C-Style string: Examples char std[]={‘A’,’h’,’m’,’e’,’d’,’\0’}; char col[]={‘E’,’n’,’g’,’i’,’n’,’e’,’e’,’r’,’i’,’n’,’g’,’\0’}; ‘\0’ is the null character and occupies one element of the array but not counted in the size of the array. ‘\0’ denotes the end of the array elements. 13

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(str): returns the length of the string str –strcpy(str1,str2): copies two strings str2 into str1 –strcat(str1,str2 ): appends a copy of str2 to the end of str1 –strcmp(): Compare two strings str1 and str2 14

15 Example: C Style Strings #include #include //strcmp(), strcpy(), strcat() uses namespace std; int main(){ char str1[30] = “Salim", str2[30] = “Ali"; char phrase[20] = "'s shirt was green", sentence[30]; if (strcmp (str1,str2) < 0) strcpy (sentence, str1);// puts “Salim" into sentence else strcpy (sentence,str2); // puts “Ali” into sentence strcat (sentence, phrase); //add phrase to sentence cout << "Sentence is: " << sentence << endl; return 0; }

16 The string class Functions defined in String class, i.e. #include 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.

String Class include declaring string objects string word="Engineering"; string word2; string member functions –size( ) : returns string length –empty( ): checks if the string is empty (true/false) –substr (int start, int len): returns a substring from the specified string –c_str(): converts string format to c string (array of chars) 17

String class operators relational operators == = concatenation + += assignment = 18

Example Using string Class #include using namespace std; int main() {string str1="Salem", str2=“Ali"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1;// puts “Salem" into sentence else sentence = str2; // puts “Ali” into sentence sentence += phrase; cout << "Sentence is: " << sentence << endl; return 0; } Sentence is: Ali's shirt was green 19