1 / 17

CS31: Introduction to Computer Science I

CS31: Introduction to Computer Science I. Discussion 1A 4/30/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang. Quick Review. What did we learn last week? functions function definition (proto type) function call return values passing arguments by value

Download Presentation

CS31: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS31: Introduction to Computer Science I Discussion 1A 4/30/2010 Sungwon Yang swyang@cs.ucla.edu www.cs.ucla.edu/~swyang

  2. Quick Review • What did we learn last week? • functions • function definition (proto type) • function call • return values • passing arguments by value • cannot access variables outside • passing arguments by reference • can access variables outside • some tips related to project #3 • scan each character in string • for ( size_t i= 0 ; i < string.size() ; i++ ) • access next characters • string.at(i+1), string.at(i+2), … , string.at(i+k) • condition check required : i+k < string.size() • convert character to integer • switch-case statement • atoi() function • ASCII code property : ‘3’ – ‘0’ = 3

  3. Arrays • can create multiple variables at one time • suppose that we want to create 10 integer variables • int x1, x2, x3, …, x9, x10; • what if we want to create 100 or 1000 variables? • int x1, x2, x3, …, x99, x100, …, x999, x1000; ???? • can do this in a single statement • int x[100]; or int x[1000]; • the range of index is 0 to 99(999) : the index starts from 0 • each item of the array can treated as regular variables • x[0] = 5; • cout << x[99] << endl; • x[10] = x[20] + x[30]; • x[50]++;

  4. array declaration • must specify the length of the array • the number must be a constant positive integer • value itself (e.g. 5, 10, 1000, 10000) • a variable explicitly declared to be a constant int main () { int a[10]; } int main () { const int num = 10; int a[num]; } int main () { int num = 10; int a[num]; } OK OK Compile ERROR

  5. initialization of an array int main () { int a[5] = {0,1,2,3,4}; for ( int i=0 ; i < 5 ; i++ ) { cout << a[i] << endl; } } int main () { int a[] = {0,1,2,3,4}; for ( int i=0 ; i < 5 ; i++ ) { cout << a[i] << endl; } } int main () { int a[5] = {0,1,2}; for ( int i=0 ; i < 5 ; i++ ) { cout << a[i] << endl; } } 0 1 2 3 4 0 1 2 3 4 0 1 2 0 0

  6. wrong initializations int main () { int a[5] = {0,1,2,3,4,5}; } int main () { int a[5] ; a[5] = {0,1,2,3,4}; } int main () { int a[5] = 0; } values should be enclosed in braces. We cannot set the size to what’s less than the number of elements in the initialization statement This is not an initialization. It is not allowed to assign multiple values, which are enclosed in braces, into an array.

  7. accessing out of bound array values int main () { int a[5] = {0,1,2,3,4}; for ( int i=0 ; i < 8 ; i++ ) { cout << a[i] << endl; } } int main () { int a[5] = {0,1,2,3,4}; a[5] = 50; a[6] = 60; for ( int i=0 ; i < 8 ; i++ ) { cout << a[i] << endl; } } They may cause run-time error, but they won’t cause compile error. C++ compiler does not check the bound of index in an array 0 1 2 3 4 -858993460 2947036 20847160 0 1 2 3 4 50 60 20847160

  8. arrays in functions • passing individual array items • indexed array is just a variable • can pass by value or by reference void display( int arg ) { cout << arg << endl; } int main () { int a[5] = {0,1,2,3,4}; for (int i = 0 ; i < 5 ; i++) { display( a[i] ); } } 0 1 2 3 4 void display( int& arg ) { cout << arg << endl; } int main () { int a[5] = {0,1,2,3,4}; for (int i = 0 ; i < 5 ; i++) { display( a[i] ); } }

  9. arrays in functions • passing entire array • not actually passing by reference, but can be regarded as a reference void change(int arg[], int size) { for (int i = 0; i < size ; i++) { arg[i] = arg[i]*arg[i]; } } int main () { int a[5] = {0,1,2,3,4}; change(a, 5); for ( int i = 0; i < 5 ; i++) { cout << a[i] << endl; } } 0 1 4 9 16 void display(int arg[], int size) { for (int i = 0; i < size ; i++) { cout << arg[i] << endl; } } int main () { int a[5] = {0,1,2,3,4}; display(a, 5); } 0 1 2 3 4

  10. const keyword • adding const keyword before argument protects the items of the array from accidentally being changed void change(const int arg[], int size) { for (int i = 0; i < size ; i++) { arg[i] = arg[i]*arg[i]; } } int main () { int a[5] = {0,1,2,3,4}; change(a, 5); for ( int i = 0; i < 5 ; i++) { cout << a[i] << endl; } } compile error void display(const int arg[], int size) { for (int i = 0; i < size ; i++) { cout << arg[i] << endl; } } int main () { int a[5] = {0,1,2,3,4}; display(a, 5); } 0 1 2 3 4

  11. Project #4 • produce a library providing functions for many common manipulations of arrays of strings • 10 functions required to be implemented • take at least two parameters

  12. functions to be implemented • int tally(const string a[], int n, string target); • Return the number of strings in the array that are equal to target • for or while loop • int findFirst(const string a[], int n, string target); • Return the position of the first string in the array that is equal to target • for or while loop (pp. 201, Savitch’s book, 3rd edition)

  13. functions to be implemented • int positionOfMin(const string a[], int n); • Return the position of a string in the array such that that string is <= every string in the array • If there is more than one such string, return the smallest position of such a string. • Return −1 if the array has no elements • initially set the 0-th value to the smallest one and compare with others. • int moveToEnd(string a[], int n, int pos); • Eliminate the item at position pos by copying all elements after it one place to the left • Put the item that was thus eliminated into the last position of the array. • Return the original position of the item that was moved to the end. • first, declare a string variable and store the item at position pos.

  14. functions to be implemented • int moveToBeginning(string a[], int n, int pos); • Eliminate the item at position pos by copying all elements before it one place to the right. • Put the item that was thus eliminated into the first position of the array. • Return the original position of the item that was moved to the beginning. • first, declare a string variable and store the item at position pos. • be careful with the manner of looping, increasing? or decreasing?? • int disagree(const string a1[], int n1, const string a2[], int n2); • Return the position of the first corresponding elements of a1 and a2 that are not equal. • n1 is the number of interesting elements in a1, and n2 is the number of interesting elements in a2. • If the arrays are equal up to the point where one or both runs out, return the smaller of n1 and n2. • set the number of iterations to the smaller number between n1 and n2. • (n1 <= n2? or n1 > n2??)

  15. functions to be implemented • int removeDups(string a[], int n); • For every sequence of consecutive identical items in a, retain only one item of that sequence. • Suppose we call the number of retained items r. Then when this functions returns, elements 0 through r-1 of a must contain the retained items (in the same relative order they were in originally), and the remaining elements may have whatever values you want. • Return the number of retained items • if the next item is duplicated one, eliminate the current item by copying all elements after it one place to the left. • if duplicate found, stay current position, otherwise, move to the next position • bool subsequence(const string a1[], int n1, const string a2[], int n2); • If all n2 elements of a2 appear in a1, in the same order (though not necessarily consecutively), then return true. • Return false if a1 does not contain a2 as a subsequence. (Of course, the empty sequence is a subsequence of any sequence.) • Return false (instead of −1) if this function is passed any bad arguments. • using nested loop • outer loop : n2 • inner loop : n1

  16. functions to be implemented • int mingle(const string a1[], int n1, const string a2[], int n2, string result[], int max); • If a1 has n1 elements in nondecreasing order, and a2 has n2 elements in nondecreasing order, place in result all the elements of a1 and a2, arranged in nondecreasing order, and return the number of elements so placed. • Return −1 if the result would have more than max elements or if a1 and/or a2 are not in nondecreasing order. (Note: nondecreasing order means that no item is > the one that follows it.) • first, check each array is in nondecreasing order and n1+n2 <= max • store items in both arrays in result array, then sort them • pp. 201, Savitch’s book, 3rd edition • int divide(string a[], int n, string divider); • Rearrange the elements of the array so that all the elements whose value is < divider come before all the other elements, and all the elements whose value is > divider come after all the other elements. • Return the position of the first element that, after the rearrangement, is not < divider, or n if there are none. • sort the array, then find the right position

  17. Midterm practice • Solutions will be posted on Saturday midnight • http://www.cs.ucla.edu/~swyang

More Related