CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang

Slides:



Advertisements
Similar presentations
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Advertisements

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.
Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Pointers and Strings. Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close relationship with arrays and strings.
Chapter 10.
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
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 8 Arrays and Strings
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Review of C++ Programming Part II Sheng-Fang Huang.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
In Addition... To the string class from the standard library accessed by #include C++ also has another library of string functions for C strings that can.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
One Dimensional Arrays (Part2) Sorting Algorithms Searching Algorithms Character Strings The string Class. 1.
Chapter 8 Arrays and Strings
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
1 C++ Syntax and Semantics, and the Program Development Process.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Pointers OVERVIEW.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
1 CS161 Introduction to Computer Science Topic #13.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Object-Oriented Programming in C++
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
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.
 2000 Deitel & Associates, Inc. All rights reserved Introduction Pointers –Powerful, but difficult to master –Simulate call-by-reference –Close.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.
1 Character Strings (Cstrings) Reference: CS215 textbook pages
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
CS31 Discussion Jie(Jay) Wang Week6 Nov.4.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Introduction to C++ Programming
Pointers, Dynamic Data, and Reference Types
Pointers and Pointer-Based Strings
Today’s Lecture I/O Streams Tools for File I/O
Chapter 15 Pointers, Dynamic Data, and Reference Types
Introduction to C++ Programming
Engineering Problem Solving with C++, Etter
7 Arrays.
CS150 Introduction to Computer Science 1
Presentation transcript:

CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang

Quick Review What did we learn last week? C-strings string type can be used only in c++ C-string is an array of characters it must end with zero byte/null character : ‘\0’ – indicates the end of the string C-string input/output cout prints out characters until it meets null character cin.getline reads one line – need to specify the maximum length of the input string – null character will be automatically inserted converting C-strings to C++ strings simple ‘=‘ operator works not vice versa C-string functions : #include strlen(s) – returns the length of s, not counting ‘\0’ strcpy(t, s) – copies the string s to t strcat(t, s) – concatenate (append) s to the end of t strcmp(t, s) – compare t and s – return 0 if they are equal – return something greater than 0 if t > s – return something less than 0 if t < s

Pointers where are variables stored? – how can computers know where they are?? int main () { int a = 10; double d = 5.5; char c = ‘a’; bool b = true; } 10 ⁞ ⁞ 5.5 ⁞ a ⁞ true 001FFD08 ⁞ ⁞ 002FDC68 ⁞ 002FF128 ⁞ 004DEF00

Pointers Variables are stored in memory locations The memory location of a variable is called the address of the variable The address specifies a place, not a value Pointer variables store the address!

pointers int main () { int a = 10; int b = 100; int* ptr1; int *ptr2; ptr1 = &a; ptr2 = &b; int sum = *ptr1 + *ptr2; cout << ptr1 << " : " << ptr2 << endl; cout << &ptr1 << " : " << &ptr2 << endl; cout << a << " : " << b << endl; cout << *ptr1 << " : " << *ptr2 << endl; cout << sum << endl; } 001AF73C : 001AF AF744 : 001AF :

pointers multiple pointers can point to the same variable int main () { int* ptr1; int *ptr2; int a = 10; ptr1 = &a; ptr2 = ptr1; cout << *ptr1 << endl; cout << *ptr2 << endl; } 10

quick questions what will be displayed? int main () { int a = 10; int *ptr = &a; int *p = ptr; cout << a << endl; cout << *&a << endl; cout << *ptr << endl; cout << *p << endl; } 10 int main () { int* ptr1; int *ptr2; int a = 10; int b = 100; ptr1 = &a; ptr2 = &b; a = *ptr2+2*a; b = *&b*b; cout << b+*ptr1 << endl; } 10120

pointers and arrays The name of an array is a pointer to its first element(item) int main () { int arr[10] = {0,1,2,3,4,5,6,7,8,9}; cout << arr << endl; cout << &arr[0] << endl; } 22FE38DF

