===== CS 111 - Week 13 Lecture 2 - 2024-11-21 ===== ===== TODAY WE WILL: ===== * announcements * a few about PSEUDOCODE * "walking" through a string using while loop * start intro to ARRAYS * prep for next class ===== * should be working on Homework 10! * at-least-first-attempts due by 11:59 pm this Friday, November 22 * YES, there IS lab, with a lab exercise, on Friday, November 22! ===== PSEUDOCODE ===== * for the design recipe complete-the-function-body step, PSEUDOCODE can be another tool that might help (in addition to templates such as the cond/if-else-if pattern, or the template for a function "walking through" a Racket list) * because, with local variables and mutation, we are getting more and more sequences of statements whose order might matter * pseudocode: * write out your logic -- your ALGORITHM -- in natural-languagish way, instead of directly using a programming language * it's fine to use *some* programming language bits if you like, but the idea is to NOT get bogged down in syntax while you try to work out the logic/algorithm * key is to write the steps IN THE ORDER they need to be taken * it's also good to INDENT in this -- indent actions to be repeated, indent actions that are part of a branch * OK to write "high level" steps for the first version * once you have your pseudocode, you can go back to it and, if needed, "break down" a higher level step into its lower-level steps to actually complete it ^ you might decide a higher-level step would be a useful helper function, and then go and design and test that function, and then come back! * keep repeating until you realize you can now translate the resulting pseudocode into C++ (or whatever programming language you are using) * the idea of going from high-level steps and then replacing those with lower-order steps could be considered a kind of TOP-DOWN design * by the way: some prefer to draw a FLOWCHART (as we used to describe the while statement syntax on Tuesday) instead of pseudocode * (you may see some example pseudocode on Homework 11...) * for example -- pseudocode for today's example function vertical (kluged after class) * set up the current location in the string, initially 0 * while I'm not finished walking through the string * print the character at the current location on its own line * move to the next location * return the string's length ===== intro to C++ arrays ===== * ...our first C++ data structure! * it is very efficient! * all the elements are contiguous in memory (one after the other) * all of the elements in the array must be the same type * for static arrays (dynamic arrays are intro'd in CS 112), you MUST give its SIZE (how many elements) when you declare the array and you must give the TYPE of the elements in the array and you have to give it a name here's the syntax: desired_element_type desired_array_name[desired_size]; string cities_to_visit[10]; double milk_prices[30]; int chairs_per_room[7]; and to access an element in an array, you use its INDEX, its 0-based position within the array, written after the array name in square brackets // this assigns 65 to the 1st element -- at position 0 -- // in the array chairs_per_room chairs_per_room[0] = 65; // this assigns 4.99 to the 4th element -- at position 3 -- // in the array milk_prices milk_prices[3] = 4.99; ********* * IMPORTANT: as for local variables in general, you should INITIALIZE the values in an array before you try to USE them! * We will discuss several approaches to this. * But, the assignment statements above are two examples of initializing those particular array elements. ********* * IMPORTANT: C++ does not check as closely as some languages, so it is especially important to make sure your array index *is* in the range of your array -- since the index of the first element is 0, note that the index of the LAST element is its size - 1, NOT its size...! * for example: int looky[3]; * here is a common way that people visualize this array of 3 int values named looky: looky |------| 0 | | |------| 1 | | |------| 2 | | |------| * (we shouldn't assume what is in those locations yet, because the C++ language definition does not specify, and C++ compilers thus differ -- might be whatever happened to be in that memory before, might be something else!) * ONE way of initializing looky: because this IS a memory location you can change, you can use the array name followd by square brackets containing the desired element's index on the LEFT-hand-side of an assignment statement to initialize or change the value at that index: looky[0] = 13; looky |------| 0 | 13 | |------| 1 | | |------| 2 | | |------| looky[1] = 5; looky |------| 0 | 13 | |------| 1 | 5 | |------| 2 | | |------| looky[2] = 100; looky |------| 0 | 13 | |------| 1 | 5 | |------| 2 | 100 | |------| * and you can grab a particular array element using the array name followed by square brackets containing the desired element's index: // this would print 5 to the screen cout << looky[1] << endl; * and you can change a particular element's value as desired: looky[2] = 44; looky |------| 0 | 13 | |------| 1 | 5 | |------| 2 | 44 | |------| * can the array index be a variable? Yep! * Just make sure its type is int, and make sure its value is in the range of the array! * for example, this will print the elements of looky to the screen, one value per line: int index = 0; // want the index to be strictly LESS than the array's size! while (index < 3) { cout << looky[index] << endl; index = index + 1; }