This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
user:acrowle1:portfolio:cprogproject4 [2014/03/09 14:41] – created acrowle1 | user:acrowle1:portfolio:cprogproject4 [2014/03/23 16:58] (current) – [Reflection] acrowle1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
- | portfolio » cprogproject4 | + | A project for C/C++ programming, |
- | -------------------------------------------------------------------------------- | + | 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/ | ||
+ | **Resources: | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * 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 " | ||
+ | [[haas: | ||
+ | To implement the program to compile and execute successfully, | ||
+ | =====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===== | ||
+ | </ | ||
+ | / | ||
+ | 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 < |
- | Opus | + | #include < |
- | ◾ | + | int main() |
- | Portfolio | + | { |
- | ◾ | + | int i; |
- | Repository | + | int *p; |
+ | p=&i; | ||
+ | int year; | ||
- | |||
- | courses | ||
- | ◾ | ||
- | CSCS1320 cprog | ||
- | | + | printf(" |
+ | scanf(" | ||
+ | year=i; | ||
+ | i=(float)(((i/ | ||
+ | i=(year+i)%7; | ||
- | user: | ||
- | | + | //leap years! |
+ | if(year%4==0&& | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
+ | | ||
+ | printf(" | ||
- | Table of Contents | ||
+ | //not leap years! | ||
+ | else if(i==1&& | ||
+ | printf(" | ||
+ | else if(i==2&& | ||
+ | printf(" | ||
+ | else if(i==3&& | ||
+ | printf(" | ||
+ | else if(i==4&& | ||
+ | printf(" | ||
+ | else if(i==5&& | ||
+ | printf(" | ||
+ | else if(i==6&& | ||
+ | printf(" | ||
+ | else if(i==7&& | ||
+ | printf(" | ||
+ | else if(i==0&& | ||
+ | printf(" | ||
- | Project: squares | + | return(0); |
+ | } | ||
- | Objectives | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
- | Prerequisites | + | </ |
+ | =====Execution===== | ||
- | Background | ||
+ | <cli> | ||
+ | lab46: | ||
+ | lab46: | ||
+ | lab46: | ||
+ | Type last two digits of year from 2001-2099; | ||
+ | 04 | ||
+ | year, 2004, is a leap year | ||
+ | Jan 1st, 2004, is on: Thursday | ||
+ | lab46: | ||
+ | January 2004 | ||
+ | Su Mo Tu We Th Fr Sa | ||
+ | | ||
+ | | ||
+ | 11 12 13 14 15 16 17 | ||
+ | 18 19 20 21 22 23 24 | ||
+ | 25 26 27 28 29 30 31 | ||
- | Scope | + | lab46: |
+ | Type last two digits of year from 2001-2099; | ||
+ | 67 | ||
+ | Jan 1st, 2067, is on: Saturday | ||
+ | lab46: | ||
+ | January 2067 | ||
+ | Su Mo Tu We Th Fr Sa | ||
+ | 1 | ||
+ | | ||
+ | 9 10 11 12 13 14 15 | ||
+ | 16 17 18 19 20 21 22 | ||
+ | 23 24 25 26 27 28 29 | ||
+ | 30 31 | ||
+ | </ | ||
- | Attributes | + | =====Reflection===== |
- | + | Interestingly enough, the year 2000 would not be a leap year if only using the first 2 criteria. | |
- | + | The criteria: | |
- | Procedure | + | - If year is evenly divisible by 4, it is a leap year. |
- | + | - If year is evenly divided | |
- | + | | |
- | Code | + | |
- | + | ||
- | + | ||
- | Execution | + | |
- | + | ||
- | + | ||
- | Reflection | + | |
- | + | ||
- | + | ||
- | References | + | |
- | + | ||
- | + | ||
- | + | ||
- | Project: squares | + | |
- | + | ||
- | + | ||
- | A project for CSCS1320S14 by Alana Whittier during the Spring Semester 2014. | + | |
- | + | ||
- | This project was first begun, on 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 | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | 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, | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | Prerequisites | + | |
- | + | ||
- | + | ||
- | In order to successfully accomplish/ | + | |
- | + | ||
- | Helpful Resources: | + | |
- | ◾ | + | |
- | http:// | + | |
- | + | ||
- | ◾ | + | |
- | http:// | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | ◾ | + | |
- | email consultations and guidance from Matt Haas | + | |
- | + | ||
- | + | ||
- | Experiences necessary: | + | |
- | ◾ | + | |
- | | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | ◾ | + | |
- | 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 | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | Background | + | |
- | + | ||
- | + | ||
- | This project implements a mental math technique to compute the square of any two-digit integer ending in 5. A program | + | |
- | + | ||
- | 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 a collection of what is commonly called Vedic Mathematics, | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | 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 | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | 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 | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | 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” | + | |
- | + | ||
- | Example 1 (My first submitted program): | + | |
- | + | ||
- | #include < | + | |
- | + | ||
- | int main() { | + | |
- | + | ||
- | int i; | + | |
- | int *p; | + | |
- | p=&i; | + | |
- | printf(" | + | |
- | scanf(" | + | |
- | | + | |
- | printf(" | + | |
- | else if(i==25){ | + | |
- | printf(" | + | |
- | else if(i==35){ | + | |
- | printf(" | + | |
- | else if(i==45){ | + | |
- | printf(" | + | |
- | else if(i==55){ | + | |
- | printf(" | + | |
- | else if(i==65){ | + | |
- | printf(" | + | |
- | else if(i==75){ | + | |
- | printf(" | + | |
- | else if(i==85){ | + | |
- | printf(" | + | |
- | else if(i=95){ | + | |
- | printf(" | + | |
- | else | + | |
- | printf(" | + | |
- | return(0); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | After more consideration, | + | |
- | + | ||
- | Example 2 (The second program using modulus operator and factor): | + | |
- | + | ||
- | #include < | + | |
- | + | ||
- | int main() { | + | |
- | int i; | + | |
- | int factor, R; | + | |
- | int *p; | + | |
- | p=&i; | + | |
- | printf(" | + | |
- | scanf(" | + | |
- | + | ||
- | factor = i/10; | + | |
- | R = i % 10; | + | |
- | + | ||
- | if(R==5) | + | |
- | printf(" | + | |
- | if(i> | + | |
- | printf(" | + | |
- | 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 < | + | |
- | + | ||
- | int main() { | + | |
- | + | ||
- | int i; | + | |
- | int *p; | + | |
- | p=&i; | + | |
- | printf(" | + | |
- | scanf(" | + | |
- | if(i==15){ | + | |
- | printf(" | + | |
- | else if(i==25){ | + | |
- | printf(" | + | |
- | else if(i==35){ | + | |
- | printf(" | + | |
- | else if(i==45){ | + | |
- | printf(" | + | |
- | else if(i==55){ | + | |
- | printf(" | + | |
- | else if(i==65){ | + | |
- | printf(" | + | |
- | else if(i==75){ | + | |
- | printf(" | + | |
- | else if(i==85){ | + | |
- | printf(" | + | |
- | else if(i=95){ | + | |
- | printf(" | + | |
- | else | + | |
- | printf(" | + | |
- | return(0); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | Execution | + | |
- | + | ||
- | lab46: | + | |
- | lab46: | + | |
- | lab46: | + | |
- | type up to a two digit integer ending in 5; | + | |
- | 25 | + | |
- | 25: 625 | + | |
- | lab46: | + | |
- | type up to a two digit integer ending in 5; | + | |
- | 55 | + | |
- | 55: 3025 | + | |
- | lab46: | + | |
- | type up to a two digit integer ending in 5; | + | |
- | 60 | + | |
- | Error: Invalid Entry | + | |
- | lab46: | + | |
- | type up to a two digit integer ending in 5; | + | |
- | 95 | + | |
- | 95: 9025 | + | |
- | lab46: | + | |
- | type up to a two digit integer ending in 5; | + | |
- | 100 | + | |
- | Error: Invalid Entry | + | |
- | lab46: | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | 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, | + | |
- | + | ||
- | 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:// | + | |
- | + | ||
- | ◾ | + | |
- | http:// | + | |
- | + | ||
- | ◾ | + | |
- | | + | |
- | + | ||
- | ◾ | + | |
- | 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' | ||
+ | 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:// | ||
+ | * http:// | ||
+ | * [[haas: | ||