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

Slides:



Advertisements
Similar presentations
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Advertisements

EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #7 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
EC-111 Algorithms & Computing Lecture #1 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Starting Out with C++, 3 rd Edition 1 Chapter 10 – Characters, Strings, and the string Class.
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
String in C++. String Series of characters enclosed in double quotes.“Philadelphia University” String can be array of characters ends with null character.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
AU/MITM/1.6 By Mohammed A. Saleh 1. Introducing the string Class  Instead of using a character array to hold a string, you can use a type string variable.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 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.
String What it is Why it’s useful library routines for handling strings how to input a string from the keyboard.
Programming is instructing a computer to perform a task for you with the help of a programming language.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
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 6 One-Dimensional Arrays ELEC 206 Computer Tools for Electrical Engineering.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
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.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
CHAPTER 6 ARRAYS IN C++ 2 nd Semester King Saud University College of Applied studies and Community Service CSC 1101 By: Fatimah Alakeel Edited.
Chapter 15 Strings as Character Arrays
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
String as Arrays, Array Sorting C++ Programming Technologies.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Character Sequences. strings are in fact sequences of characters, we can represent them also as plain arrays of char elements. For example, the following.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
EC-111 Algorithms & Computing Lecture #9 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 2 Arrays. Topics 1 Arrays hold Multiple Values 2 Accessing Array Elements 3 Inputting and Displaying Array Contents 4 Array Initialization 5 Using.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
C++ Programming Lecture 18 Pointers – Part II The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CSC 113: Computer Programming (Theory = 03, Lab = 01)
getline() function with companion ignore()
Chapter 2 part #3 C++ Input / Output
Learning Objectives String Class.
Arrays Kingdom of Saudi Arabia
Sequential input and output Operations in file
String What it is Why it’s useful
Arrays Arrays A few types Structures of related data items
EECE.2160 ECE Application Programming
Chapter 2 part #3 C++ Input / Output
Arrays.
C++ Programming Lecture 20 Strings
CS-161 Computer Programming Lecture 15 & 16: Arrays II
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Put the on A on the keyboard.
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.
getline() function with companion ignore()
4.1 Introduction Arrays A few types Structures of related data items
Presentation transcript:

EC-111 Algorithms & Computing Lecture #8 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST

Character Arrays  Strings  Arrays of characters –All strings end with null ( '\0' ) –Examples char string1[] = "hello"; –Null character implicitly added –string1 has 6 elements char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ }; –Subscripting is the same String1[ 0 ] is 'h' string1[ 2 ] is 'l'

Examples Using Arrays  Input from keyboard char string2[ 10 ]; cin >> string2; –Puts user input in string Stops at first whitespace character Adds null character  Printing strings – cout << string2 << endl; Does not work for other array types –Characters printed until null found

1 2 // Treating character arrays as strings. 3 #include int main() 10 { 11 char string1[ 20 ], // reserves 20 characters 12 char string2[] = "string literal"; // reserves 15 characters // read string from user into array string2 15 cout << "Enter the string \"hello there\": "; 16 cin >> string1; // reads "hello" [space terminates input] // output strings 19 cout << "string1 is: " << string1 20 << "\nstring2 is: " << string2; cout << "\nstring1 with spaces between characters is:\n"; 23

24 // output characters until null character is reached 25 for ( int i = 0; string1[ i ] != '\0'; i++ ) 26 cout << string1[ i ] << ' '; cin >> string1; // reads "there" 29 cout << "\nstring1 is: " << string1 << endl; return 0; // indicates successful termination } // end main Enter the string "hello there": hello there string1 is: hello string2 is: string literal string1 with spaces between characters is: h e l l o string1 is: there

Static & Automatic Local Arrays  Recall static storage –If static, local variables save values between function calls –Visible only in function body –Can declare local arrays to be static Initialized to zero static int array[3];  If not static –Created (and destroyed) in every function call

1 2 // Static arrays are initialized to zero. 3 #include void staticArrayInit( void ); // function prototype 9 void automaticArrayInit( void ); // function prototype int main() 12 { 13 cout << "First call to each function:\n"; 14 staticArrayInit(); 15 automaticArrayInit(); cout << "\n\nSecond call to each function:\n"; 18 staticArrayInit(); 19 automaticArrayInit(); 20 cout << endl; return 0; // indicates successful termination } // end main 25

26 // function to demonstrate a static local array 27 void staticArrayInit( void ) 28 { 29 // initializes elements to 0 first time function is called 30 static int array1[ 3 ]; cout << "\nValues on entering staticArrayInit:\n"; // output contents of array1 35 for ( int i = 0; i < 3; i++ ) 36 cout << "array1[" << i << "] = " << array1[ i ] << " "; cout << "\nValues on exiting staticArrayInit:\n"; // modify and output contents of array1 41 for ( int j = 0; j < 3; j++ ) 42 cout << "array1[" << j << "] = " 43 << ( array1[ j ] += 5 ) << " "; } // end function staticArrayInit 46

