User Tools

Site Tools


user:acrowle1:portfolio:cprogproject4

Differences

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

Link to this comparison view

Next revision
Previous revision
user:acrowle1:portfolio:cprogproject4 [2014/03/09 14:41] – created acrowle1user:acrowle1:portfolio:cprogproject4 [2014/03/23 16:58] (current) – [Reflection] acrowle1
Line 1: Line 1:
 +======Project: dayofweek======
  
-portfolio » cprogproject4+A project for C/C++ programming, CSCS1320S14, by Alana Whittier during the Spring semester 2014.
  
--------------------------------------------------------------------------------- +This project was begun on March 7, 2014 and it took nearly 3 days to complete.  
-  +=====Objectives===== 
- +The purpose of this project is to write a program that utilizes a mental math trick to determine what day of the week that January 1st of any given year falls on in the 21st century. A two digit year (between 2001 and 2099) is input from the user and is computed using a series of selection statements to derive the day of the week that Jan 1st falls on per calendar year, minus one day for leap years.   
 +=====Prerequisites===== 
 +In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:
  
 +**Resources:**
 +  * http://vpcalendar.net/when-is-the-next-leap-year.html
 +  * http://www.timeanddate.com/date/leapyear.html
 +  * project assignment page
 +  * consultation with C/C++ programming instructor, Matt Haas
 +**Experiences:**
 +  * ability to obtain user input
 +  * ability to obtain the day of the week January 1st falls on per given year.
 +  * ability to use selection statements to achieve desired result
 +  * ability to use Bitwise && to distinguish between leap year and non-leap year
 +=====Background=====
 +The purpose of this project is to use a mental math technique to programmatically compute the day on which January 1st falls per given calendar year in the 21st century. The "mental math" trick is used to simplify the process of computation without being dependent on a calculator. To see how this particular technique works for computing the week day that January 1st falls on, see the assignment link below.
 +[[haas:spring2014:cprog:projects:dayofweek|Project: MENTAL MATH (DAY OF WEEK)]]
 +To implement the program to compile and execute successfully, it is required to know how to determine leap years from non-leap years. As we experience leap years as much as every four years, simple algorithms can be used to determine the leap year. However, every 100 years, a leap year is skipped to make up for the 11 minutes removed from the 365.25 day calendar year.  
  
 +=====Scope=====
 +The scope of the project is to write a program that utilizes the described mental math technique (see assignment in Background section above) to compute the week day that January 1st happens to fall on for a given year in the 21st century (2000-2099). In doing so, the program should prompt the user for the last two digits of the year, perform the calculation of the day of the week that January 1st falls on for the year given by user input, and display the value. Applying pointers to enable the use of scanf() for obtaining input from the user, using if() statements, are used in the implementation of this program. 
 +=====Attributes=====
 + Upon successful completion of this project, the following attributes are achieved.
  
 +  * More familiarity with pointers and scanf: This project requires user input.
 +  * Become more exposed to selection statements: if statements are used to compute the day of the week January 1st falls on per given calendar year.
 +  * Get more comfortable with Bitwise && operators: used to differentiate between leap years and non-leap years to obtain the correct output.
 +=====Procedure=====
 +The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.
  
 +=====Code=====
 +</code>
 +/*dayofweek.c- A program using a math technique to output the day of the week;
 +               that January 1st falls on from calender years 2001 to 2099 based;
 +               on what year the user inputs;
  
-content  +A C program Written by: Alana Whittier for CSCS1320S14 on March 7, 2014; 
-◾ +*/
-Wiki Page+
  
- +#include <stdio.h> 
-Opus+#include <stdlib.h>
  
- +int main() 
-Portfolio+{
  
- +int i; 
-Repository+int *p; 
 +p=&i; 
 +int year;
  
  
-  
-courses  
- 
-CSCS1320 cprog 
  
-  +printf("Type last two digits of year from 2001-2099;\n"); 
 +scanf("%d", p);
  
 +year=i;
 +i=(float)(((i/2)/2));
 +i=(year+i)%7;
  
-user:acrowle1:portfolio:cprogproject3 
  
-  +//leap years! 
 +if(year%4==0&&(year/4!=0)){ 
 +        printf("year, %d, is a leap year\n",2000+ year);} 
 +   if(i==1&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st,%d, is on : Sunday\n", 2000+ year);} 
 +   if(i==2&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on: Monday\n", 2000+year);
 +   if(i==3&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on: Tuesday\n", 2000+year);
 +   if(i==4&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on: Wednesday\n", 2000+year);
 +   if(i==5&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on: Thursday\n", 2000+year);
 +   if(i==6&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on: Friday\n", 2000+year);
 +   if(i==7&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on Saturday\n", 2000+year);
 +   if(i==0&&(year%4==0&&(year/4!=0))){ 
 +        printf("Jan 1st, %d, is on Saturday\n", 2000+year);}
  
-Table of Contents 
  
 +//not leap years!
 +    else if(i==1&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Monday\n", 2000+year);}
 +    else if(i==2&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Tuesday\n", 2000+year);}
 +    else if(i==3&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Wednesday\n", 2000+year);}
 +    else if(i==4&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Thursday\n", 2000+year);}
 +    else if(i==5&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Friday\n", 2000+year);}
 +    else if(i==6&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Saturday\n", 2000+year);}
 +    else if(i==7&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Sunday\n", 2000+year);}
 +    else if(i==0&&(year%4!=0)){
 +        printf("Jan 1st, %d, is on: Sunday\n", 2000+year);}
  
-Project: squares+return(0); 
 +}
  
-Objectives 
  
 +        
 +        
 +        
 +        
  
-Prerequisites+</code>
  
 +=====Execution=====
  
-Background 
  
 +<cli>
 +lab46:~/src/cscs1320$ nano dayofweek.c
 +lab46:~/src/cscs1320$ gcc -o dayofweek dayofweek.c
 +lab46:~/src/cscs1320$ ./dayofweek
 +Type last two digits of year from 2001-2099;
 +04
 +year, 2004, is a leap year
 +Jan 1st, 2004, is on: Thursday
 +lab46:~/src/cscs1320$ cal 01 2004
 +    January 2004
 +Su Mo Tu We Th Fr Sa
 +              2  3
 +  5  6  7  8  9 10
 +11 12 13 14 15 16 17
 +18 19 20 21 22 23 24
 +25 26 27 28 29 30 31
  
-Scope+lab46:~/src/cscs1320$ ./dayofweek 
 +Type last two digits of year from 2001-2099; 
 +67 
 +Jan 1st, 2067, is on: Saturday 
 +lab46:~/src/cscs1320$ cal 01 2067 
 +    January 2067 
 +Su Mo Tu We Th Fr Sa 
 +                   1 
 +  3  4  5  6  7  8 
 + 9 10 11 12 13 14 15 
 +16 17 18 19 20 21 22 
 +23 24 25 26 27 28 29 
 +30 31
  
 +</cli>
  
-Attributes +=====Reflection===== 
- +Interestingly enough, the year 2000 would not be leap year if only using the first 2 criteria.  
- +The criteria:  
-Procedure +  If year is evenly divisible by 4it is a leap year
- +  If year is evenly divided by 100, it is not a leap yearexcept when  
- +  the year is also evenly divisible by 400.
-Code +
- +
- +
-Execution +
- +
- +
-Reflection +
- +
- +
-References +
- +
- +
-  +
-Project: squares +
- +
- +
-A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014.  +
- +
-This project was first begunon or around, February, 20, 2014 and took approximately 1 week to complete. The program I first submitted is not incorrect, as it does compile and yields the expected output, although it is not necessarily the most elegant or efficient script. In that first program, series of 'if' and 'else if' statements were used to ensure appropriate execution of the program. After some more consideration, I decided to try writing the program in an entirely different manner, using both the factor and the remainder to achieve the same output as produced in my first program. However, even this endeavor had it's challenges as I am still not sure how to conditionally tell the compiler for ALL values greater than 95, INCLUDING those that end in 5, to only return the error message +
- +
- +
- +
- +
- +
-Objectives +
- +
- +
-The purpose of this project is to become familiarized with using a pointer in order to use the scanf() feature for obtaining input from a user. Also, this project serves to introduce a new user of C programming to conditional statements in order to achieve the desired output. In order to successfully compile and execute the program using a mental math technique, a pointer must be applied appropriately, scanf() must be implemented to call for user input and communicate back to the compiler, and the use of conditional statements (if statements, etc.) must be used.  +
- +
- +
- +
- +
- +
-Prerequisites +
- +
- +
-In order to successfully accomplish/perform this project, the listed resources/experiences may be consulted/achieved:  +
- +
-Helpful Resources:  +
-◾ +
-http://wildaboutmath.com/2007/11/11/impress-your-friends-with-mental-math-tricks/comment-page-6/ +
- +
-◾ +
-http://saurabhg.hubpages.com/hub/Vedic-Mathematics---Quick-multiplication-techniques---Part-1 +
- +
-◾ +
- Kernighan, Ritchie The C Programming Language Second Edition, AT&T Bell Laboratories, 1988. Print. +
- +
-◾ +
- email consultations and guidance from Matt Haas +
- +
- +
-Experiences necessary:  +
-◾ +
- ability to edit code +
- +
-◾ +
- ability to use lab46 remotely +
- +
-◾ +
- an understanding of the mental math technique to apply to the two digit integers ending in 5 to acquire the square of that number +
- +
-◾ +
- a basic understanding of pointers and scanf() to obtain user input +
- +
-◾ +
- ability to use conditional statements to achieve desired output +
- +
- +
- +
- +
- +
- +
-Background +
- +
- +
-This project implements a mental math technique to compute the square of any two-digit integer ending in 5. A program is written in a manner consistent with the technique described in the project assignment page.  +
- +
-See project assignment page on the provided link.  +
- +
-Project: MENTAL MATH (SQUARES OF 5)  +
- +
-This mental math technique, as well as many others, is part of collection of what is commonly called Vedic Mathematics, methods to compute quickly, without the use of a calculator, or performing a lengthy multi-step multiplication process by hand.  +
- +
- +
- +
- +
- +
-Scope +
- +
- +
-The motivation behind this project is to become familiarized with applying a pointer in order to use the Scanf() feature to acquire user input for computing the squares of two-digit integers ending in 5. A program is written in which the Vedic math technique is applied to compute the squares individually and output them for the user.  +
- +
- +
- +
- +
- +
-Attributes +
- +
- +
-State and justify the attributes you'd like to receive upon successful approval and completion of this project.  +
-◾ +
- write program to successfully calculate the square of the two-digit integer ending in 5 using the Mental Math Technique +
- +
-◾ +
- Obtain user input with a prompt by proper implementation of pointer and use of scanf().  +
- +
-◾ +
- Output both the user input value and the square of that value.  +
- +
- +
- +
- +
- +
- +
-Procedure +
- +
- +
-My first attempt at writing the program was using the modulus operator%, although abandoned that effort quickly as it was not clear what to do with the factor in order to implement the mental math technique. I then decided to implement the computation of the square in what I deemed more simplistic of an approach and then used a series of if, else if statements to output only the values I wanted the user to input and obtain (two-digit integers ending in 5), and finally an else statement in which “Error: Invalid Entry” is displayed for all values greater than 95 and for all values that do not end in 5.  +
- +
-Example 1 (My first submitted program):  +
- +
-#include <stdio.h> #include <stdlib.h>  +
- +
-int main() {  +
-       +
-      int i; +
-      int *p; +
-      p=&i; +
-      printf("type up to two digit integer ending in 5;\n"); +
-      scanf("%d"p); +
-      if(i==15){ +
-              printf("%.2d: %d\n",i, (1*(1+1))*100 + 5*5);} +
-  else if(i==25){ +
-              printf("%.2d: %d\n",i, (2*(2+1))*100 +5*5);} +
- else if(i==35){ +
-              printf("%.2d: %d\n",i, (3*(3+1))*100 +5*5);} +
-      else if(i==45){ +
-              printf("%.2d: %d\n",i, (4*(4+1))*100 +5*5);}  +
-      else if(i==55){ +
-              printf("%.2d: %d\n",i, (5*(5+1))*100 +5*5);} +
-      else if(i==65){ +
-              printf("%.2d: %d\n",i, (6*(6+1))*100 +5*5);}  +
-      else if(i==75){ +
-              printf("%.2d: %d\n",i, (7*(7+1))*100 +5*5);         +
-      else if(i==85){ +
-              printf("%.2d: %d\n",i, (8*(8+1))*100 +5*5);} +
-      else if(i=95){ +
-              printf("%.2d: %d\n",i, (9*(9+1))*100 +5*5);  +
- else  +
- printf("Error: Invalid Entry\n"); +
-      return(0); +
- +
-}  +
- +
-After more consideration, I decided to try again writing the program using the modulus operator, which I called R and the factor which I declared a variable. This code is substantially more efficient as it does not require so many lines of code to obtain the squared values. However, I am uncertain how to completely limit the computation and output, since this approach also works for 3 and 4 digit integers ending in five.  +
- +
-Example 2 (The second program using modulus operator and factor):  +
- +
-#include <stdio.h> #include <stdlib.h>  +
- +
-int main() {  +
-      int i; +
- int factor, R; +
-      int *p; +
-      p=&i; +
-      printf("type up to a two digit integer ending in 5;\n"); +
-      scanf("%d", p); +
-  +
- factor = i/10; +
-      R = i % 10; +
-  +
-if(R==5) +
-      printf("%.2d: %d\n",i, (factor*(factor+1))*100 + 5*5 ); +
-if(i>95) +
- printf("Error: Invalid Entry"); +
-      return(0); +
- +
-}  +
- +
- +
- +
- +
- +
-Code +
- +
- +
-<code c> /*Squares.c - A program which implements a mental math;  +
-            technique for computing the square of any; +
-   two digit integer ending in 5, given by +
-   user input; +
-    +
- +
-Written by: Alana Whittier for CSCS1320S14 on February 26, 2014;  +
- +
-Compile with: gcc- - squares squares.c; Execute with: ./squares */  +
- +
-#include <stdio.h> #include <stdlib.h>  +
- +
-int main() {  +
-       +
-      int i; +
-      int *p; +
-      p=&i; +
-      printf("type up to a two digit integer ending in 5;\n"); +
-      scanf("%d", p); +
-      if(i==15){ +
-              printf("%.2d: %d\n",i, (1*(1+1))*100 + 5*5);} +
-  else if(i==25){ +
-              printf("%.2d: %d\n",i, (2*(2+1))*100 +5*5);} +
- else if(i==35){ +
-              printf("%.2d: %d\n",i, (3*(3+1))*100 +5*5);} +
-      else if(i==45){ +
-              printf("%.2d: %d\n",i, (4*(4+1))*100 +5*5);}  +
-      else if(i==55){ +
-              printf("%.2d: %d\n",i, (5*(5+1))*100 +5*5);} +
-      else if(i==65){ +
-              printf("%.2d: %d\n",i, (6*(6+1))*100 +5*5);}  +
-      else if(i==75){ +
-              printf("%.2d: %d\n",i, (7*(7+1))*100 +5*5);         +
-      else if(i==85){ +
-              printf("%.2d: %d\n",i, (8*(8+1))*100 +5*5);} +
-      else if(i=95){ +
-              printf("%.2d: %d\n",i, (9*(9+1))*100 +5*5);  +
- else  +
- printf("Error: Invalid Entry\n"); +
-      return(0); +
- +
-}  +
- +
- +
- +
- +
- +
-Execution +
- +
-lab46:~/src/cscs1320$ nano squares.c +
-lab46:~/src/cscs1320$ gcc -o squares squares.c +
-lab46:~/src/cscs1320$ ./squares +
-type up to a two digit integer ending in 5; +
-25 +
-25: 625 +
-lab46:~/src/cscs1320$ ./squares +
-type up to a two digit integer ending in 5; +
-55 +
-55: 3025 +
-lab46:~/src/cscs1320$ ./squares +
-type up to a two digit integer ending in 5; +
-60 +
-Error: Invalid Entry +
-lab46:~/src/cscs1320$ ./squares +
-type up to a two digit integer ending in 5; +
-95 +
-95: 9025 +
-lab46:~/src/cscs1320$ ./squares +
-type up to a two digit integer ending in 5; +
-100 +
-Error: Invalid Entry +
-lab46:~/src/cscs1320$ +
- +
- +
- +
- +
- +
- +
- +
-Reflection +
- +
- +
-In writing this program, I encountered some initial difficulty with the use of if, else if, and else statements. I was not understanding why the program was not doing as I expected. After a series of trial and error attempts, it was observed that my code would work if I used the correct syntax. First mistake: the semi-colon following the if, else if, else conditions that I tried to impose. Removing them helped significantly. Second mistake: The way I used the curly braces. Essentially, I was embedding several else if statements within the initial if. By correcting this my code worked flawlessly.  +
- +
-Since my initial attempt at writing this program included the modulus operator, which had been quickly abandoned, I decided to revisit that since I now had a working program written in a manner I felt was more simplistic. What I realized that I was missing from my initial program was the factor (the number of times that 10 went into the integer). For example, if I declared the factor to be i/10 and R= i%10, then for an integer value of 25, R=5 and the factor=2, since 10 can go into 25 twice, with a remainder of 5. With this program written this way, the same mental math technique works for 3 and 4 digit integers ending in 5 as well, to compute the squares. This code is shown above in Example 2 of the Procedure section.  +
- +
- +
- +
- +
- +
-References +
- +
- +
-In performing this project, the following resources were referenced:  +
-◾ +
-http://wildaboutmath.com/2007/11/11/impress-your-friends-with-mental-math-tricks/comment-page-6/ +
- +
-◾ +
-http://saurabhg.hubpages.com/hub/Vedic-Mathematics---Quick-multiplication-techniques---Part-1 +
- +
-◾ +
- Kernighan, Ritchie The C Programming Language Second Edition, AT&T Bell Laboratories, 1988. Print. +
- +
-◾ +
- email consultations and guidance from Matt Haas+
  
 +The year 2000 is a significant calendar year because it was the first instance when the third criteria was invoked since switching from the Julian calendar to the Gregorian one. The reason for leap years? This was necessary to keep our calendar in alignment with Earth's revolutions around the sun. If we did not add a day to our calendar ~every 4 years, we would lose nearly 6 hours every year, so after 100 years our calendar would be off by nearly 24 days as measured by earth's revolutions. Why is a leap year skipped every 100 years, except in special instances (like year 2000)? This is to account for the 11 minutes shaved off from the 365.25 calendar year. 
  
 +While I wrote my code more simplistically saying if i divided by 4 equals zero, then it is a leap year and if i divided by 4 does not equal zero, not a leap year, the code worked. I could have added that i/100==0 || i/400==0 is a leap year to make my code more realistic to how the calendar interprets leap years from non-leap years, particularly in special instances such as 2000.
 +=====References=====
 +In performing this project, the following resources were referenced:
  
 +  * http://www.timeanddate.com/date/leapyear.html
 +  * http://vpcalendar.net/when-is-the-next-leap-year.html
 +  * [[haas:spring2014:cprog:projects:dayofweek|Project: MENTAL MATH (DAY OF WEEK)]]
  
  
user/acrowle1/portfolio/cprogproject4.1394376098.txt.gz · Last modified: 2014/03/09 14:41 by acrowle1