String as Arrays, Array Sorting C++ Programming Technologies.

Slides:



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

The simple built-in data types of the C language such as int, float, - are not sufficient to represent complex data such as lists, tables, vectors, and.
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
#include using namespace std; void main() { int a[3]={10,11,23}; for(int i=0;i
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Chapter 2: Introduction to C++.
Chapter 8 Arrays and Strings
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Computer Science 1620 Strings. Programs are often called upon to store and manipulate text word processors chat databases webpages etc.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Programming Languages
Input & Output: Console
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 8 Arrays and Strings
February 11, 2005 More Pointers Dynamic Memory Allocation.
Chapter 7 One-Dimensional Arrays 7.1 Arrays in C One of the more useful features of C is the ability to create arrays for storing a collection of related.
Arrays CE 102 Algorithms and Programming KTO Karatay University Arrays are data structures consisting of data items of the same type Arrays are not dynamic.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, FIFTH EDITION Chapter 10: Strings and string type.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays.
Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 15 Strings as Character Arrays
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
A Sample Program #include using namespace std; int main(void) { cout
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Command Line Arguments
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
C Arrays.
2.1 Parts of a C++ Program.
Jeff West - Quiz Section 4
Chapter 2: Introduction to C++.
CHAPTER 2 Arrays and Vectors.
CS250 Introduction to Computer Science II
CHAPTER 2 Arrays and Vectors.
CS-161 Computer Programming Lecture 15 & 16: Arrays II
Programming Strings.
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
Presentation transcript:

String as Arrays, Array Sorting C++ Programming Technologies

String as Array The string class has been briefly introduced in an earlier chapter. It is a very powerful class to handle and manipulate strings of characters. However, because strings are, in fact, sequences of characters, we can represent them also as plain arrays of elements of a character type. For example, the following array: char foo [20]; - is an array that can store up to 20 elements of type char.

Examples By convention, the end of strings represented in character sequences is signaled by a special character: the null character, whose literal value can be written as '\0' (backslash, zero). In this case, the array of 20 elements of type char called foo can be represented storing the character sequences "Hello"and "Merry Christmas" as:

Examples Because arrays of characters are ordinary arrays, they follow the same rules as these. For example, to initialize an array of characters with some predetermined sequence of characters, we can do it just like any other array: Therefore, the array of char elements called myword can be initialized with a null-terminated sequence of characters by either one of these two statements: char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char myword[] = "Hello";

Example // strings and NTCS: #include using namespace std; int main () { char question1[] = "What is your name? "; string question2 = "Where do you live? "; char answer1 [80]; string answer2; cout << question1; cin >> answer1; cout << question2; cin >> answer2; cout << "Hello, " << answer1; cout << " from " << answer2 << "!\n"; return 0; } What is your name? Homer Where do you live? Greece Hello, Homer from Greece!

Sorting Arrays : Bubble Sort Example: Original: Pass 1: (elements exchanged) Pass 2: Pass 3: (no changes needed) Pass 4: Pass 5: Small elements "bubble" to the top (like 2 in this example)

Bubble Sort Bubble sorting is the very commonly and widely used sorting technique in C++ programming. It is also known as the exchange sort. It repeatedly visits the elements of an array and compares the two adjacent elements. It visits the array elements and compare the adjacent elements if they are not in the right order then it puts them into the right order. It continues to swap the elements until no more swaps are required, which means the given array is sorted. It works in the following steps described below:

Bubble Sort Algorithm int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; }

THANKS FOR ATTENTION