47 // function to demonstrate an automatic local array 48 void automaticArrayInit( void ) 49 { 50 // initializes elements each time function is called 51 int array2[ 3 ] = { 1, 2, 3 }; cout << "\n\nValues on entering automaticArrayInit:\n"; // output contents of array2 56 for ( int i = 0; i < 3; i++ ) 57 cout << "array2[" << i << "] = " << array2[ i ] << " "; cout << "\nValues on exiting automaticArrayInit:\n"; // modify and output contents of array2 62 for ( int j = 0; j < 3; j++ ) 63 cout << "array2[" << j << "] = " 64 << ( array2[ j ] += 5 ) << " "; } // end function automaticArrayInit

First call to each function: Values on entering staticArrayInit: array1[0] = 0 array1[1] = 0 array1[2] = 0 Values on exiting staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on entering automaticArrayInit: array2[0] = 1 array2[1] = 2 array2[2] = 3 Values on exiting automaticArrayInit: array2[0] = 6 array2[1] = 7 array2[2] = 8 Second call to each function: Values on entering staticArrayInit: array1[0] = 5 array1[1] = 5 array1[2] = 5 Values on exiting staticArrayInit: array1[0] = 10 array1[1] = 10 array1[2] = 10 Values on entering automaticArrayInit: array2[0] = 1 array2[1] = 2 array2[2] = 3 Values on exiting automaticArrayInit: array2[0] = 6 array2[1] = 7 array2[2] = 8

Passing Arrays to Functions  Specify name without brackets –To pass array myArray to myFunction int myArray[ 24 ]; myFunction( myArray, 24 );

Passing Arrays to Functions  Arrays passed-by-reference –Functions can modify original array data –Value of name of array is address of first element Function knows where the array is stored Can change original memory locations  Individual array elements passed-by-value –Like regular variables – square( myArray[3] );

Passing Arrays to Functions  Functions taking arrays –Function prototype void modifyArray( int b[], int arraySize ); void modifyArray( int [], int ); –Names optional in prototype Both take an integer array and a single integer –No need for array size between brackets Ignored by compiler

1 2 // Passing arrays and individual array elements to functions. 3 #include #include void modifyArray( int [], int );  void modifyElement( int ); int main() 16 { 17 const int arraySize = 5; // size of array a 18 int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize a cout << "Effects of passing entire array by reference:" 21 << "\n\nThe values of the original array are:\n"; // output original array 24 for ( int i = 0; i < arraySize; i++ ) 25 cout << setw( 3 ) << a[ i ]; Syntax for accepting an array in parameter list.

26 27 cout << endl; // pass array a to modifyArray by reference 30 modifyArray( a, arraySize ); cout << "The values of the modified array are:\n"; // output modified array 35 for ( int j = 0; j < arraySize; j++ ) 36 cout << setw( 3 ) << a[ j ]; // output value of a[ 3 ] 39 cout << "\n\n\n" 40 << "Effects of passing array element by value:" 41 << "\n\nThe value of a[3] is " << a[ 3 ] << '\n'; // pass array element a[ 3 ] by value 44 modifyElement( a[ 3 ] ); // output value of a[ 3 ] 47 cout << "The value of a[3] is " << a[ 3 ] << endl; return 0; // indicates successful termination } // end main

52 53 // in function modifyArray, "b" points to 54 // the original array "a" in memory 55 void modifyArray( int b[], int sizeOfArray ) 56 { 57 // multiply each array element by 2 58 for ( int k = 0; k < sizeOfArray; k++ ) 59 b[ k ] *= 2; } // end function modifyArray // in function modifyElement, "e" is a local copy of 64 // array element a[ 3 ] passed from main 65 void modifyElement( int e ) 66 { 67 // multiply parameter by 2 68 cout << "Value in modifyElement is " 69 << ( e *= 2 ) << endl; } // end function modifyElement

Effects of passing entire array by reference: The values of the original array are: The values of the modified array are: Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6

1 2 // Demonstrating the const type qualifier. 3 #include void tryToModifyArray( const int [] ); // function prototype 9 10 int main() 11 { 12 int a[] = { 10, 20, 30 }; tryToModifyArray( a ); cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n'; return 0; // indicates successful termination } // end main 21

22 // In function tryToModifyArray, "b" cannot be used 23 // to modify the original array "a" in main. 24 void tryToModifyArray( const int b[] ) 25 { 26 b[ 0 ] /= 2; // error 27 b[ 1 ] /= 2; // error 28 b[ 2 ] /= 2; // error } // end function tryToModifyArray d:\cpphtp4_examples\ch04\Fig04_15.cpp(26) : error const object