Lecture 4/15: Lecture 5 Q&A


Lecture 5 Questions and Answers (Exported from Zoom Q&A log)

Q: can we add songs to the playlist?

A1: yeah! I can make it public on the website


Q: hi! quick pset question before lecture. when making the isPrime helper, can we insert a break?

A1: yes, break is used to exit a loop early


Q: Do grids need to be a perfect square, or rather a rectangle?

A1: live answered


Q: Can grid be rectangular? 3x5? Or only square?

A1: live answered


Q: a grid can be a rectangle right

A1: live answered


Q: square or rectangular?

A1: live answered


Q: is a grid analagous to a numpy array in python?

A1: Yes, similar abstraction, many languages have support for multi-dimension arrays


Q: is default grid value zero?

A1: For integers, yes, for other types it would be the default value for type, e.g. empty string


Q: so matrix[0][1] is like matrix[y][x]??

A1: Yes, row then column


Q: do you need to import something to use the grids?

A1: #include “grid.h”


Q: are grids used for image processing?

A1: yes!


Q: Can you ask a grid its size and it tells you how many rows/collums it has?

A1: Yes, g.numRows() is number of rows and g.numCols() is number of columns


Q: Can i switch value between grids? For example change matrix[1][0] to matrix [0][1]

A1: there is not a transpose function that does this for you, but you could write it yourself


Q: can you multiply two grids (similar to multiplying matrices)

A1: There is no mulitply operation built-in to Grid, but you could write your own


Q: why is r not size_t? can the number of rows be negative?

A1: It really ought to be, but in 106B we tend to try to avoid the notion of signed/unsigned and postpone that discussion until 107


Q: for the traversing grid example, why did we need to include the second cout endl; line after the inner for loop?

A1: It advances down a line in the output at end of each row


Q: When using resize(): Does “return to default values” mean that every [r][c] is empty in the resized grid? So is resize() basically the same thing as declaring a new empty vector altogether?

A1: there is an optional retain parameter that indicates that you want it to retain any values, otherwise it discards and reinitializes all elements to default


Q: Could you repeat why the type should be bool in Grid?</i>

A1: The element type is chosen based on what you are trying to store, if the information was just a yes/no for each location, bool would be appropriate


Q: In C++, is a color is a single number rather than a tuple of 3 numbers?

A1: Typically a color will be represented usingt hree 8-bit numbers, which can be mashed into one 32-bit int (including 8-bits of transparency/alpha)


Q: do you recommend we follow along coding?

A1: There is an archive of the lecture project on the website (see on lecture page for 4/15) you can dowloand that zip, and open project in QT and follow along


Q: for the for each loop will the complier throw an error and say that “color” variable hasnt been used yet?

A1: I am not sure what you are referring to, can you clarify?


Q: Since you probably won't modify the pallette, is it still better to use the direct reference (&)?

A1: live answered


Q: does it make a difference whether the & is next to the variable type or the variable name?

A1: The & can go in next to typename or next to variable name, means the exact same thing.


Q: how do we read getClosestColor(palette, pixels[r][c])?

A1: There is an archive of the lecture project on the website (see on lecture page for 4/15) you can dowloand that zip, and open project in QT and then study the code at your leisure


Q: what does #04182B represent?

A1: It is a hexadecimal color string (used in html/web), first two digits are amount of red, next two are green, last two are blue


Q: Do most filters use a color palette? How big do these lists of hexidecimals get?

A1: A transform style fitler might be palette-based, others do other kinds of manipulation (convert to grayscale, boost, sharpen, etc) that are not pallette-based


Q: what exactly does the & represent again?

A1: It indicates that the parameter is passed by reference


Q: How to run only a part of the main in Qt?

A1: A C++ program starts at main and executes the entire function.


Q: Im still a little confused why we passed the palette by reference

A1: Solely for reasons of efficiency. Otherwise we make a full copy of all the values, which can be slow for a large data type


Q: Friday is just the early due date right? And final due date is Sunday.

A1: There is one due dat and tha’s Friday. Beyond that is a grace period that extends to Sunday.


Q: Sorry I was not clear enough. How do I run just the tooShade function but not the rest?

A1: You cannot. A C++ program executes main function always.


Q: Do we need to add test cases to Soundex, even if we got it right the first tiem and did not need additional tests?

A1: Yes. Getting it correct without testing is a mixed achievement. Rather than count on such forunte next time, please work at establishing a development practice that will go the distance with you :-)


Q: are stacks faster than vectors?

A1: They can be, yes


Q: What is ADT?

A1: live answered


Q: what was ADT again?

A1: live answered


Q: Remind me what an ADT is?

A1: live answered


Q: Thanks! So Chris always runs the whole cpp file whenever he demos it? Why it always only shows the part he is showing us, but not the previous results?

A1: I believe today he had open two different projects and he was switching between them


Q: What does ADT stand for again?

A1: live answered


Q: ADT = Abstract data type?

A1: live answered


Q: Is this idea similar to scope?

A1: Yes, as you enter a new scope, it is akin to pushing a new scope on a stack and when you exit that scope, it is like popping


Q: Is what Chris is talking about right now reflective of the fact that we look at the call “stack” when we debug?

A1: live answered


Q: does stacks work well with recursion?

A1: You’re a bit of ahead of us there, give us a few lectures and we’ll get back to you on that one :-)


Q: If you pop a value off a stack, is there a way to push it back on again?

A1: Catch the result and put it back on: val = stack.pop() and then call stack.push(val)


Q: How would you build a stack from an arry or vector?

A1: Iterate over vector, call stack.push on each element


Q: mosty unrelated, but what do people mean when they say “stack overflow”

A1: it specifically refers to the function call stack, which has a hard upper limit on the number of function calls you can stack up, and if you exceed that -> stack overflow! (usually crashes program)

A2: we’ll cover this next week!


Q: in the above question would val then be stuck as that value? or would it be possible to continually reset the val variable to the value you pop?

A1: I am not sure of the context for this question, can you clarify?


Q: So in python when we did .extend() or .append() was that the same concept of adding to the end? Just trying to see how this relates to what we may have already seen.

A1: Yes, same concept, slightly different vocabulary


Q: this might be silly, but just to clarify: is a stack more just like a school of thought than a real thing like grid or vector?

A1: Stacks are just as real as grids and vectors! That’s why you can see chris using a Stack in the last code example.


Q: Why is main declared to return an int?

A1: main is a special kind of function in C++ that must return an int


Q: why is SPACE in single quotes while other strings are in double quotes?

A1: It is a character, not a string


Q: i guess I’m a bit lost as to why we define it by its “behavior” rather than the “under the hood” aspect

A1: This frees up from thinking about the internalls and nitty gritty — we interact with stack as a thing that has simple and well-defined operations, push and pop


Q: dont we need to add characters to make the word

A1: word += c is append each character to word


Q: if you type SPACE as a character does it output as a space, or did he define that somewhere?

A1: It was declared as a constant further up in the code


Q: Isn't the if statement outside the for loop so that it pushes the final word (since there may not be a space at the end of the string)?

A1: You’re right that we have a fencepost situation — will need to add the very last word outside the loop


Q: Will the code be updated after lecture?

A1: yes, chris will make the fixes to the code shortly after lecture ends


Q: What if you peek on an empty que or stack?

A1: live answered


Q: My point was that the code looks correct. The if statement needs to be outside just to push the last word to the stack, right?

A1: Yes you are correct (btw, it is hard to relate followup to earlier question when both coming in anonymous, please help me out by using your name if you don’t mind)