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

Similar documents
CS31 Discussion 1E. Jie(Jay) Wang Week5 Oct.27

CS 31 Discussion ABDULLAH-AL-ZUBAER IMRAN WEEK 6: C-STRINGS, STRUCT, CLASS AND PROJECT4

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Week 5. Muhao Chen.

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

CS31 Discussion. Jie(Jay) Wang Week8 Nov.18

CS31 Discussion 1E. Jie(Jay) Wang Week1 Sept. 30

CS2141 Software Development using C/C++ C++ Basics

Characters, c-strings, and the string Class. CS 1: Problem Solving & Program Design Using C++

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

CSCE121: Introduction to Program Design and Concepts Practice Questions for Midterm 1

Name. CPTR246 Spring '17 (100 total points) Exam 2

Discussion 1H Notes (Week 4, April 22) TA: Brian Choi Section Webpage:

C++ For Science and Engineering Lecture 12

pointers + memory double x; string a; int x; main overhead int y; main overhead

BITG 1113: Array (Part 2) LECTURE 9

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Getting started with C++ (Part 2)

Exercise 1.1 Hello world

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

CS31 Discussion 1E Spring 17 : week 08

CS 31 Review Sheet. Tau Beta Pi - Boelter Basics 2. 2 Working with Decimals 2. 4 Operators 3. 6 Constants 3.

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Module 07: Programming in C++

CS 103 Lab - Party Like A Char Star

Computer Programming

Strings and Stream I/O

Engineering Problem Solving with C++, Etter

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

Integer Data Types. Data Type. Data Types. int, short int, long int

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Review of Important Topics in CS1600. Functions Arrays C-strings

CS 103 Lab 6 - Party Like A Char Star

Bruce Merry. IOI Training Dec 2013

Review Questions for Final Exam

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

A First Program - Greeting.cpp

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Week 1: Hello World! Muhao Chen

Lecture 23: Pointer Arithmetic

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

CS 31 Discussion: Week 6. Taylor Caulfield

Strings in C++ Dr. Ferdin Joe John Joseph Kamnoetvidya Science Academy

Midterm Practice TA: Brian Choi Section Webpage:

CE221 Programming in C++ Part 1 Introduction

Intermediate Programming, Spring 2017*

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Pointers and References

CS201- Introduction to Programming Current Quizzes

W3101: Programming Languages C++ Ramana Isukapalli

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

C-String Library Functions

Chapter 2: Basic Elements of C++

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

EE 355 Lab 4 - Party Like A Char Star

CMPS 221 Sample Final

Consider the above code. This code compiles and runs, but has an error. Can you tell what the error is?

Unit 14. Passing Arrays & C++ Strings

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CS24 Week 3 Lecture 1

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Week 3: Pointers (Part 2)

Overloading Functions & Command Line Use in C++ CS 16: Solving Problems with Computers I Lecture #6

Goals of this Lecture

CS 0449 Sample Midterm

C++ for Java Programmers

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Lab#5 Due Wednesday, February 25, at the start of class. Purpose: To develop familiarity with C++ pointer variables

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

Homework Assignment #2 (revised)

Classes in C++98 and C++11

Chapter 9: Pointers Co C pyr py igh i t gh Pear ea so s n n E ducat ca io i n, n Inc. n c.

Computers Programming Course 10. Iulian Năstac

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

CSc Introduction to Computing

Chapter 1 INTRODUCTION

Lab 8. Follow along with your TA as they demo GDB. Make sure you understand all of the commands, how and when to use them.

CSCE Practice Midterm. Data Types

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

My First Command-Line Program

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

CMSC 202 Midterm Exam 1 Fall 2015

More Examples Using Functions and Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #6

Structured Types. 8. Arrays. collection of components whose organization is characterized by method used to access individual components.

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

Transcription:

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

Initialization of an Array int a[5] = {1, 2, 3, 5, 7; int a[] = {1, 2, 3, 5, 7; 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[3] = {1, 2, 3, 5, 7; // wrong int a[10] = {1, 2, 3, 5, 7; // okay

Common mistakes int a[10]; for (int i = 0; i < a.size(); i++) {... No size() function is defined for arrays. 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]; int i[rows][columns] = { {row_00, row_01,..., {row_10, row11,...,..., {row_i0, row_i1,...; some2darray[i][j]; #include <iostream> using namespace std; int main () { int i[][3] = { {1, 2, 3, {4, 5, 6, {20, -1, 0 ;

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 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; Output: 1 int foo (int a); int main () { int i = 1; foo(i); cout << i << endl; int foo (int a) { return a++;

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

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). char s[10]; The maximal length of this string is 9 (not 10) Note that for string, there is no null terminator.

C Strings To initialize a string char s[10] = "HOWAREYOU"; H O W A R E Y O U \0 char s[] = "HOWAREYOU"; char s[10] = hello"; h e l 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 O U \0 H O W \0 R E Y O U \0 Output: HOW

C Strings Question: Will this code compile? What s the output? #include <iostream> using namespace std; Output: int main() { char s[10]; s = "abcdefg"; cout << s << endl; 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 \0

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

Functions for C Strings #include <cstring> Operation What it does strlen(s) Returns the length of s, not counting \0. strcpy(t,s) strncpy(t,s,n) 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.) Copies the first n characters of s to t. (Note: No size check.) strcat(t,s) strcmp(t,s) Appends s to the end of t. (Notes: No size check. t += s won t do the job.) 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 char s[10][20]; In s, we can store up to 10 C strings, and each C string can be at most 19 characters long. 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. char cs[10] = "hello"; string cpps; cpps = cs; string cpps = cs; string cpps(cs); C string to string You cannot: char cs[10] = cpps.c_str(); char cs[10]; cs = cpps.c_str(); char cs[10]; string cpps = "hello"; strcpy(cs, cpps.c_str()); string to c string

C Strings Question: Will this code compile? What s the 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 3 0

C Strings Question: Will this code compile? What s the 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?"; char a[100] char b[] s u p \0 \0 m y b r o? \0 strcat(a, b); cout << a << endl; 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!