Instructions: Write a program that asks a user to enter a date in month day year
ID: 3910576 • Letter: I
Question
Instructions:
Write a program that asks a user to enter a date in month day year format
(for example 10 12 2016) and displays the day of the week for that date, in this case, Wednesday.
Be sure to validate month and day to be sure they are in range. To validate 2/29/yyyy, you will have to call the isLeapyearfunction.
The algorithm discussed here is based upon the article athttp://en.wikipedia.org/wiki/Calculating_the_day_of_the_weekunder the heading:
A tabular method to calculate the day of the week
You will require several functions:
bool isLeapYear(int year);
This function will return true if the year passed is a leap year and false if not. The pseudocode to determine if a given year is a leap year is:
leapYear = ((year evenly divisible by 400) or ( year evenly divisible by 4 and year NOT evenly divisible by 100))
int getCenturyValue(int year);
This function should take the first two digits of the year (ie the century), divide by 4 and save the remainder. Subtract the remainder from 3, multiply the result by 2 and return that value. For example the year 2013 becomes (20/4) = 5, remainder 0. 3 – 0 = 3 return 3 * 2 = 6. Hint, to get the century divide by 100.
int getYearValue(int year);
This function computes a value based on the years since the beginning of the century. First, extract the last two digits of the year (year%100), in this case 13. Next, factor in leap years. Divide the last two digits by 4 and add that back to the result and return that. Eg (13/4) = 3. 3 + 13 = 16.
int getMonthValue(int month, int year);
This function should return a value based on the table below and will require calling theisLeapYearfunction.
Month
Return (if not leap year)
Return (if leap year)
1
0
6
2
3
2
3
3
3
4
6
6
5
1
1
6
4
4
7
6
6
8
2
2
9
5
5
10
0
0
11
3
3
12
5
5
Finally, to compute the day of the week, add together the day and the values returned by getMonthValue, getyearValue, and getCenturyValue. Divide that sum modulo by 7.
0 indicates Sunday, 1 Monday, 2 Tuesday, etc.
Sample run:
I am having a hard time with
int getMonthValue(int month, int year);
This function should return a value based on the table below and will require calling theisLeapYearfunction.
I tried using a switch statement, but I keep getting this error. "Control reaches end of non-void function".
Month
Return (if not leap year)
Return (if leap year)
1
0
6
2
3
2
3
3
3
4
6
6
5
1
1
6
4
4
7
6
6
8
2
2
9
5
5
10
0
0
11
3
3
12
5
5
Explanation / Answer
here I have provided an approach how u can create the function get value using a structure in C.
#include <stdio.h>
#include<malloc.h>
typedef struct array{
int il;
int nl;
}a;
int getMonthValue(int month, int year)
{
struct array *a=(int*)malloc(sizeof(int)*12);
//inserting table entries into a structure having a 2 values in an array
a[0].il=6;
a[0].nl=0;
a[1].il=2;
a[1].nl=3;
a[2].il=3;
a[2].nl=3;
a[3].il=6;
a[3].nl=6;
a[4].il=1;
a[4].nl=1;
a[5].il=4;
a[5].nl=4;
a[6].il=6;
a[6].nl=6;
a[7].il=2;
a[7].nl=2;
a[8].il=5;
a[8].nl=5;
a[9].il=0;
a[9].nl=0;
a[10].il=3;
a[10].nl=3;
a[11].il=5;
a[11].nl=5;
//printf("%d%d%d",a[0].il,a[0].nl,a[1].il);
// printf("Hello World");
bool r=isLeapYear(int year);
if(r==1)
return a[month+1].il;
else
return a[month+1].nl;
}
int main()
{
int month,year;
int value=getMonthValue(int month, int year);
return 0;
}