User Tools

Site Tools


haas:spring2023:unix:projects:cnv0

Differences

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

Link to this comparison view

Next revision
Previous revision
haas:spring2023:unix:projects:cnv0 [2023/02/28 14:37] – created wedgehaas:spring2023:unix:projects:cnv0 [2023/03/19 19:40] (current) – [Specifications] wedge
Line 1: Line 1:
 <WRAP centeralign round box> <WRAP centeralign round box>
 <WRAP><color red><fs 200%>Corning Community College</fs></color></WRAP> <WRAP><color red><fs 200%>Corning Community College</fs></color></WRAP>
-<WRAP><fs 150%>CSCS1320 C/C++ Programming</fs></WRAP>+<WRAP><fs 150%>CSCS1730 UNIX/Linux Fundamentals</fs></WRAP>
 </WRAP> </WRAP>
  
Line 7: Line 7:
  
 =====Objective===== =====Objective=====
-To create a program that can calculate and determine the number of factor pairs of a given number, starting with values composed of exactly 2 sets of factor pairs. +To create a script that can calculate and determine the number of factor pairs of a given number, starting with values composed of exactly 2 sets of factor pairs.
- +
-=====Reading===== +
-In "The C Book", please read through Chapter 8. +
- +
-Review needed concepts in [[https://www.tutorialspoint.com/cprogramming/|this tutorial]] and also [[https://www.cprogramming.com/tutorial/c-tutorial.html?inl=hp|this one]]+
  
 =====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 "primarynumber, 6 is a "secondarynumber.
  
 ====Determining factor pairs==== ====Determining factor pairs====
Line 94: Line 89:
  
 Run '**grabit**' on lab46 in the appropriate manner to obtain the files. Run '**grabit**' on lab46 in the appropriate manner to obtain the files.
- 
-=====Compiling===== 
-Since there is a provided Makefile in the project grabit, we can use that to compile, either regularly: 
- 
-<cli> 
-yourpi:~/src/SEMESTER/DESIG/cnv0$ make 
-</cli> 
- 
-Or, with debugging support: 
- 
-<cli> 
-yourpi:~/src/SEMESTER/DESIG/cnv0$ make debug 
-</cli> 
  
 =====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 "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. 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.
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 **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.   * **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 loopmeaning it runs 1 or more times (do-while loop is guaranteed to run at least once).+  * list-based **for** loop (top-driven conditional loop) - sometimes this is referred to as a "for each" loop: it is provided a set of values and it will iterate through each one, one at time.
  
-====for() loops==== +====numeric for() loops==== 
-A **for()** loop is the most syntactically unique of the loops, so care must be taken to use the proper syntax.+numeric **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. 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.+numeric 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:+Here's a sample numeric for loop, in BASH, which will display the squares of each number, starting at 0, and stepping one at a time, for 8 total iterations:
  
-<code c> +<code bash
-int i = 0; +for ((index=0; index<8; index++)); do 
- +    printf "loop #%d ... %d\n" "$((${index}+1))" "$((${index}*${index}))" 
-for (= 0; < 8; i++) +done
-{ +
-    fprintf(stdout, "loop #%d ... %d\n"(i+1)(i*i)); +
-}+
 </code> </code>
  
Line 154: Line 133:
 </cli> </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.+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't look at this as one step-- we need to see there are 8 discrete, distinct steps happening here (when is 0, when is 1, when is 2, ... up until (and including) when is 7).+And again, we shouldn't look at this as one step-- we need to see there are 8 discrete, distinct steps happening here (when index is 0, when index is 1, when index is 2, ... up until (and including) when index 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 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: **+ 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:
  
-<code c+<code bash
-int i = 0+index=0 
- +while [ "${index}" -lt ]; do 
-while (i < 8+    printf "loop #%d ... %d\n" "$((${index}+1))" "$((${index}*${index}))" 
-{ +    let index=index+1 
-    fprintf(stdout, "loop #%d ... %d\n"(i+1)(i*i)); +done
-    + 1;   // I could have used "i++;" here +
-}+
 </code> </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!) 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 **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. 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.
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 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-drivenloops (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 third commonly utilized looping structure in BASH, the list-based for loop (aka "foreach" loop) is based more on providing it list of tokens (commonly a string, or the expanded output of some command containing interpretable tokens), which get iterated through one at a time.
  
-The placement of this test determines the minimal number of times a loop can run.+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.
  
-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 loop:
  
-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. +    printf "loop #%d ... %d\n" "$((${index}+1))" "$((${index}*${index}))" 
- +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'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> </code>
  
-In this case, the 0 or more vs. 1 or more minimal iterations wasn't important; the difference is purely syntactical.+In this case, the command-expanded output of the call to the **seq**(**1**) command is what the loop iterates upon.
  
-With the do-while loop, we start the loop with a **do** statement. +=====Script===== 
- +It is your task to write a script that, upon accepting various pieces of input from the user, computes the number of factor pairs of a given number, displaying its eligibility as a secondary number.
-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 that, upon accepting various pieces of input from the user, computes the number of factor pairs of a given number, displaying its eligibility as a secondary number.+
  
 =====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 (range appropriate of an unsigned long int) +  * 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:~/src/SEMESTER/DESIG/cnv0$ ./cnv0+yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0.sh
 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:~/src/SEMESTER/DESIG/cnv0$ ./cnv0+yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0.sh
 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:~/src/SEMESTER/DESIG/cnv0$ ./cnv0+yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0.sh
 Enter a number: 8 Enter a number: 8
 8 is a secondary number 8 is a secondary number
-yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0+yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0.sh
 Enter a number: 16 Enter a number: 16
 16 is NOT a secondary number 16 is NOT a secondary number
-yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0+yourpi:~/src/SEMESTER/DESIG/cnv0$ ./cnv0.sh
 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 cleanly (no notes, warnings, nor errors)+  * Script must execute cleanly (no errors or other noise outside of needed output)
   * 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 must be nicely and consistently indented 
-  * Code must be well commented +  * Script must be well commented 
-  * 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 must conform to the provided output (see above). +  * Output Formatting (including spacing) of script must conform to the provided output (see above). 
-  * Track/version the source code in repository +  * Track/version the script in your lab46 SEMESTER repository 
-  * Submit a copy of your source code to me using the **submit** tool.+  * Submit a copy of your script to me using the **submit** tool.
  
-To submit this program to me using the **submit** tool, run the following command at your lab46 prompt:+To submit this project to me using the **submit** tool, run the following command at your lab46 prompt:
  
 <cli> <cli>
Line 361: Line 318:
 *:cnv0:resources obtained via grabit by Sunday before deadline [4/4] *:cnv0:resources obtained via grabit by Sunday before deadline [4/4]
 *:cnv0:proper error checking and status reporting performed [6/6] *:cnv0:proper error checking and status reporting performed [6/6]
-*:cnv0:correct variable types and name lengths used [6/6]+*:cnv0:correct variable name lengths used [6/6]
 *:cnv0:proper output formatting per specifications [6/6] *:cnv0:proper output formatting per specifications [6/6]
 *:cnv0:proper selection logic applied to perform comparisions [6/6] *:cnv0:proper selection logic applied to perform comparisions [6/6]
 *:cnv0:proper iteration logic applied to carry out process [6/6] *:cnv0:proper iteration logic applied to carry out process [6/6]
 *:cnv0:runtime verify tests of submission succeed [6/6] *:cnv0:runtime verify tests of submission succeed [6/6]
-*:cnv0:no negative compiler messages for program [6/6] +*:cnv0:no error messages while running script [6/6] 
-*:cnv0:code is pushed to lab46 repository [6/6]+*:cnv0:code is pushed to lab46 SEMESTER repository [6/6]
 </code> </code>
  
haas/spring2023/unix/projects/cnv0.1677595062.txt.gz · Last modified: 2023/02/28 14:37 by wedge