1 / 12

Administrative HW 6 is out ― due next Monday 3/3 @ 9pm

Administrative HW 6 is out ― due next Monday 3/3 @ 9pm Heads up ― you're writing *3 * different programs… Lab sections are meeting Thurs/Fri as usual Need help? We have TAs, we have tutors, we have Piazza H ours and locations: http://www.joehummel.net/cs109.html

brand
Download Presentation

Administrative HW 6 is out ― due next Monday 3/3 @ 9pm

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. Administrative • HW 6 is out ― due next Monday 3/3 @ 9pm • Heads up ― you're writing *3* different programs… • Lab sections are meeting Thurs/Fri as usual • Need help? We have TAs, we have tutors, we have Piazza • Hours and locations: http://www.joehummel.net/cs109.html • Next ― and final ― topic in C++? • Data structures… CS 109 C/C++ Programming for Engineers with MATLAB CS 109 -- 26 Feb 2014

  2. Where are we? • Logic skills: • Sequence • Selection • Repetition • Programming skills: • C++ • files • functions • What’s next? • Data structures: • ways to organize data inside the computer… CS 109 -- 26 Feb 2014

  3. Motivation — how to model / store data? "apple: a fruit…" "zebra: animal…" "catamaran: a yacht or…" CS 109 -- 26 Feb 2014

  4. linear equation 1D array of coefficients DNA two 1D char arrays of DNA "letters" Contacts "John Accrington" 1D array ofstrings "Helen Allsworth" CS 109 -- 26 Feb 2014

  5. matrix 2D array of coefficients 2D array of char CS 109 -- 26 Feb 2014

  6. 'O' 'X' • Example #1: Tic-Tac-Toe board in C++ • Using variables: • Using a 2D array: #include <iostream> #include <string> using namespace std; . . intmain() { char B11, B12, B13; char B21, B22, B23; char B31, B32, B33; init(B11, B12, B13, …); . . . #include <iostream> #include <string> using namespace std; . . intmain() { char board[3][3]; init(board); . . . CS 109 -- 26 Feb 2014

  7. Arrays start at 0, not 1… void init(charboard[3][3]) { board[0][0] = ' '; board[0][1] = ' '; board[0][2] = ' '; board[1][0] = ' '; board[1][1] = ' '; board[1][2] = ' '; board[2][0] = ' '; board[2][1] = ' '; board[2][2] = ' '; } OR void init(char& B11, char& B12, char& B13, char& B21, char& B22, char& B23, char& B31, char& B32, char& B33) { B11 = ' '; // set every position to empty: B12 = ' '; B13 = ' '; B21 = ' '; B22 = ' '; B23 = ' '; B31 = ' '; B32 = ' '; B33 = ' '; } void init(charboard[3][3]) { for (int r=0; r<3; r=r+1) { for (int c=0; c<3; c=c+1) { board[r][c] = ' '; } } } CS 109 -- 26 Feb 2014

  8. Example #2: strings in C++ #include <iostream> #include <string> using namespace std; . . intmain() { string s; s = "60605"; if (s[0] == '6') // look at first digit: { cout << "Chicago zip code?"; } A string is a 1D array of characters string object CS 109 -- 26 Feb 2014

  9. Arrays: • The most important data structure in Computer Science • Available in all programming languages: C, C++, Java, … • Most programs contain arrays… elements indices Def: an array is a sequence of elements of a single type ― e.g. int, double, or string. Given an array of N elements, their positions are indexedby 0 .. N-1; the [ ] operator is used to access individual elements. intA[100]; // declare an array of 100 elements: for (inti=0; i<100; i=i+1) A[i] = 0; // set element to 0: CS 109 -- 26 Feb 2014

  10. for (initial; condition; advance) { . . . } • The For loop: • the For loop is a specialized counting loop // count 0, 1, 2, …, N-1: inti; intN; cin>> N; i = 0; while (i< N) { cout << i; cout << endl; i = i + 1; } // count 0, 1, 2, …, N-1: inti; intN; cin>> N; for (i=0;i< N;i=i+1) { cout << i; cout << endl; } CS 109 -- 26 Feb 2014

  11. Example: "vector add" using arrays… • V3 = V1 + V2 // V3 = V1 + V2 int V1[N]; int V2[N]; int V3[N]; . . // fill vectors V1, V2: . for(inti=0; i<N; i=i+1) { V3[i] = V1[i] + V2[i]; } V1 V2 V3 inti; i = 0; while(i < N) { V3[i] = V1[i] + V2[i]; i = i + 1; } CS 109 -- 26 Feb 2014

  12. VectorAdd function: #include <iostream> . . . #define N 100 // // V3 = V1 + V2 // void VectorAdd(int V3[N], int V1[N], int V2[N]) { for (inti=0; i<N; i=i+1) { V3[i] = V1[i] + V2[i]; } } intmain() { int V1[N], V2[N], V3[N]; . . . VectorAdd(V3, V1, V2);

More Related