CS31 Discussion Jie(Jay) Wang Week6 Nov.4.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Advertisements

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.
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.
Chapter 10.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
CS 31 Discussion, Week 6 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang
Arrays, Vectors, and Strings Allocation and referencing.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
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.
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Introduction Programs which manipulate character data don’t usually just deal with single characters, but instead with collections of them (e.g. words,
Discussion 1E Jie(Jay) Wang Week 10 Dec.2.
Pointers and Dynamic Arrays
Computer Organization and Design Pointers, Arrays and Strings in C
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
Chapter 7 Pointers and C-Strings
Jie(Jay) Wang Week1 Sept. 30
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Fundamentals of Characters and Strings
Motivation and Overview
CS31 Discussion 1E Jie(Jay) Wang Week5 Oct.27.
Command Line Arguments
Pointers and Pointer-Based Strings
Student Book An Introduction
Strings A string is a sequence of characters treated as a group
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Week 9 – Lesson 1 Arrays – Character Strings
String What it is Why it’s useful
CPS120: Introduction to Computer Science
Engineering Problem Solving with C++, Etter
7 Arrays.
Overloading functions
C++ Pointers and Strings
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Exercise Arrays.
C++ Programming Lecture 20 Strings
Pointers and dynamic objects
Lecture 2 Arrays & Pointers September 7, 2004
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1H Fall18: week 6
CISC181 Introduction to Computer Science Dr
Chapter 1 c++ structure C++ Input / Output
C++ Pointers and Strings
Dr. Khizar Hayat Associate Prof. of Computer Science
Presentation transcript:

CS31 Discussion Jie(Jay) Wang Week6 Nov.4

Outline Project 4 Array C Strings

Project 4 DDL: Monday, November 7 Read the spec & FAQ carefully Incremental development You can call your function in other functions If you wish, your implementation of a function required here may call other functions required here. (spec)

Project 4 Return value All functions that return an int must return -1 if they are passed any bad arguments (e.g., a negative array size, or a position that would require looking at the contents of an element past the last element we’re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array. Empty string (i.e., the string “”) Empty string (i.e., the string “”) is just another string, no more special than “hillary” or “donald”. N equals 0 Do what makes sense. The empty sequence is a subsequence of every sequence (even an empty one), at position 0.

Array Declare an array a[i] is an i-th variable in the array a. <type> <name>[size] a[i] is an i-th variable in the array a. size should must be a positive integer constant. You can treat each element of the array as a variable. 1 2 3 int a[4]; const int N = 10; int a[N]; int a[4]; x[3] = 5; x[1]++; cout << x[i] << endl;

Initialization of an Array You cannot set the size to what’s less than the number of elements in the initialization statement. However, it is okay to set the size to what’s more than the number of elements in the initialization statement. int a[5] = {1, 2, 3, 5, 7}; int a[] = {1, 2, 3, 5, 7}; int a[3] = {1, 2, 3, 5, 7}; // wrong int a[10] = {1, 2, 3, 5, 7}; // okay

Common mistakes No size() function is defined for arrays. int a[10]; for (int i = 0; i < a.size(); i++) { ... } const int SIZE = 10; int a[SIZE]; for (int i = 0; i < SIZE; i++) { ... }

Common mistakes Out-of-range access not allowed! int a[10]; a[15] = 5; // error a[-10] = 4; // error a[9] = 10; // okay

Array Multi-dimensional arrays int i[ROWS][COLUMNS]; #include <iostream> using namespace std; int main () { int i[][3] = { {1, 2, 3}, {4, 5, 6}, {20, -1, 0} }; } int i[ROWS][COLUMNS] = { {row_00, row_01, ...}, {row_10, row11, ...}, ..., {row_i0, row_i1, ...}}; some2DArray[i][j];

Arrays in a Function void fibo10(int fib[]); Note that the size of fib is not specified, you can explicitly pass the size in the function. void fibo10(int fib[], int n); Now after you learnt about 2D arrays. void fibo10(int fib[][], int n);

Arrays in a Function Pass by Value When C++ functions pass arguments by default for basic types like int, double, and string, Pass by Value The function parameters are copies of the arguments Question: Will the code compile? If so, what’s the output? #include <iostream> #include <string> using namespace std; int foo (int a); int main () { int i = 1; foo(i); cout << i << endl; } int foo (int a) { return a++; Output: 1

Arrays in a Function Pass by Reference #include <iostream> #include <string> using namespace std; int foo (int& a); int main () { int i = 1; foo(i); cout << i << endl; } int foo (int& a) { return a++; Output: 2

Arrays in a Function Question: Will the code compile? If so, what’s the output? #include <iostream> #include <string> using namespace std; int foo (int a[]); int main () { int i[] = {1, 2}; foo(i); cout << i[0] << endl; } int foo (int a[]) { return a[0]++; Output: 2 Pass by Pointer int i[] 1 2

C Strings Why we learn C strings? An array of characters C strings are null-terminated Every C string ends with a marker called the zero byte (null character), which we use an escape sequence \0 to represent (its ASCII number is 0). The maximal length of this string is 9 (not 10) Note that for string, there is no null terminator. 1.Many libraries are written in C, which doesn't have the std::string type that we've been using, so we have to know something about C strings.  2.Also, the convenience of std::strings comes at a cost above using C strings instead, so high-performance C++ apps, like some games, use C strings in the performance-critical parts of the code.  The use of the word "escape" really means to temporarily escape out of parsing the text and into a another mode where the subsequent character is treated differently. char s[10];

C Strings × To initialize a string char s[10] = "HOWAREYOU"; \0 char s[] = "HOWAREYOU"; char s[10] = “hello"; h e l o \0 × char s[10] = “Hello, world!";

C Strings Question: Will this code compile? What’s the output? #include <iostream> using namespace std; int main() { char s[10] = "HOWAREYOU"; s[3] = '\0'; cout << s << endl; } H O W A R E Y U \0 H O W \0 R E Y U Output: HOW

C Strings Question: Will this code compile? What’s the output? #include <iostream> using namespace std; int main() { char s[10]; s = "abcdefg"; cout << s << endl; } Output: Cannot compile. Direct assignment like that works only in an initialization expression.

C Strings Question: Will this code compile? What’s the output? #include <iostream> #include <string> using namespace std; int main () { char a[3] = "ca\0"; cout << a << endl; } Output: c a \0 \0

C Strings Question: Will this code compile? What’s the output? #include <iostream> #include <string> using namespace std; int main () { char a[4] = "ca\0"; cout << a << endl; } Output: ca c a \0

C Strings Question: Will this code compile? What’s the output? Output: #include <iostream> #include <string> using namespace std; int main() { string s = "hello"; cout << s.size() << ' '; s[1] = '\0'; cout << s.size() << '\n'; } Output: 5 5

Functions for C Strings #include <cstring> Operation What it does strlen(s) Returns the length of s, not counting ‘\0’. strcpy(t,s) Copies the string s to t. (Notes: t=s won’t do the job. Also, strcpy doesn’t do the size check for you. You must make sure there’s enough space in t yourself.) strncpy(t,s,n) Copies the first n characters of s to t. (Note: No size check.) strcat(t,s) Appends s to the end of t. (Notes: No size check. t += s won’t do the job.) strcmp(t,s) Compares t and s. Returns 0 if they are equal, something greater than 0 if t > s, and something less than 0 if t < s. (Note: t == s or t < s won’t do the job.)

C Strings Two alternatives to traverse a C string. char s[10] = "HOWAREYOU"; for (int k = 0; t[k] != '\0'; k++) cout << t[k] << end; #include<cstring> ... char s[10] = "HOWAREYOU"; for (int k = 0; k < strlen(s); k++) cout << t[k] << end;

C Strings An array of C strings In s, we can store up to ___ C strings, and each C string can be at most ___ characters long. char s[10][20]; 10 19 s[3]; // refer to the string in position 3 s[3][5]; // refer to the letter in position 5 of the string in position 3

C Strings Convert a C string into a C++ string, and vice versa. You cannot: char cs[10] = "hello"; string cpps; cpps = cs; string cpps = cs; string cpps(cs); char cs[10]; string cpps = "hello"; strcpy(cs, cpps.c_str()); C string to string string to c string char cs[10] = cpps.c_str(); char cs[10]; cs = cpps.c_str();

C Strings Question: Will this code compile? What’s the output? Output: #include <iostream> #include <cstring> using namespace std; int main () { char a[] = "sup"; char b[] = "hey\0you"; char c[] = {'\0‘}; cout << strlen(a) << endl; cout << strlen(b) << endl; cout << strlen(c) << endl; } Output: 3

C Strings Question: Will this code compile? What’s the output? Output: #include <iostream> #include <cstring> using namespace std; int main () { char a[]= "sup"; char A[] = "SUP"; cout << strcmp(a, A) << endl; cout << strcmp(A, a) << endl; } Output: 32 -32 ASCII table

C Strings Question: Will this code compile? What’s the output? #include <iostream> #include <cstring> using namespace std; int main () { char a[100] = "sup\0"; char b[] = " my bro?"; strcat(a, b); cout << a << endl; } char a[100] s u p \0 ⋯⋯ char b[] m y b r o ? \0 char a[100] s u p m y b r Output: sup my bro? o ? \0 ⋯⋯

Programming practice Design a function, csReverse, that takes in a cstring and reverses the order of characters in it. void csReverse (char c[]);

Programming practice Design a function, multiCStringAppend, that takes in an array of cstrings and a single cstring, and appends toAppend to each of n elements of target. void multiCStringAppend ( char target[][MAX_CSTRING_SIZE], char toAppend[], int n);

Credit to 2 previous CS31 TAs This slide is finished with reference from: Andrew Forney http://web.cs.ucla.edu/~forns/ Brian Choi http://netlab.cs.ucla.edu/~schoi/cs31/

Thanks!