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?