array element access when an array is created, items are allocated in consecutive memory spaces – if we know the beginning address, we know the locations of all the items. int main () { int i = 5; int arr[10] = {10,1,2,3,4,5,6,7,8,9}; cout << *arr << endl; cout << *(arr+ 1) << endl; cout << *(arr + 2) << endl; cout << *arr + 3 << endl; cout << *arr + 4 << endl; cout << *(arr + i) << endl; cout << *arr+ i+1 << endl; }

Pointers and functions Pointer is one type of variable – stores addresses Pointers can be function parameters – Value pointer parameters – Reference pointer parameters Pointers can be function return values – Value returned is a pointer

value parameter passing pointer by value – cannot change the address outside – but, can change the values pointers point to void func(int* arg1, int* arg2) { int* temp; temp = arg1; arg1 = arg2; arg2 = temp; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } 100 : : 100 void func(int* arg1, int* arg2) { *arg1 = 100; *arg2 = 10; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } 100 : 10

reference parameter passing pointer by reference – can change the address outside void func(int* &arg1, int* &arg2) { int* temp; temp = arg1; arg1 = arg2; arg2 = temp; cout << *arg1 <<" : "<< *arg2 << endl; } int main () { int a = 10, b = 100; int *ptr1 = &a, *ptr2 = &b; func(ptr1, ptr2); cout << *ptr1 <<" : "<< *ptr2 << endl; } 100 : 10

Dynamic memory allocation The compiler manages an area of memory called the heap for allocation by request We can request memory space “on-the-fly” from the heap area using the new operator The return value of the new operator is the address of the memory allocated – value must be saved in a pointer – The pointer must be used for data access

