e49cc99a9cb4135e4753c21754b7967cc0ff3473
haas/spring2026/cprog/projects/dow0.md
| ... | ... | @@ -0,0 +1,365 @@ |
| 1 | +# CSCS1320 C/C++ Programming |
|
| 2 | + |
|
| 3 | +# PROJECT: DAY OF WEEK (dow0) |
|
| 4 | + |
|
| 5 | +## OBJECTIVE |
|
| 6 | + |
|
| 7 | +To begin our exploration of decision-making and more detailed |
|
| 8 | +process-following in C, implementing a program that uses a mental math |
|
| 9 | +technique to determine the day of the week that January 1st falls on for |
|
| 10 | +any year in the 21st century. |
|
| 11 | + |
|
| 12 | +## BACKGROUND |
|
| 13 | + |
|
| 14 | +Mental Math constitutes an intersection of mental tricks and math- |
|
| 15 | +instead of utilizing a purely math-only solution, textual manipulations |
|
| 16 | +or simplifications in the computational process may take place enabling |
|
| 17 | +an individual to, once having learned the process, solve such problems in |
|
| 18 | +their head, and typically without the use of a calculating device. |
|
| 19 | + |
|
| 20 | +The process in this case is one of simple (reduced) multiplication and |
|
| 21 | +mapping against a table. To wit: |
|
| 22 | + |
|
| 23 | +### DAY VALUES |
|
| 24 | + |
|
| 25 | +For this trick to work, we need to be familiar with the following table |
|
| 26 | +(a map of days to numeric values): |
|
| 27 | + |
|
| 28 | +| Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | |
|
| 29 | +| ------ | ------- | --------- | -------- | ------ | -------- | ------ | |
|
| 30 | +| 1 | 2 | 3 | 4 | 5 | 6 | 7 or 0 | |
|
| 31 | + |
|
| 32 | +**NOTE:** Depending on how you implement your algorithm, you may find |
|
| 33 | +your approach prefers 7s OR 0s... you aren't necessarily going to be |
|
| 34 | +encountering them at random. Once you work through your approach, you |
|
| 35 | +will discover which one you end up with. |
|
| 36 | + |
|
| 37 | +### CALCULATING DAY OF THE WEEK BASED ON YEAR |
|
| 38 | + |
|
| 39 | +Okay, time for the magic. |
|
| 40 | + |
|
| 41 | +Let us try it on January 1st, 2014. |
|
| 42 | + |
|
| 43 | +### STEP 1: OBTAIN LAST TWO DIGITS OF THE YEAR |
|
| 44 | + |
|
| 45 | +In our example, we're working with `2014`, the last two digits are |
|
| 46 | +therefore: `14` |
|
| 47 | + |
|
| 48 | +You should be able to come up with a means of extracting this information |
|
| 49 | +in your program. |
|
| 50 | + |
|
| 51 | +### STEP 2: COMPUTE 25% AND DROP THE DECIMAL |
|
| 52 | + |
|
| 53 | +Even this is something we can do in our heads. I can think of two |
|
| 54 | +approaches right off the bat: |
|
| 55 | + |
|
| 56 | +Approach 1: 10 + 10 + 5 |
|
| 57 | + |
|
| 58 | +10% percent of anything is merely moving the decimal over one place to |
|
| 59 | +the left. 10% of 54 is 5.4 |
|
| 60 | + |
|
| 61 | +For our 2014 example, 10% of 14 is therefore 1.4 |
|
| 62 | + |
|
| 63 | +So we need two 10 percents... 1.4 + 1.4 = 2.8 |
|
| 64 | + |
|
| 65 | +Finally, 5% is half of 10% (half of 1.4 is 0.7), so 1.4 + 1.4 + 0.7 = 3.5 |
|
| 66 | + |
|
| 67 | +But, since we do not care about the decimal, we drop it and are left with |
|
| 68 | +just 3. |
|
| 69 | + |
|
| 70 | +Approach 2: half of half |
|
| 71 | + |
|
| 72 | +25% is a convenient value for us with respect to 100, allowing this |
|
| 73 | +optimized approach to work. |
|
| 74 | + |
|
| 75 | + * Half of 100 is 50 (50%) |
|
| 76 | + * Half of 50 is 25 (25%) -- hence the "half of half" |
|
| 77 | + |
|
| 78 | +So, `14` cut in half is `7`. |
|
| 79 | + |
|
| 80 | +`7` cut in half is `3.5`. |
|
| 81 | + |
|
| 82 | +Once again, dropping the decimal yields `3`. |
|
| 83 | + |
|
| 84 | +### STEP 3: ADD 25% TO YEAR VALUE |
|
| 85 | + |
|
| 86 | +Once we have our 25% value, go and add it back to our two-digit year |
|
| 87 | +value: |
|
| 88 | + |
|
| 89 | +`14 + 3 = 17` |
|
| 90 | + |
|
| 91 | +### STEP 4: SUBTRACT THE LARGEST FITTING MULTIPLE OF 7 |
|
| 92 | + |
|
| 93 | +Some multiples of 7: |
|
| 94 | + |
|
| 95 | +| 0 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | |
|
| 96 | + |
|
| 97 | +So, with a value of 17, what is the largest multiple of 7 that is still |
|
| 98 | +less than (or equal to) 17? |
|
| 99 | + |
|
| 100 | +Hopefully you identified the 14 as the likely candidate. |
|
| 101 | + |
|
| 102 | +`17 - 14 = 3` |
|
| 103 | + |
|
| 104 | +### STEP 5: LOOK UP DAY IN TABLE |
|
| 105 | + |
|
| 106 | +We ended up with a 3 as the result for January 1st, 2014. |
|
| 107 | + |
|
| 108 | +Go and reference the `3` from that table... what day do we get? Does it |
|
| 109 | +match the actual day of the week for January 1st, 2014? |
|
| 110 | + |
|
| 111 | +``` |
|
| 112 | +lab46:~$ cal 01 2014 |
|
| 113 | + January 2014 |
|
| 114 | +Su Mo Tu We Th Fr Sa |
|
| 115 | + 1 2 3 4 |
|
| 116 | + 5 6 7 8 9 10 11 |
|
| 117 | +12 13 14 15 16 17 18 |
|
| 118 | +19 20 21 22 23 24 25 |
|
| 119 | +26 27 28 29 30 31 |
|
| 120 | + |
|
| 121 | +lab46:~$ |
|
| 122 | +``` |
|
| 123 | + |
|
| 124 | +Pretty neat, eh? |
|
| 125 | + |
|
| 126 | +### EXCEPTION: LEAP YEARS |
|
| 127 | + |
|
| 128 | +In the event of a leap year, we simply subtract `1` from the 25% value, |
|
| 129 | +and continue on as usual. |
|
| 130 | + |
|
| 131 | +Makes sense, right? Leap years add a day, so something ends up being "off |
|
| 132 | +by one". |
|
| 133 | + |
|
| 134 | +## SELECTION |
|
| 135 | + |
|
| 136 | +The computer follows instructions, one step at a time, in a top-down |
|
| 137 | +manner (starting at the first instruction, proceeding to the next, then |
|
| 138 | +the next, then the next, etc.) until it arrives at the end of the |
|
| 139 | +provided instructions. |
|
| 140 | + |
|
| 141 | +One such instruction available to us is that of the selection statement, |
|
| 142 | +of which we will focus specifically on the general-purpose and broadly |
|
| 143 | +used **if()**. |
|
| 144 | + |
|
| 145 | +An **if()** statement can make a decision, assuming that either the |
|
| 146 | +result of a comparison of two values, or the provided value, results in a |
|
| 147 | +true or false result |
|
| 148 | + |
|
| 149 | +Should the condition presented be true, perform a specific subset of |
|
| 150 | +instructions. If false, skip the processing of those instructions. |
|
| 151 | + |
|
| 152 | +The general presentation of an **if()** statement in your code will look |
|
| 153 | +like: |
|
| 154 | + |
|
| 155 | +``` |
|
| 156 | + int x = 7; |
|
| 157 | + int y = 2; |
|
| 158 | + int z = 0; |
|
| 159 | + |
|
| 160 | + ... |
|
| 161 | + |
|
| 162 | + z = x % y; // take the remainder of x divided by y |
|
| 163 | + if (z == 0) |
|
| 164 | + { |
|
| 165 | + first thing to do; |
|
| 166 | + next thing to do; |
|
| 167 | + ... |
|
| 168 | + nth thing to do; |
|
| 169 | + } |
|
| 170 | +``` |
|
| 171 | + |
|
| 172 | +The **condition** present will typically be in terms of two values being |
|
| 173 | +compared in some way, using one of the following relational operators |
|
| 174 | +available to us in C: |
|
| 175 | + |
|
| 176 | + * `==` is equal to |
|
| 177 | + * `!=` is not equal to |
|
| 178 | + * `<` is less than |
|
| 179 | + * `>` is greater than |
|
| 180 | + * `<=` is less than or equal to |
|
| 181 | + * `>=` is greater than or equal to |
|
| 182 | + |
|
| 183 | +In the example above, we were comparing the relationship of the value |
|
| 184 | +currently contained within the **z** variable, and the constant **0**, |
|
| 185 | +with respect to being equal. If the two match, the result is true (and we |
|
| 186 | +run what is inside the if() block). If false, we skip the if() block. |
|
| 187 | + |
|
| 188 | +There are further variations of the **if()**, which have to do with |
|
| 189 | +capturing additional scenarios. |
|
| 190 | + |
|
| 191 | +The next one is the **else**, a companion to the **if()**. Where the |
|
| 192 | +**if()** processes a set of included instructions in the event some |
|
| 193 | +provided condition is true, the **else** contains a set of instructions |
|
| 194 | +to process in the event that condition is false. |
|
| 195 | + |
|
| 196 | +``` |
|
| 197 | + int x = 7; |
|
| 198 | + int y = 2; |
|
| 199 | + int z = 0; |
|
| 200 | + |
|
| 201 | + ... |
|
| 202 | + |
|
| 203 | + z = x % y; // take the remainder of x divided by y |
|
| 204 | + if (z == 0) |
|
| 205 | + { |
|
| 206 | + first thing to do; |
|
| 207 | + next thing to do; |
|
| 208 | + ... |
|
| 209 | + nth thing to do; |
|
| 210 | + } |
|
| 211 | + else |
|
| 212 | + { |
|
| 213 | + first thing when false; |
|
| 214 | + next thing; |
|
| 215 | + ... |
|
| 216 | + nth thing; |
|
| 217 | + } |
|
| 218 | +``` |
|
| 219 | + |
|
| 220 | +When doing selection, the minimum needed is the **if()** statement. Once |
|
| 221 | +you have an **if()** statement, you can optionally append up to ONE |
|
| 222 | +**else** clause (or you can have none: it all depends on the nature of |
|
| 223 | +the condition you are testing, and what you'd like to do as a result). |
|
| 224 | + |
|
| 225 | +You can have multiple **if()** statements, each checking their unique |
|
| 226 | +condition, and each of those **if()** statements potentially having an |
|
| 227 | +associated **else** clause. |
|
| 228 | + |
|
| 229 | +Should the process call for a finer level of evaluation (say, you want to |
|
| 230 | +do something unique when a given variable is 0, and something different |
|
| 231 | +if 1, yet different if 2, etc.), we also have the ability to encapsulate |
|
| 232 | +that into our **if()** block (assuming we wouldn't be better served just |
|
| 233 | +having individual **if()** statements), by the use of the **else if()**: |
|
| 234 | + |
|
| 235 | +``` |
|
| 236 | + int x = 17; |
|
| 237 | + int y = 5; |
|
| 238 | + int z = 0; |
|
| 239 | + |
|
| 240 | + ... |
|
| 241 | + |
|
| 242 | + z = x % y; // take the remainder of x divided by y |
|
| 243 | + if (z == 0) |
|
| 244 | + { |
|
| 245 | + first thing to do; |
|
| 246 | + next thing to do; |
|
| 247 | + ... |
|
| 248 | + nth thing to do; |
|
| 249 | + } |
|
| 250 | + else if (z <= 3) |
|
| 251 | + { |
|
| 252 | + first thing to do; |
|
| 253 | + next thing to do; |
|
| 254 | + ... |
|
| 255 | + nth thing to do; |
|
| 256 | + } |
|
| 257 | + else if (z > 3) |
|
| 258 | + { |
|
| 259 | + first thing to do; |
|
| 260 | + next thing to do; |
|
| 261 | + ... |
|
| 262 | + nth thing to do; |
|
| 263 | + } |
|
| 264 | + else |
|
| 265 | + { |
|
| 266 | + first thing when false; |
|
| 267 | + next thing; |
|
| 268 | + ... |
|
| 269 | + nth thing; |
|
| 270 | + } |
|
| 271 | +``` |
|
| 272 | + |
|
| 273 | +Note that whether or not we have an **else if()**, and whether or not we |
|
| 274 | +have an **else**, we MUST (and first) have an **if()**. |
|
| 275 | + |
|
| 276 | +If we have an **else**, it necessarily comes last (it has no |
|
| 277 | +**condition**, it is the catch-all, the clause to handle things in the |
|
| 278 | +event none of the previous conditioned clauses matched). |
|
| 279 | + |
|
| 280 | +## PROGRAM |
|
| 281 | + |
|
| 282 | +It is your task to write the program that will use the above method to |
|
| 283 | +determine the day of the week any given January 1st in the 21st century |
|
| 284 | +falls on. |
|
| 285 | + |
|
| 286 | +Your program should: |
|
| 287 | + * prompt the user for the four digit year (input) |
|
| 288 | + * perform the task (process) |
|
| 289 | + * display the final value (output) |
|
| 290 | + |
|
| 291 | +## VERIFICATION |
|
| 292 | + |
|
| 293 | +Included in the grabit is a **dow0verify** script that can run your |
|
| 294 | +program through the entire gamut of years in this century, displaying the |
|
| 295 | +accuracy of results. |
|
| 296 | + |
|
| 297 | +It would be worthwhile to use it to ensure your final result is |
|
| 298 | +functional. |
|
| 299 | + |
|
| 300 | +## GRABIT |
|
| 301 | + |
|
| 302 | +I have prepared a `grabit` for resources related to this project. To |
|
| 303 | +obtain it: |
|
| 304 | + |
|
| 305 | +``` |
|
| 306 | +lab46:~/src/SEMESTER/DESIG$ grabit DESIG dow0 |
|
| 307 | +``` |
|
| 308 | + |
|
| 309 | +At which point you can change into the newly created and populated |
|
| 310 | +`dow0` directory. |
|
| 311 | + |
|
| 312 | +Be sure, using **git**, to **add**, **commit**, and **push** this to your |
|
| 313 | +lab46 clone of your repository. |
|
| 314 | + |
|
| 315 | +Then, if working on your pi/system, **git pull** to get the files |
|
| 316 | +available there, or continue working on it on lab46. |
|
| 317 | + |
|
| 318 | +## SUBMISSION |
|
| 319 | + |
|
| 320 | +To successfully complete this project, the following criteria must be |
|
| 321 | +met: |
|
| 322 | + |
|
| 323 | + * Code must compile/execute cleanly (no notes, warnings, nor errors) |
|
| 324 | + * Code must be nicely and consistently indented |
|
| 325 | + * Code must be well commented |
|
| 326 | + * Do NOT double space your code. Group like statements together. |
|
| 327 | + * Track/version the source code in your private semester repository |
|
| 328 | + * Submit a copy of your source code to me using the **submit** tool |
|
| 329 | + |
|
| 330 | +To submit this program to me using the **submit** tool, run the following |
|
| 331 | +command at your LAB46 prompt: |
|
| 332 | + |
|
| 333 | +``` |
|
| 334 | +lab46:~/src/SEMESTER/cprog/dow0$ make submit |
|
| 335 | +``` |
|
| 336 | + |
|
| 337 | +You should get some sort of confirmation indicating successful submission |
|
| 338 | +if all went according to plan. If not, check for typos and or locational |
|
| 339 | +mismatches. |
|
| 340 | + |
|
| 341 | +What I'll be looking for: |
|
| 342 | + |
|
| 343 | +### RUBRIC |
|
| 344 | + |
|
| 345 | +I'll be evaluating the project based on the following criteria: |
|
| 346 | + |
|
| 347 | +``` |
|
| 348 | +156:dow0:final tally of results (156/156) |
|
| 349 | +*:dow0:grabit the code on lab46 by Sunday before deadline [26/26] |
|
| 350 | +*:dow0:code is pushed to private semester repository [13/13] |
|
| 351 | +*:dow0:clean compile, no compiler messages [26/26] |
|
| 352 | +*:dow0:program conforms to project specifications [52/52] |
|
| 353 | +*:dow0:project submitted with 'make submit' [13/13] |
|
| 354 | +*:dow0:program passes verification checks [26/26] |
|
| 355 | +``` |
|
| 356 | + |
|
| 357 | +NOTE: The spirit of program implementation includes your process |
|
| 358 | +using the mental math approach, not a more seemingly-straightforward |
|
| 359 | +computational approach, or one given to you out of context by AI. |
|
| 360 | + |
|
| 361 | +Additionally: |
|
| 362 | + * Solutions not abiding by spirit of project will be subject to a 50% overall deduction |
|
| 363 | + * Solutions not utilizing descriptive why and how comments will be subject to a 25% overall deduction |
|
| 364 | + * Solutions not utilizing indentation to promote scope and clarity will be subject to a 25% overall deduction |
|
| 365 | + * Solutions not organized and easy to read are subject to a 25% overall deduction |
haas/spring2026/cprog/projects/sof0.md
| ... | ... | @@ -147,11 +147,11 @@ lab46:~/src/SEMESTER/DESIG$ |
| 147 | 147 | At which point you can change into the newly created and populated |
| 148 | 148 | `sof0` directory. |
| 149 | 149 | |
| 150 | -Be sure to **add**, **commit**, and **push** this to your lab46 clone of |
|
| 151 | -your repository. |
|
| 150 | +Be sure, using **git**, to **add**, **commit**, and **push** this to your |
|
| 151 | +lab46 clone of your repository. |
|
| 152 | 152 | |
| 153 | -Then, if working on your pi/system, **pull** and **update** to get the |
|
| 154 | -files available there, or continue working on it on lab46. |
|
| 153 | +Then, if working on your pi/system, **git pull** to get the files |
|
| 154 | +available there, or continue working on it on lab46. |
|
| 155 | 155 | |
| 156 | 156 | ## PROGRAM |
| 157 | 157 |