User Tools

Site Tools


user:acrowle1:portfolio:cprogproject4

This is an old revision of the document!


Project: dayofweek

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:

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. 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

/*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;

A C program Written by: Alana Whittier for CSCS1320S14 on March 7, 2014; */

#include <stdio.h> #include <stdlib.h>

int main() {

int i; int *p; p=&i; int year;

printf(“Type last two digits of year from 2001-2099;\n”); scanf(“%d”, p);

year=i; i=(float)1); i=(year+i)%7;

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);} 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);}

return(0); }

</code>

Execution

Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:

lab46:~/src/cprog$ ./hello
Hello, World!
lab46:~/src/cprog$ 

Reflection

Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?

References

In performing this project, the following resources were referenced:

  • URL1
  • URL2
  • URL3 (provides useful information on topic)
  • URL4

Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)

1)
(i/2)/2
user/acrowle1/portfolio/cprogproject4.1394418755.txt.gz · Last modified: 2014/03/10 02:32 by acrowle1