Question

C Programming Quesition (Structs in C): Write a C program that prompts the user for a...

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\n", 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 you’d 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source code:

#include<stdio.h>
long int calc_date_number(int date,int month,int year);
int main(){
   int flag = 1;
   int date,month,year;
   //runs till no error in the code occurs
   while(flag){
       flag =0;
       printf("Enter month/date/year in this format : ");
       scanf("%d/%d/%d",&month,&date,&year);
       if(month>12){
           printf("Invalid month, Please re enter date.\nmonth must be less than 12 .\n");
           flag=1;
       }
       if(year<=1900){
           printf("Invalid year, Please re enter date.\nYear must be greater than 1900.\n");
           flag=1;
       }
   }
   //function calls to claculate individual values
   long int N = calc_date_number(date,month,year);
   int day_number = calc_day_number(N);
   display_day_of_week(day_number);
   printf("\nThank Your for using Date to Day_of_week program.\n");
   return 0;
}
//function which returnss long value
long int calc_date_number(int date,int month,int year){
       int year_month_fvalue = year;
       int month_gvalue = month+1;
       if(month<=2){
           year_month_fvalue = year-1;
           month_gvalue = month+13;
       }
       long int N = ((1461*year_month_fvalue)/4)+153*((month_gvalue)/5)+date;
       return N;
}
//fucntion to find out day
int calc_day_number(long int N){
   return ((N-621049)%7);
}
//printing dat of the week using the day_number
void display_day_of_week(int N){
   char days[7][20] = {"Sunday","Monday","Tuesday","Wednesday","Friday","Saturday"};
   printf("You were born on %s",days[N]);
}

OUTPUT:

CProgram Files (x86) Dev-CpplConsolePauser.exe Enter month/date/year in this format 07/21/1996 lou were born on Sunday Thank

Add a comment
Know the answer?
Add Answer to:
C Programming Quesition (Structs in C): Write a C program that prompts the user for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program...

    Write a program that prompts the user to enter the date in mm/dd/yyyy format. The program should use a single input statement to accept the date, storing each piece of the date into an appropriate variable. Demonstrate that the input was obtained correctly by outputting the month, day, and year to the screen. Sample output: Enter (date (mm/dd/yyyy): 02/08/2011 Month entered: 2 Day entered: 8 Year entered: 2011 Challenge: Although the user entered 02, C++ drops the 0 as insignificant...

  • Write a program that prompts the user to enter a person's birth date in numeric form...

    Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...

  • Write a C++ program that determines the user's age after the   user enters both the current...

    Write a C++ program that determines the user's age after the   user enters both the current date and hisher birthdate as 3   integers:                yyyy mm dd                                                                                             Define a class which is named DateType and will be used to     declare 2 objects: "birthday" and "today". The class will have a function "output" which will display the date in conventional form (like it is above next to Date Assigned: Month dd, yyyy and it will have a second function named     “input”...

  • Write a program named CheckMonth2 that prompts a user to enter a birth month and day....

    Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...

  • Write a program that accepts a date from the user in the form mm/dd/yyyy

    # C PROGRAMMING LANGUAGEWrite a program that accepts a date from the user in the form mm/dd/yyyy and then dis- plays it in the form month dd, yyyy, where month is the name of the month: Enter a date (mm/dd/yyyy): 2/17/2011 You entered the date February 17, 2011 Store the month names in an array that contains pointers to strings.

  • C Program Question 1: Write a program that reads a date from the keyboard and tests...

    C Program Question 1: Write a program that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • Question 1: Write a program in C that reads a date from the keyboard and tests...

    Question 1: Write a program in C that reads a date from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid. The input date will have the format: mm/dd/yyyy Note that there is no space in the above format. A date in this format must be entered in one line. A valid...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and...

    anyString.substr(x, n) - Returns a copy of a substring. The substring is n characters long and begins at position x of anyString. Write a program that reads a string from the user containing a date in the format mm/dd/yyyy. You have to use above substring method to extract the various fields from the format. It should print the date in the form Month Date, Year. Validate:  Exit the program with error message as “Invalid date format” if length of...

  • Write program that prompts the user to enter a person's date of birth in numeric form...

    Write program that prompts the user to enter a person's date of birth in numeric form such as 8-27-1980. The program then outputs the date of birth in the form: August 27, 1980. Your program must contain at least two exception classes: invalidDay and invalidMonth. If the user enters an invalid value for day, then the program should throw and catch an invalidDay object. Similar conventions for the invalid values of month and year. (Note that your program must handle...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT