This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
haas:spring2014:cprog:projects:dayofweek [2014/01/17 07:55] – [Background] wedge | haas:spring2014:cprog:projects:dayofweek [2014/02/06 17:39] (current) – [Reflection] wedge | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <WRAP centeralign round box> | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | ~~TOC~~ | ||
+ | |||
======Project: | ======Project: | ||
- | A project for C/C++ Programming. | ||
=====Objective===== | =====Objective===== | ||
To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of determining what day of the week January 1 of any given year (in the 21st century) falls on. | To implement a programmatic solution (ie simulation) of a real life process- the mental math trick of determining what day of the week January 1 of any given year (in the 21st century) falls on. | ||
- | =====Prerequisites===== | + | ======Assumptions====== |
+ | |||
+ | To assist you in completing this project, you may make the following assumptions: | ||
+ | |||
+ | * all requests will be for days in the 21st century (2000-2099) | ||
+ | =====Prerequisites/ | ||
In addition to the new skills required on previous projects, to successfully accomplish/ | In addition to the new skills required on previous projects, to successfully accomplish/ | ||
Line 15: | Line 26: | ||
The process in this case is one of simple (reduced) multiplication and mapping against a table. To wit: | The process in this case is one of simple (reduced) multiplication and mapping against a table. To wit: | ||
- | ====Squaring a value==== | + | ====Day values==== |
- | Squaring is essentially multiplying | + | For this trick to work, we need to be familiar with the following table (a map of days to numeric values): |
- | * 5 squared (5< | + | ^ |
- | * 8 squared (8< | + | | 1 | |
- | While not outwardly a difficult procedure, the nature | + | ====Calculating day of the week based on year==== |
- | Finding a shortcut through this process enables it to remain solidly within the realm of mental math, and makes for a good algorithm to practice implementing on the computer. | + | Okay, time for the magic. |
- | This particular trick relies | + | Let us try it on January 1st, 2014. |
- | The operational scope of this trick will be just those two-digit values ending | + | ===Step 1: Obtain last two digits of the year=== |
+ | In our example, we're working | ||
- | * 15 | + | You should be able to come up with a means of extracting this information in your program. |
- | * 25 | + | |
- | * 35 | + | |
- | * 45 | + | |
- | * 55 | + | |
- | * 65 | + | |
- | * 75 | + | |
- | * 85 | + | |
- | * 95 | + | |
- | ====Squaring double digit values ending with 5==== | + | ===Step 2: Compute 25% (drop the decimal)=== |
- | The trick here is two-fold. | + | Even this is something we can do in our heads. I can think of two approaches right off the bat: |
- | One, we square the 5 to get 25. That is how our result will end (so bam! we now have our tens and ones place already solved) | + | ==Approach 1: 10 + 10 + 5== |
+ | 10% percent of anything is merely dropping the last digit. 10% of 54 is 5. | ||
- | Next, we take the value in the tens place, and multiply it by its increment: | + | For our 2014 example, 10% of 14 is therefore 1. |
- | * 1's increment (1+1) is 2, so 1*2 | + | So we need two 10 percents... |
- | * 2's increment (2+1) is 3, so 2*3 | + | |
- | * 3's increment (3+1) is 4, so 3*4 | + | |
- | * 4's increment (4+1) is 5, so 4*5 | + | |
- | * ... | + | |
- | * 9's increment (9+1) is 10, so 9*10 | + | |
- | We take this result and append the 25 after it. | + | Finally, 5% is half of 10% (half of 1 is 0.5), so 1.4 + 1.4 + 0.5 = 3.3 |
- | For example: | + | But, since we do not care about the decimal, we drop it and are left with just 3. |
- | < | + | ==Approach |
- | 15 * 15 = 1*(1+1) 5*5 | + | 25% is a convenient value for us with respect to 100, allowing this optimized approach to work. |
- | | + | |
- | | + | |
- | | + | |
- | </ | + | |
- | and: | + | * Half of 100 is 50 (50%) |
+ | * Half of 50 is 25 (25%) -- hence the "half of half" | ||
- | < | + | So, 14 cut in half is 7. |
- | 75 * 75 = 7*(7+1) 5*5 | + | |
- | = 7*8 5*5 | + | |
- | = 56 25 | + | |
- | = 5625 | + | |
- | </ | + | |
+ | 7 cut in half is 3.5. | ||
+ | |||
+ | Once again, dropping the decimal yields 3. | ||
+ | |||
+ | ===Step 3: Add 25% to year value=== | ||
+ | Once we have our 25% value, go and add it back to our two-digit year value: | ||
+ | |||
+ | 14 + 3 = 17 | ||
+ | |||
+ | ===Step 4: Subtract the largest fitting multiple of 7=== | ||
+ | |||
+ | Some multiples of 7: | ||
+ | |||
+ | | 0 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | | ||
+ | |||
+ | So, with a value of 17, what is the largest multiple of 7 that is still less than (or equal to) 17? | ||
+ | |||
+ | Hopefully you identified the 14 as the likely candidate. | ||
+ | |||
+ | 17 - 14 = 3 | ||
+ | |||
+ | ===Step 5: Look up day in table=== | ||
+ | We ended up with a 3 as the result for January 1st, 2014. | ||
+ | |||
+ | Go and reference the 3 from that table... what day do we get? Does it match the actual day of the week for January 1st, 2014? | ||
+ | |||
+ | <cli> | ||
+ | lab46:~$ cal 01 2014 | ||
+ | January 2014 | ||
+ | Su Mo Tu We Th Fr Sa | ||
+ | 1 2 3 4 | ||
+ | | ||
+ | 12 13 14 15 16 17 18 | ||
+ | 19 20 21 22 23 24 25 | ||
+ | 26 27 28 29 30 31 | ||
+ | |||
+ | lab46: | ||
+ | </ | ||
+ | |||
+ | Pretty neat, eh? | ||
+ | |||
+ | ====Exception: | ||
+ | In the event of a leap year, we simply subtract 1 from the 25% value, and continue on as usual. | ||
+ | |||
+ | Makes sense, right? Leap years add a day, so something ends up being "off by one". | ||
=====Program===== | =====Program===== | ||
- | It is your task to write the program that will use the above method to compute | + | It is your task to write the program that will use the above method to determine |
Your program should: | Your program should: | ||
- | * prompt the user for the number | + | * prompt the user for the four digit year (input) |
* perform the task (process) | * perform the task (process) | ||
* display the final value (output) | * display the final value (output) | ||
Line 84: | Line 121: | ||
<cli> | <cli> | ||
- | lab46: | + | lab46: |
- | Enter value: 75 | + | Which year: 2014 |
- | 75 x 75 = 5625 | + | January 1st, 2014 falls on: Wednesday |
- | lab46: | + | lab46: |
</ | </ | ||
Line 95: | Line 132: | ||
Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project. | Be sure to provide any commentary on your opus regarding realizations had and discoveries made during your pursuit of this project. | ||
- | * Does this trick work (or can it be adapted to work) for three digit values ending with 5? | + | * 2000 by definition isn't a leap year, but how does this algorithm |
- | * How about 4 digits? | + | * Try this algorithm on some years in the 20th century and see how it fares. |
+ | * Is it correct? Is it correct for any of them? | ||
+ | * If it isn't correct, is it consistently off by a value? | ||
+ | This isn't just about implementing a particular algorithm, it is about understanding an algorithm- its domain of correctness, | ||
=====Submission===== | =====Submission===== | ||
To successfully complete this project, the following criteria must be met: | To successfully complete this project, the following criteria must be met: | ||
Line 115: | Line 155: | ||
<cli> | <cli> | ||
- | $ submit cprog squares squares.c | + | $ submit cprog dayofweek dayofweek.c |
- | Submitting cprog project "squares": | + | Submitting cprog project "dayofweek": |
- | -> squares.c(OK) | + | -> dayofweek.c(OK) |
SUCCESSFULLY SUBMITTED | SUCCESSFULLY SUBMITTED | ||
Line 124: | Line 164: | ||
You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. | You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches. | ||
- | ====Verify submission==== | ||
- | To verify you submitted successfully, | ||
- | |||
- | <cli> | ||
- | lab46:~$ verify cprog squares | ||
- | squares: submitted successfully | ||
- | </ | ||
- | Note if automated assessment is available for the project, you may actually see results in the output as well. |