Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming Quesition (Structs in C): Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date

C Programming Quesition (Structs in C):

Write a C program that prompts the user for a date (mm/dd/yyyy). The program should then take that date and use the formula on page 190 (see problem 2 in the textbook) to convert the date entered into a very large number representing a particular date. Here is the formula from Problem 2 in the textbook:

A formula can be used to calculate the number of days between two dates. This is affected by computing the value of N for each of the two dates and then taking the difference, where N is calculated as follows:

N = 1461 x f(year, month) / 4 + 153 x g(month) / 5 + day

Where: f(year, month) = year - 1 if month <=2 year otherwise

g(month) = month + 13 if month <= 2 month + 1 otherwise

This very large number can be stored in a variable of type long integer, and is referred to in the formula below (and on page 190) as N. Your assignment is to take the value stored in variable N and, using the formula below, determine the number representing the day of the week that the date falls on. That is, if the number calculated by the algorithm is zero, then the day is Sunday. If the number calculated is a 1, then the day is Monday, etc.

The formula for calculating the numeric day of week is as follows: numeric day of week = (N 621049) modulo 7.

Your job is then to take that numeric day of the week (0 - 6), and output the day of the week in English text (such as Monday). See below for a sample program output.

Your program requires the following:

function calc_date_number: which takes a date as an argument and returns a number representing the date entered.

function calc_day_number: which takes as a parameter the number calculated in calc_date_number, and returns the number (0 - 6) representing the day of the week (Sunday - Saturday).

function display_day_of_week: which takes as a parameter the N from the prior function. You must then take this numeric representation of the day of the week, and use it as an index into a character string array, to get the English representation of the day of the week. This can be done in function main().

Hint:

Since we really haven't covered pointers to date, you can use the following variable declaration to solve this problem. Notice the character string array called days. char *days[7] = {"Sunday', "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

To use this in your program, simply use the days array to output your day of the week. For example, if I wanted to output: Today is Monday, the printf statement would look like:

printf ("Today is %s ", days[1]);

Assume the date entered by the user follows a particular valid format. That is, assume the user enters an integer (month) followed by a slash (/), another integer (day) followed by a slash (/), and another integer (year). (The input reference for this is in chapter 15.)

However, your program should ensure that the month entered is between 1 and 12, the day entered is between 1 and 31, and the year entered is greater than 1900, and that the actual day exists in history taking into account leap years etc.

If the date is invalid, output error message and re-prompt, using the following guidelines:

If the month is invalid, output the following message: Invalid month. Please re-enter date.

If the day is invalid, output the following message: Invalid day. Please re-enter date.

If the year is invalid, output the following message: Invalid year. Please re-enter date.

Other than the above requirements, you can be as creative as youd like.

The dialog with the user at a minimum should be as follows in this sample run below:

Welcome to the Date to Day-of-Week program.

The program will give the day of the week for any date from 1/1/1900

Enter date (mm/dd/yyyy): 20/9/2015

Invalid month. Please re-enter date.

Enter date (mm/dd/yyyy): 3/40/2015

Invalid day. Please re-enter date.

Enter date (mm/dd/yyyy): 10/15/1800

Invalid year. Please re-enter date. Year must be greater than 1900.

Enter date (mm/dd/yyyy): 10/15/2015

10/15/2015 falls on a Thursday.

Thank you for using the Date to Day-of-Week program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

=+10. Explain the two main categories of mobile analytics.

Answered: 1 week ago

Question

Which is not a common data type? Character Text Integer Date Video

Answered: 1 week ago

Question

What is human nature?

Answered: 1 week ago