User Tools

Site Tools


haas:fall2020:cprog:projects:cbf0

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
haas:fall2020:cprog:projects:cbf0 [2020/03/10 19:18] – external edit 127.0.0.1haas:fall2020:cprog:projects:cbf0 [2020/09/29 15:02] (current) – [Experiencing xxd] wedge
Line 183: Line 183:
  
 <cli> <cli>
 +system:~/src/desig/cbf0$ ./cbf0 in/sample0.txt
 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO 00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO
 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab 00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab
Line 194: Line 195:
 00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29  EF:HEXADECIMAL.) 00000090: 45 46 3a 48 45 58 41 44 45 43 49 4d 41 4c 0a 29  EF:HEXADECIMAL.)
 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              !@#$%^&*(...
 +</cli>
 +
 +Or, if using a line throttle:
 +
 +<cli>
 +system:~/src/desig/cbf0$ ./cbf0 in/sample0.txt 4
 +00000000: 3e 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  >ABCDEFGHIJKLMNO
 +00000010: 50 51 52 53 54 55 56 57 58 59 5a 3c 0a 5b 61 62  PQRSTUVWXYZ<.[ab
 +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:   
 </cli> </cli>
 =====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)
 +</code>
 +
 +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 "ac" and "av"):
 +
 +  * int argc: the count (an integer) of tokens given on the command line (program name + arguments)
 +  * <nowiki>char **argv</nowiki>: an array of strings (technically an array of an array of char) that contains "strings" of the various tokens provided on the command-line.
 +
 +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, let's not forget the **argc** variable, an integer, which contains a count of arguments (argc == argument count). If we provided argv[0] through argv[4], argc would contain a 5.
 +
 +===example===
 +For example, if we were to execute a program as follows:
 +
 +<cli>
 +$ ./program word 73 another word
 +</cli>
 +
 +We'd have:
 +
 +  * <nowiki>argv[0]</nowiki>: "./program" 
 +  * <nowiki>argv[1]</nowiki>: "word"
 +  * <nowiki>argv[2]</nowiki>: "73" (note not the integer number 73, but the string "73")
 +  * <nowiki>argv[3]</nowiki>: "another" 
 +  * <nowiki>argv[4]</nowiki>: "word" 
 +
 +and let's not forget:
 +
 +  * argc: 5   (there are 5 things, argv indexes 0, 1, 2, 3, and 4)
 +
 +=====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 "see" or visualize a loop, you must analyze what is going on with EACH iteration or cycle, watching the values/algorithm/process slowly march from its initial state to its resultant state. Think of it as climbing a set of stairs... yes, we can describe that action succinctly as "climbing a set of stairs", but there are multiple "steps" (heh, heh) involved: we place our foot, adjust our balance-- left foot, right foot, from one step, to the next, to the next, allowing us to progress from the bottom step to the top step... that process of scaling a stairway is the same as iterating through a loop-- but what is important as we implement is what needs to happen each step along the way.
 +
 +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, or to perform another iteration.
 +
 +A for loop typically also has a defined starting point, a "keep-looping-while" condition, and a stepping equation.
 +
 +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, "loop #%d ... %d\n", (i+1), (i*i));
 +}
 +</code>
 +
 +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
 +</cli>
 +
 +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't look at this as one step-- we need to see there are 8 discrete, distinct steps happening here (when i is 0, when i is 1, when i is 2, ... up until (and including) when i is 7).
 +
 +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, "loop #%d ... %d\n", (i+1), (i*i));
 +    i = i + 1;   // I could have used "i++;" here
 +}
 +</code>
 +
 +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 "infinite loop"; this is basically where you erroneously provide incorrect conditions to the particular loop used, allowing it to start running, but never arriving at its conclusion, thereby iterating forever.
 +
 +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 "top-driven" loops (ie the test for loop continuance occurs at the top of the loop, **before** running the code in the loop body), the **do-while** is a "bottom-driven" loop (ie the test for loop continuance occurs at the bottom of the loop).
 +
 +The placement of this test determines the minimal number of times a loop can run.
 +
 +In the case of the for()/while() loops, because the test is at the top- if the looping conditions are not met, the loop may not run at all. It is for this reason why these loops can run "0 or more times"
 +
 +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't utilized. This way, you can get more familiar with how to structure your solutions and express them. You will find you tend to think in a certain way (from experience, we seem to get in the habit of thinking "top-driven", and as we're unsure, we tend to exert far more of a need to control the situation, so we tend to want to use **for** loops for everything-- but practicing the others will free your mind to craft more elegant and efficient solutions; but only if you take the time to play and explore these possibilities).
 +
 +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, "loop #%d ... %d\n", (i+1), (i*i));
 +    i = i + 1;  // again, we could just as easily use "i++;" here
 +} while(i < 8);
 +</code>
 +
 +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:
  
 <code> <code>
-52:cbf0:final tally of results (52/52)+78:cbf0:final tally of results (78/78)
 *:cbf0:cbf0.c compiles cleanly, no compiler messages [13/13] *:cbf0:cbf0.c compiles cleanly, no compiler messages [13/13]
-*:cbf0:cbf0.c implements only specified algorithm [13/13] +*:cbf0:cbf0.c pushed to repository [13/13] 
-*:cbf0:cbf0.c code conforms to project specifications [13/13+*:cbf0:cbf0.c code conforms to project specifications [26/26
-*:cbf0:cbf0 runtime output conforms to specifications [13/13]+*:cbf0:cbf0 runtime output conforms to specifications [26/26]
 </code> </code>
  
haas/fall2020/cprog/projects/cbf0.1583867902.txt.gz · Last modified: 2020/03/10 19:18 by 127.0.0.1