Dynamic Array Declaration array declaration requires specific size(length) new operator allows us to create an array dynamically int main () { int len; cin >> len; int arr[len];//ERROR } int main () { int len; cin >> len; int *arr = new int[len]; } size of len int array will be created arr stores the address of first array element

quick question what will be displayed? int main () { int len = 3, *ptr = &len; int *arr = new int[len]; *arr = *ptr; *(arr+1) = 20; *(arr+2) = *arr + *(arr+1); for (int i=0; i<len; i++) cout << *(arr+i) << endl; }

creation & destruction regular variables – created statically – destroyed automatically dynamic variables – created dynamically with new – destroyed manually with delete need to destroy variables before program terminates otherwise, it will cause memory leakage

memory leak dynamic variables do not have names – can refer to them only by pointers – what will happen if we lost the pointers? int main () { int *ptr; ptr = new int[100]; … ptr = new int[10]; … delete[] ptr; }

Project #5 Plain Text File Reformatter – reformats a plain text file into neatly arranged paragraphs with a particular maximum allowed line length – int reformat(istream& inf, ostream& outf, int lineLength); an already-opened input source you will read from (probably a file the caller opened). an already-opened output destination you will write to (probably either cout or an output file the caller created) an int with the desired maximum line length – return value 0 if is successful (i.e., neither of the following problems occurs) 1 if any input word portion is longer than the maximum line length 2 if the desired maximum line length is less than 1

definition of an word A word is a sequence of non-whitespace characters. – The function isspace tells you if a character is a whitespace character A word can be viewed as a sequence of one or more word portions WordWord portions Sungwon so-calledso- | called come-as-you-arecome- | as- | you- | are so--calledso- | - | called so- -so- | so

reformatting rules Fit as many word portions in an output line as you can without exceeding the line length. Output lines may well end up being different lengths, giving a "ragged-right" effect ItalwaysdoesseemtomethatIam doingmoreworkthanIshoulddo.Itis notthatIobjecttothework,mindyou; Ilikework:itfascinatesme.Ican sitandlookatitforhours.Iloveto keepitbyme:theideaofgettingrid ofitnearlybreaksmyheart.

reformatting rules Words in an output line must normally be separated by one blank. if the last character of an output word is a period, question mark, or exclamation point, it must be separated from any following word on that output line by two blanks. The last word on an output line must not be followed by any blanks. The first word portion on an output line must not be preceded by any blanks. Blanks must not appear on an output line within any word. The last line of output must end with a newline, and there must be no output lines (not even empty ones) after the last word of the last paragraph. ItalwaysdoesseemtomethatIam doingmoreworkthanIshoulddo.Itis notthatIobjecttothework,mindyou; Ilikework:itfascinatesme.Ican sitandlookatitforhours.Iloveto keepitbyme:theideaofgettingrid ofitnearlybreaksmyheart.

reformatting rules If a word portion is longer than the line length, as much as will fit must be on an output line by itself. The rest of that word portion must begin the next output line (and, of course, is subject to similar splitting if it's too long). If this situation ever occurs, your function continues reformatting, but it must eventually return 1 instead of 0 to its caller. Notice that this is the only situation where a word is allowed to be split across lines other than at a hyphen. abcdefghij klmnopqrst uvwxyz function returns 1

reformatting rules A paragraph break is indicated in the input by one or more consecutive lines that contain no words (i.e., are empty or contain only whitespace characters). The first word in the input following a paragraph break will be the first word of a new paragraph in the output. If a paragraph has already been output, the new paragraph must be separated from the one that precedes it by an empty line (i.e., a line with no characters other than the terminating newline). The very first output paragraph must not be preceded by an empty line Ilovetokeepitbyme:theideaof gettingridofitnearlybreaksmy heart. Youcannotgivemetoomuchwork;to accumulateworkhasalmostbecomea passionwithme:

more rules Your reformat function and any functions you write that it calls directly or indirectly must not use any std::string objects. If you need to use a string, use a C string. Your function may assume that no input line will be 200 or more characters long. This project is doable without assuming any upper limit for the output line length (the third parameter of reformat) — rather than storing the parts of an output line in a C string to be written later, you instead write them as soon as you can. However, some people may not figure out how to do that, so we'll give you a choice. If the third parameter of reformat is greater than 500, your implementation of reformat must either: – return 2 without writing any output – reformat the text using the indicated line length, returning 0 or 1 as appropriate (i.e., your algorithm doesn't impose any upper limit on the output line length). Implementing this choice correctly is worth 5 bonus points.

File I/O #include #include – ifstream input file stream type this object will be associated with a particular file – ofstream output file stream type this object will be associated with a particular file

Advice from Professor Break down the function into two major tasks – one fetches next word from the input text file identifies the next interest is a word, paragraph break, or the end of file if it is a word, get the word – the other one writes the fetched word according to the defined format will it be the first word in a line? will it be the last word in a line? does it end with period/question mark/exclamation?

getting next word basically deliver a single word to the reformat function – read single character in text file.get() function – need code that skips redundant spaces isspace() function – maybe helpful to provide additional information along with the word is this word preceded by a newline? is this word followed by a newline?

sample code bool getWord(istream& inf, char outWord[], int& wordLen) { char c; int index = 0; do { if ( ! inf.get(c)) return false; } while (isspace(c)); do { outWord[index] = c; index++; if ( ! inf.get(c)) break; } while( ! isspace(c)); outWord[index] = '\0'; wordLen = index; return true; } int main () { ifstream inf("input.txt"); char word[200]; int wordLen; while (getWord(inf, word, wordLen)) { cout << word << " : "; cout << wordLen << endl; } Hello everyone, my name is Sungwon Yang. Nice to meet you! Hello : 5 everyone, : 9 my : 2 name : 4 is : 2 Sungwon : 7 Yang. : 5 Nice : 4 to : 2 meet : 4 you! : 4

sample code it eliminates all spaces/newlines – need to add some code that identifies line breaks Hello everyone, my name is Sungwon Yang. Nice to meet you! Hello : 5 everyone, : 9 my : 2 name : 4 is : 2 Sungwon : 7 Yang. : 5 Nice : 4 to : 2 meet : 4 you! : 4

writing text in file it is very similar to cout object int main () { ofstream outText("result.txt"); char space = ' '; char newline = '\n'; char word1[10] = "Hello,"; char word2[10] = "how"; char word3[10] = "are"; char word4[10] = "you?"; outText << word1 << newline; outText << word2 << space << word3 << space << word4; } Hello, how are you?