Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
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.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
 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.
 2003 Prentice Hall, Inc. All rights reserved Introduction Arrays –Structures of related data items –Static entity (same size throughout program)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
Arrays Chapter 6.
CMPUT 101 Lab #6 October 29, :00 – 17:00. Array in C/C++ Array is a structure type variable. One dimension of array int: int num[3]; There are.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Chapter 8 Arrays and Strings
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
Arrays in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays and Strings
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Topics to be covered  Introduction to array Introduction to array  Types of array Types of array  One dimensional array One dimensional array  Declaration.
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.
計算機程式語言 Lecture 07-1 國立臺灣大學生物機電系 7 7 Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Introduction to C++ Programming Language Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University,
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
1 Arrays and Vectors Chapter 7 Arrays and Vectors Chapter 7.
1 Chapter 4 - Arrays Outline 4.1Introduction 4.2Arrays 4.3Declaring Arrays 4.4Examples Using Arrays 4.5Passing Arrays to Functions 4.6Sorting Arrays 4.7Case.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Introduction to Programming Lecture 8. String Handling  Character is the building block of strings.  Characters are represented inside the computer.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
1 CS161 Introduction to Computer Science Topic #8.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
 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.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CHAPTER 3 ARRAYS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Arrays 3.Declaring Arrays 4.Examples Using Arrays 5.Multidimensional Arrays 6.Multidimensional.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2000 Prentice Hall, Inc. All rights reserved Arrays Array –Consecutive group of memory locations –Same name and type To refer to an element, specify.
Arrays float Scores[9]; ? index: element // one dimensional array 2.
Objectives You should be able to describe: One-Dimensional Arrays
Introduction to Programming
Computer Programming BCT 1113
Programming Fundamental
Basic Array Definition
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Capitolo 4 - Arrays Outline 4.1 Introduction 4.2 Arrays
Arrays Arrays A few types Structures of related data items
Fundamental Programming
Arrays.
Presentation transcript:

Introduction to Programming Lecture 12

Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms using arrays Multi-dimensional arrays Multi-dimensional arrays

char name [ 100 ] ;

\0

In C we have Used \nNew Line \tTab Character \0Null Character All C strings are terminated by Null character

Character Array in Memory char name [ 100 ] ; cout << “ Please enter your name ” ; cin >> name ;

Initializing an Array Initializing array of integers int c [ 10 ] = { 1,2,3,4,5,6,7,8,9,10 } ; int c [ ] = { 1,2,3,4,5,6,7,8,9,10 } ; For character arrays char name [ 100 ] = { ‘a’,b’,’c’,’0’,’1’ } ; char name [ 100 ] = “abc01“ ; char name [ ] = “Hello World“ ;

Character Arrays To read name from keyboard and display it on screen char name [ 100 ] ; cout << “ Please enter you name” ; cin >> name ; cout << name ;

Character Arrays Displaying name on screen using loop for ( i = 0 ; i < 100 ; i ++ ) { cout << name [ i ] ; }

Comparing Two arrays Array size should be equal int equal = 0 ; int num1 [ 100 ], num2 [ 100 ] ; for ( i = 0 ; i < 100 ; i ++ ) { if ( num1 [ i ] != num2 [ i ] ) { equal = 1 ; break ; }} if ( equal ==1 ) cout << “ The arrays are not equal” ; else cout << “ The arrays are equal” ; Condition :

Comparing Two Arrays AZMAT HAMEED Azmat Hameed

Exercise Input your name and display it in reverse order Input your name and display it in reverse order Determine the length of character array Determine the length of character array

Sorting Bubble Sort Bubble Sort Quick Sort Quick Sort

[0] [1] [2] [16] [99] 1 Brute-Force Technique

66 Swapping Memory Location [0] [1] [2] [16] [99]

Swapping Two Numbers int num [ ] ; int x ; x = num [ 0 ] ; num [ 0 ] = num [ 15 ] ; num [ 15 ] = x ;

Binary Search Algorithms Divide and Conquer rule

Binary Search Algorithm If we think about it, it is If we think about it, it is log n log 2 Total array size will be 2 n and number Total array size will be 2 n and number can be found in n times can be found in n times If 1000 numbers then 10 tries are max If 1000 numbers then 10 tries are max 2 10 = 1024

Is divide and conquer the fastest way, all the time, of searching for a number in a list ? Linear Search ? Binary Search ? Suppose they are Random Suppose they are Ordered Suppose they are mixed-up

Functions and Arrays

Sending Arrays into Another Functions Name of the array Name of the array Size of the array Size of the array

Example 1 c har name [ 100 ] ; reverse ( name, 100 ) ; reverse ( name, 100 ) ; Declaration Function Call

Example 1 void reverse ( char [ ], int ) ; void reverse ( char [ ], int ) ; void reverse ( char characters [ ], int arraySize) { reverse the character string; reverse the character string; } Prototype Definition

Example 1 main ( ) { cin >> name [ ] ; reverse ( character [ ], arraySize ) ; cout << name [ ] ; } What will it Show ?

Call by Reference & Address Operator * Pointer Operator In case of arrays, call by reference is default

X is a variable which is a location in the memory Name [ ] is an array Array called Name Starting address Memory name

Example 2 void f ( int [ ], int ) ; main ( ) { int numbers [ 100 ] ; f ( numbers, 100) ; for ( int i = 0 ; i < 100 ; i ++) cout << numbers [ i ] ; }

Example 2 void f ( int x [ ], int arraySize ) { int i ; for ( i = 0 ; i < arraySize ; i ++) x [ i ] = i ; }

f ( x [ 3 ] ) ; Executed with call by value, not by reference

Whenever a variable is passed, it is passed by value Whenever a variable is passed, it is passed by value Whenever you pass an array to Whenever you pass an array to function, it is called by reference function, it is called by reference

Vector 2 Dimensional 3 Dimensional Dot Product Vector Product

Matrix Rows Columns

Two Dimensional Array int x [ 2 ] [ 3 ] ;

Example 3 int maxRows = 2; int maxCols = 3 ; int matrix [ 2] [ 3 ]; int row, col ; for ( row = 0 ; row < maxRows ; row ++ ) { for ( col = 0 ; col < maxCols ; col ++ ) { cout << “Please enter value of ”<< row << “ “ << col; cin >> matrix [ row ] [ col ] ; }}

After first outer loop After second outer loop Input [0] [1] [0]

Three Dimensional Arrays int x [ ] [ ] [ ] ;