This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:fall2020:cprog:projects:cbf0 [2020/03/10 19:18] – external edit 127.0.0.1 | haas:fall2020:cprog:projects:cbf0 [2020/09/29 15:02] (current) – [Experiencing xxd] wedge | ||
---|---|---|---|
Line 183: | Line 183: | ||
<cli> | <cli> | ||
+ | system: | ||
00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f > | 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f > | ||
00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62 PQRSTUVWXYZ< | 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62 PQRSTUVWXYZ< | ||
Line 194: | Line 195: | ||
00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29 EF: | 00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29 EF: | ||
000000a0: 21 40 23 24 25 5e 26 2a 28 0a 2e 0a !@# | 000000a0: 21 40 23 24 25 5e 26 2a 28 0a 2e 0a !@# | ||
+ | </ | ||
+ | |||
+ | Or, if using a line throttle: | ||
+ | |||
+ | <cli> | ||
+ | system: | ||
+ | 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f > | ||
+ | 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62 PQRSTUVWXYZ< | ||
+ | 00000020: 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 cdefghijklmnopqr | ||
+ | 00000030: 73 74 75 76 77 78 79 7a 5d 0a 30 31 3a 20 20 20 stuvwxyz].01: | ||
</ | </ | ||
=====Detecting Terminal Size===== | =====Detecting Terminal Size===== | ||
Line 221: | Line 232: | ||
Compile and run the above code to see how it works. Try it in different size terminals. Then incorporate the logic into your hex viewer for this project. | Compile and run the above code to see how it works. Try it in different size terminals. Then incorporate the logic into your hex viewer for this project. | ||
+ | |||
+ | =====Command-Line Arguments===== | ||
+ | |||
+ | ====setting up main()==== | ||
+ | To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional **argc** and **argv** names, although it is also common to see them abbreviated as **ac** and **av**. | ||
+ | |||
+ | Please declare your main() function as follows: | ||
+ | |||
+ | <code c> | ||
+ | int main(int argc, char **argv) | ||
+ | </ | ||
+ | |||
+ | There are two very important variables involved here (the types are actually what are important, the names given to the variables are actually quite, variable; you may see other references refer to them as things like " | ||
+ | |||
+ | * int argc: the count (an integer) of tokens given on the command line (program name + arguments) | ||
+ | * < | ||
+ | |||
+ | The arguments are accessible via the argv array, in the order they were specified: | ||
+ | |||
+ | * argv[0]: program invocation (path + program name) | ||
+ | * argv[1]: our first argument | ||
+ | * argv[2]: second argument | ||
+ | * argv[3]: third argument | ||
+ | * ... | ||
+ | * argv[N]: Nth argument | ||
+ | |||
+ | Additionally, | ||
+ | |||
+ | ===example=== | ||
+ | For example, if we were to execute a program as follows: | ||
+ | |||
+ | <cli> | ||
+ | $ ./program word 73 another word | ||
+ | </ | ||
+ | |||
+ | We'd have: | ||
+ | |||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | and let's not forget: | ||
+ | |||
+ | * argc: 5 | ||
+ | |||
+ | =====Loops===== | ||
+ | A loop is basically instructing the computer to repeat a section, or block, or code a given amount of times (it can be based on a fixed value-- repeat this 4 times, or be based on a conditional value-- keep repeating as long as (or while) this value is not 4). | ||
+ | |||
+ | Loops enable us to simplify our code-- allowing us to write a one-size-fits all algorithm (provided the algorithm itself can appropriately scale!), where the computer merely repeats the instructions we gave. We only have to write them once, but the computer can do that task any number of times. | ||
+ | |||
+ | Loops can be initially difficult to comprehend because unlike other programmatic actions, they are not single-state in nature-- loops are multi-state. What this means is that in order to correctly " | ||
+ | |||
+ | With that said, it is important to be able to focus on the process of the individual steps being taken. What is involved in taking a step? What constitutes a basic unit of stairway traversal? If that unit can be easily repeated for the next and the next (and in fact, the rest of the) steps, we've described the core process of the loop, or what will be iterated a given number of times. | ||
+ | |||
+ | In C and C-syntax influenced languages (C++, Java, PHP, among others), we typically have 3 types of loops: | ||
+ | |||
+ | * **for** loop (automatic counter loop, stepping loop; top-driven) - when we know exactly how many times we wish something to run; we know where we want to start, where we want to end, and exactly how to progress from start to end (step value) | ||
+ | * **while** loop (top-driven conditional loop) - when we want to repeat a process, but the exact number of iterations is either not known, not important, not known, or variable in nature. While loops can run 0 or more times. | ||
+ | * **do-while** loop (bottom-driven conditional loop) - similar to the while loop, only we do the check for loop termination at the bottom of the loop, meaning it runs 1 or more times (a do-while loop is guaranteed to run at least once). | ||
+ | |||
+ | ====for() loops==== | ||
+ | A **for()** loop is the most syntactically unique of the loops, so care must be taken to use the proper syntax. | ||
+ | |||
+ | With any loop, we need (at least one) looping variable, which the loop will use to analyze whether or not we've met our looping destination, | ||
+ | |||
+ | A for loop typically also has a defined starting point, a " | ||
+ | |||
+ | Here's a sample for() loop, in C, which will display the squares of each number, starting at 0, and stepping one at a time, for 8 total iterations: | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | for (i = 0; i < 8; i++) | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The output of this code, with the help of our loop should be: | ||
+ | |||
+ | <cli> | ||
+ | loop #1 ... 0 | ||
+ | loop #2 ... 1 | ||
+ | loop #3 ... 4 | ||
+ | loop #4 ... 9 | ||
+ | loop #5 ... 16 | ||
+ | loop #6 ... 25 | ||
+ | loop #7 ... 36 | ||
+ | loop #8 ... 49 | ||
+ | </ | ||
+ | |||
+ | Note how we can use our looping variable (**i**) within mathematical expressions to drive a process along... loops can be of enormous help in this way. | ||
+ | |||
+ | And again, we shouldn' | ||
+ | |||
+ | The loop exits once **i** reaches a value of 8, because our loop determinant condition states as long as **i** is **less than** **8**, continue to loop. Once **i** becomes **8**, our looping condition has been satisfied, and the loop will no longer iterate. | ||
+ | |||
+ | The stepping (that third) field is a mathematical expression indicating how we wish for **i** to progress from its starting state (of being equal to 0) to satisfying the loop's iterating condition (no longer being less than 8). | ||
+ | |||
+ | **i++** is a shortcut we can use in C; the longhand (and likely more familiar) equivalent is: **i = i + 1** | ||
+ | |||
+ | ====while() loops==== | ||
+ | A **while()** loop isn't as specific about starting and stepping values, really only caring about what condition needs to be met in order to exit the loop (keep looping while this condition is true). | ||
+ | |||
+ | In actuality, anything we use a for loop for can be expressed as a while loop-- we merely have to ensure we provide the necessary loop variables and progressions within the loop. | ||
+ | |||
+ | That same loop above, expressed as a while loop, could look like: | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | while (i < 8) | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | i = i + 1; // I could have used " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The output of this code should be identical, even though we used a different loop to accomplish the task (try them both out and confirm!) | ||
+ | |||
+ | **while()** loops, like **for()** loops, will run 0 or more times; if the conditions enabling the loop to occur are not initially met, they will not run... if met, they will continue to iterate until their looping conditions are met. | ||
+ | |||
+ | It is possible to introduce a certain kind of **logical error** into your programs using loops-- what is known as an " | ||
+ | |||
+ | Another common **logical error** that loops will allow us to encounter will be the "off by one" error-- where the conditions we pose to the loop are incorrect, and the loop runs one magnitude more or less than we had intended. Again, proper debugging of our code will resolve this situation. | ||
+ | |||
+ | ====do-while loops==== | ||
+ | The third commonly recognized looping structure in C, the do-while loop is identical to the while() (and therefore also the for()) loop, only it differs in where it checks the looping condition: where **for()** and **while()** are " | ||
+ | |||
+ | The placement of this test determines the minimal number of times a loop can run. | ||
+ | |||
+ | In the case of the for()/ | ||
+ | |||
+ | For the do-while loop, because the test occurs at the bottom, the body of the loop (one full iteration) is run before the test is encountered. So even if the conditions for looping are not met, a do-while will run "1 or more times" | ||
+ | |||
+ | That may seem like a minor, and possibly annoying, difference, but in nuanced algorithm design, such distinctions can drastically change the layout of your code, potentially being the difference between beautifully elegant-looking solutions and those which appear slightly more hackish. They can BOTH be used to solve the same problems, it is merely the nature of how we choose express the solution that should make one more preferable over the other in any given moment. | ||
+ | |||
+ | I encourage you to intentionally try your hand at taking your completed programs and implementing other versions that utilize the other types of loops you haven' | ||
+ | |||
+ | So, expressing that same program in the form of a do-while loop (note the changes from the while): | ||
+ | |||
+ | <code c> | ||
+ | int i = 0; | ||
+ | |||
+ | do | ||
+ | { | ||
+ | fprintf(stdout, | ||
+ | i = i + 1; // again, we could just as easily use " | ||
+ | } while(i < 8); | ||
+ | </ | ||
+ | |||
+ | In this case, the 0 or more vs. 1 or more minimal iterations wasn't important; the difference is purely syntactical. | ||
+ | |||
+ | With the do-while loop, we start the loop with a **do** statement. | ||
+ | |||
+ | Also, the do-while is the only one of our loops which NEEDS a terminating semi-colon (**;**).. please take note of this. | ||
=====Submission===== | =====Submission===== | ||
Line 252: | Line 421: | ||
< | < | ||
- | 52:cbf0:final tally of results (52/52) | + | 78:cbf0:final tally of results (78/78) |
*: | *: | ||
- | *: | + | *: |
- | *: | + | *: |
- | *:cbf0:cbf0 runtime output conforms to specifications [13/13] | + | *:cbf0:cbf0 runtime output conforms to specifications [26/26] |
</ | </ | ||