This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
haas:spring2023:unix:projects:cnv0 [2023/02/28 14:37] – created wedge | haas:spring2023:unix:projects:cnv0 [2023/03/19 19:40] (current) – [Specifications] wedge | ||
---|---|---|---|
Line 1: | Line 1: | ||
<WRAP centeralign round box> | <WRAP centeralign round box> | ||
< | < | ||
- | < | + | < |
</ | </ | ||
Line 7: | Line 7: | ||
=====Objective===== | =====Objective===== | ||
- | To create a program | + | To create a script |
- | + | ||
- | =====Reading===== | + | |
- | In "The C Book", please read through Chapter 8. | + | |
- | + | ||
- | Review needed concepts in [[https:// | + | |
=====Background===== | =====Background===== | ||
Line 35: | Line 30: | ||
* factor pair of 2 and 3 | * factor pair of 2 and 3 | ||
- | Where 17 was a primary number, 6 is a secondary number. | + | Where 17 was a prime or "primary" |
====Determining factor pairs==== | ====Determining factor pairs==== | ||
Line 94: | Line 89: | ||
Run ' | Run ' | ||
- | |||
- | =====Compiling===== | ||
- | Since there is a provided Makefile in the project grabit, we can use that to compile, either regularly: | ||
- | |||
- | <cli> | ||
- | yourpi: | ||
- | </ | ||
- | |||
- | Or, with debugging support: | ||
- | |||
- | <cli> | ||
- | yourpi: | ||
- | </ | ||
=====Loops===== | =====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). | + | A loop is basically instructing the computer to repeat a section, or block, or logic 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 enable us to simplify our logic-- 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 " | 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 " | ||
Line 117: | Line 99: | ||
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. | 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: | + | In BASH, we will generally focus on the following three 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) | + | * numeric |
* **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. | * **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 | + | * list-based |
- | ====for() loops==== | + | ====numeric |
- | A **for()** loop is the most syntactically unique of the loops, so care must be taken to use the proper syntax. | + | A numeric |
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, | 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 " | + | A numeric |
- | 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: | + | Here's a sample |
- | < | + | < |
- | int i = 0; | + | for ((index=0; index< |
- | + | | |
- | for (i = 0; i < 8; i++) | + | done |
- | { | + | |
- | | + | |
- | } | + | |
</ | </ | ||
Line 154: | Line 133: | ||
</ | </ | ||
- | 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. | + | Note how we can use our looping variable (**index**) within mathematical expressions to drive a process along... loops can be of enormous help in this way. |
- | And again, we shouldn' | + | 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 loop exits once **index** reaches a value of 8, because our loop determinant condition states as long as **index** is **less than** **8**, continue to loop. Once **index** 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). | + | The stepping (that third) field is a mathematical expression indicating how we wish for **index** 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** | + | **index++** is a shortcut we can use in C; the longhand (and likely more familiar) equivalent is: **index=index+1** |
- | ====while() loops==== | + | ====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). | + | 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. | 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. | ||
Line 171: | Line 150: | ||
That same loop above, expressed as a while loop, could look like: | That same loop above, expressed as a while loop, could look like: | ||
- | < | + | < |
- | int i = 0; | + | index=0 |
- | + | while [ " | |
- | while (i < 8) | + | |
- | { | + | |
- | | + | done |
- | | + | |
- | } | + | |
</ | </ | ||
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!) | 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. | + | **while()** loops, like numeric |
It is possible to introduce a certain kind of **logical error** into your programs using loops-- what is known as an " | It is possible to introduce a certain kind of **logical error** into your programs using loops-- what is known as an " | ||
Line 189: | Line 166: | ||
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. | 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==== | + | ====list-based for loops==== |
- | The third commonly | + | The third commonly |
- | The placement of this test determines | + | I encourage you to intentionally try your hand at taking your completed programs and implementing other versions that utilize |
- | 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" | + | So, expressing that same logic in the form of a list-based |
- | 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" | + | <code bash> |
- | + | for index in $(seq 0 7); do | |
- | 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. | + | |
- | + | done | |
- | 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 | + | |
- | + | ||
- | <code c> | + | |
- | int i = 0; | + | |
- | + | ||
- | do | + | |
- | { | + | |
- | | + | |
- | i = i + 1; // again, we could just as easily use "i++;" here | + | |
- | } while(i < 8); | + | |
</ | </ | ||
- | In this case, the 0 or more vs. 1 or more minimal iterations wasn't important; | + | In this case, the command-expanded output of the call to the **seq**(**1**) command |
- | With the do-while loop, we start the loop with a **do** statement. | + | =====Script===== |
- | + | It is your task to write a script | |
- | Also, the do-while is the only one of our loops which NEEDS a terminating semi-colon (**;**).. please take note of this. | + | |
- | + | ||
- | =====Program===== | + | |
- | It is your task to write a program | + | |
=====Specifications===== | =====Specifications===== | ||
Line 230: | Line 191: | ||
* all code within the same scope aligned to its indentation level | * all code within the same scope aligned to its indentation level | ||
* have proximal comments explaining your rationale (the why and how), throughout your code | * have proximal comments explaining your rationale (the why and how), throughout your code | ||
- | * to STDERR, prompt for the number | + | * to STDERR, prompt for the number |
- | * properly store this in a variable of type **unsigned long int** | + | |
* immediately after the input, check to make sure the input number is a positive number greater than or equal to 2; if in violation, display an error (to STDERR) and exit with a non-zero value. | * immediately after the input, check to make sure the input number is a positive number greater than or equal to 2; if in violation, display an error (to STDERR) and exit with a non-zero value. | ||
* proceed to evaluate the input number, determining whether or not it is a secondary (nary(2)) number. | * proceed to evaluate the input number, determining whether or not it is a secondary (nary(2)) number. | ||
Line 237: | Line 197: | ||
* if it is not, display to STDOUT that it is not a secondary number (again, see execution section below) | * if it is not, display to STDOUT that it is not a secondary number (again, see execution section below) | ||
* using a single return statement at the conclusion of the code, return a 0 indicating successful operation | * using a single return statement at the conclusion of the code, return a 0 indicating successful operation | ||
- | |||
- | Some additional points of consideration: | ||
- | * Note that the driving variables in your loops need to be at least of type **short int**, otherwise you may get a warning when you compile it. | ||
=====Execution===== | =====Execution===== | ||
Line 245: | Line 202: | ||
====Secondary number output==== | ====Secondary number output==== | ||
<cli> | <cli> | ||
- | yourpi: | + | yourpi: |
Enter a number: 6 | Enter a number: 6 | ||
6 is a secondary number | 6 is a secondary number | ||
Line 253: | Line 210: | ||
====Non-secondary number output==== | ====Non-secondary number output==== | ||
<cli> | <cli> | ||
- | yourpi: | + | yourpi: |
Enter a number: 7 | Enter a number: 7 | ||
7 is NOT a secondary number | 7 is NOT a secondary number | ||
Line 260: | Line 217: | ||
====Additional outputs==== | ====Additional outputs==== | ||
<cli> | <cli> | ||
- | yourpi: | + | yourpi: |
Enter a number: 8 | Enter a number: 8 | ||
8 is a secondary number | 8 is a secondary number | ||
- | yourpi: | + | yourpi: |
Enter a number: 16 | Enter a number: 16 | ||
16 is NOT a secondary number | 16 is NOT a secondary number | ||
- | yourpi: | + | yourpi: |
Enter a number: 21 | Enter a number: 21 | ||
21 is a secondary number | 21 is a secondary number | ||
Line 336: | Line 293: | ||
To successfully complete this project, the following criteria must be met: | To successfully complete this project, the following criteria must be met: | ||
- | * Code must compile | + | * Script |
* Output must be correct, and match the form given in the sample output above. | * Output must be correct, and match the form given in the sample output above. | ||
- | * Code must be nicely and consistently indented | + | * Script |
- | * Code must be well commented | + | * Script |
- | * Do NOT double space your code. Group like statements together. | + | * Do NOT double space your script. Group like statements together. |
- | * Output Formatting (including spacing) of program | + | * Output Formatting (including spacing) of script |
- | * Track/ | + | * Track/ |
- | * Submit a copy of your source code to me using the **submit** tool. | + | * Submit a copy of your script |
- | To submit this program | + | To submit this project |
<cli> | <cli> | ||
Line 361: | Line 318: | ||
*: | *: | ||
*: | *: | ||
- | *: | + | *: |
*: | *: | ||
*: | *: | ||
*: | *: | ||
*: | *: | ||
- | *: | + | *: |
- | *:cnv0:code is pushed to lab46 repository [6/6] | + | *:cnv0:code is pushed to lab46 SEMESTER |
</ | </ | ||