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

Slides:



Advertisements
Similar presentations
Computer Programming Basics Assistant Professor Jeon, Seokhee Assistant Professor Department of Computer Engineering, Kyung Hee University, Korea.
Advertisements

Programming Character I/O. COMP102 Prog. Fundamentals I: Character I/O/ Slide 2 More on char Type l Constant Declaration: const char star = '*'; l Variable.
C++ Data Type String A string is a sequence of characters enclosed in double quotes. Examples of strings: “Hello” “CIS 260” “Students” The empty string.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 5/7/2010 Sungwon Yang
Character I/O. COMP104 Character I/O Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable.
Characters. COMP104 Lecture 21 / Slide 2 Data Type: char * Constant declaration const char star = '*'; * Variable declaration char resp; * Variable assignment.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
How Create a C++ Program. #include using namespace std; void main() { cout
Basic Elements of C++ Chapter 2.
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.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
Discussion 1E Jie(Jay) Wang Week 10 Dec.2.
Characters, Strings, and the cstring Library
Chapter Topics The Basics of a C++ Program Data Types
CS31 Discussion Jie(Jay) Wang Week8 Nov.18.
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Jie(Jay) Wang Week1 Sept. 30
Chapter 1.2 Introduction to C++ Programming
LESSON 2 Basic of C++.
CMSC 202 Lesson 2 C++ Primer.
Motivation and Overview
Documentation Need to have documentation in all programs
Characters, Strings, and the cstring Library
The Ohio State University
Basic Elements of C++.
Command Line Arguments
Chapter 2.1 Control Structures (Selection)
Pointers and Pointer-Based Strings
Andy Wang Object Oriented Programming in C++ COP 3330
Basic Elements of C++ Chapter 2.
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Chapter 12: Text Processing
Chapter 2 Elementary Programming
Chapter 12: Text Processing
Review for Final Exam.
String What it is Why it’s useful
Review for Final Exam.
C++ Pointers and Strings
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Pointers and Pointer-Based Strings
Arrays Arrays A few types Structures of related data items
CS250 Introduction to Computer Science II
Std Library of C++.
CS 144 Advanced C++ Programming February 12 Class Meeting
ENERGY 211 / CME 211 Lecture 9 October 10, 2008.
CS31 Discussion 1D Winter19: week 3
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS31 Discussion 1D Winter19: week 4
CISC181 Introduction to Computer Science Dr
Chapter 1 c++ structure C++ Input / Output
CS31 Discussion 1H Fall18: week 3
C++ Pointers and 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:

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

Outline Project 3 Debugging Array

Project 3 Read the spec and FAQ carefully. Test your code on SEASnet Linux server using the command curl -s -L http://cs.ucla.edu/classes/fall16/cs31/Utilities/p3tester | bash Go through two notes carefully. A technique for processing strings A note about characters and integers. Incremental development No assumption about the pollData parameter. It can be anything!. DDL: Monday, October 31

About spec Poll data string format Return value Pseudocode

String processing #include <string> #include <cctype> Operation What it does Example string s = “hello”; string s = “!!!”; Declare strings s and s2 s.length() or s.size() Return the length of s cout << s.size(); // prints 5 s[i] or s.at[i] Return i-th character. (i should be integer between 0 and size-1 (inclusive)) cout << s[1]; // prints ‘e’ cout << s.at(0); // prints ‘h’ s + s2 Concatenate two strings cout << s + s2; // prints “hello!!!” #include <cctype> Operation What it does char c; Declare a character c isspace(c) True if c is a whitespace character isalpha(c) True if c is a letter isdigit(c) True if c is a digit islower(c) True is c is a lowercase letter isupper(c) True if c is a uppercase letter

String processing In order to process characters in a string, E.g., string str = “123AFb32#@sd”; ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ for (int i = 0; i < str.size(); i++) { char ch = str[i]; // do something to ch } for loop caret int i = 0; while(i < str.size()){ char ch = str[i]; // do something to ch i++; } while loop

String processing Question: count the number of digits and letters in the string str. str #digit #letter “ABC12@cd” 2 5 “sd#12#12” 4 Operation What it does char c; Declare a character c isspace(c) True if c is a whitespace character isalpha(c) True if c is a letter isdigit(c) True if c is a digit islower(c) True is c is a lowercase letter isupper(c) True if c is a uppercase letter

String processing “Afbsd” Question: given a string, filter out all non-letter characters, and print out the new string which is concatenated by all the letters left. E.g., string str = “123AFb32#@sd”; string concatLetter(string str); “Afbsd” #include <string> Operation What it does Example s + s2 Concatenate two strings cout << s + s2; // prints “hello!!!” caret #include <cctype> Operation What it does isalpha(c) True if c is a letter isdigit(c) True if c is a digit

String processing Question: You are writing a program to filter out the illegal date records in the database, and return the number of legal records in December. The legal date string: year(4 digits) month(3 letters, all UPPERCASE) day (1/2 digits). The month is guaranteed to be all uppercase letters. Only care about number of characters! int filterCount(strint str); str Y/N 1993DEC3 Y 2004DEC52 12MAR3 N 2012AU15 2016OCT2 2

Characters and Integers ‘0’ 48 ‘1’ 49 ‘2’ 50 ‘3’ 51 ‘4’ 52 ‘5’ 53 ‘6’ 54 ‘7’ 55 ‘8’ 56 ‘9’ 57 ‘0’ is not mapped to 0! However, the integer code for chars ‘0’ through ‘9’ are contiguous. ASCII code

Characters and Integers ‘0’ 48 ‘1’ 49 ‘2’ 50 ‘3’ 51 ‘4’ 52 ‘5’ 53 ‘6’ 54 ‘7’ 55 ‘8’ 56 ‘9’ 57 char ch = '0'; ch++; // ch is '1' int a = ch - '0'; // a is 1 ch += 7; // ch is '8' a = ch - '0'; // a is 8

Characters and Integers Question: Given a string str which contains several digits (0-9), how do you derive the integer value that it represents? int cast(string str) Hint: ‘4’ -> int a = ‘4’ – ‘0’; // a is 4 ‘5’ -> int a = ‘5’ – ‘0’; // a is 5 45 = 4*10 + 5 str return value “123” 123 “45” 45

Debugging Demo on VC++ and XCode

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-boundary access not allowed! int a[10]; a[15] = 5; // error a[-10] = 4; // error a[9] = 10; // okay

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);

Quick tests Question: Will the code compile? If so, what’s the output? #include <iostream> #include <string> using namespace std; int main () { int flimsySize = 3; int i[flimsySize]; cout << i[1] << endl; } #include <iostream> #include <string> using namespace std; int main () { const int FLIMSY_SIZE = 3; int i[FLIMSY_SIZE]; cout << i[1] << endl; }

Quick tests Question: Will the code compile? If so, what’s the output? #include <iostream> #include <string> using namespace std; int main () { int i[3]; i[0] = 104; i[1] = 101; i[2] = 121; cout << i[0] << endl; cout << i[1] << endl; cout << i[2] << endl; } #include <iostream> #include <string> using namespace std; int main () { int i[3]; i = {104, 101, 121}; cout << i[0] << endl; cout << i[1] << endl; cout << i[2] << endl; } The initialization syntax is special at declaration.

We will cover more details about array next week.

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!