Question

In: Computer Science

C Question! Problem: Given a day of the week (1-7 corresponding to Sunday through Saturday), the...

C Question!

Problem: Given a day of the week (1-7 corresponding to Sunday through Saturday), the month number (1-12), and the year, determine whether that given month has five occurrences of the day of the week and display those dates.

Example Execution #1:

Enter day of week (1-7) -> 4

Enter month of the year -> 10

Enter the year -> 2019

Finding: There exists five Wednesday dates in October of 2019.

Dates: 2 9 16 23 30

Example Execution #2:

Enter day of week (1-7) -> 2

Enter month of the year -> 10

Enter the year -> 2019

Finding: There are not five Monday dates in October of 2019.

Solutions

Expert Solution

The C code is given below:

#include <stdio.h>

int main() {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char* dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
char* monthName[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int dayOfWeek, month, year, i;
printf("Enter day of week (1-7) -> ");
scanf("%d", &dayOfWeek);
printf("Enter month of the year -> ");
scanf("%d", &month);
printf("Enter the year -> ");
scanf("%d", &year);
/* if the given year is leap then change the number of days of February to 29. */
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
monthDays[1] = 29;
int date_of_month = 1; // we will find the day of 1st date of the given month.
int days=1;
/* count total number of days from 1 january 1900 to 1st date of the given month and year */
for (i = 1900; i < year; i++) // according to Gregorian calendar 1 january 1900 is Monday.
{
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) // add 366 when year is leap
days += 366;
else // otherwise add 365
days += 365;
}
for (i = 0; i < month - 1; i++) // add number of days upto the given month of the given year.
days += monthDays[i];
int weekDay = days%7 + 1; // find week day
while(weekDay != dayOfWeek) // find date of the first day of month which is given
{
date_of_month++;
weekDay = weekDay%7 + 1;
}
int count = 0;
int temp = date_of_month;
while(date_of_month <= monthDays[month - 1]) // count number of that day in the month.
{
count++;
date_of_month += 7;
}
if(count == 5)
{
printf("Finding: There exists five %s dates in %s of %d.\n",dayName[dayOfWeek-1],monthName[month-1],year);
printf("Dates: ");
while(count--) // print all the 5 dates
{
printf("%d ",temp);
temp += 7;
}
}
else
printf("Finding: There are not five %s dates in %s of %d.\n",dayName[dayOfWeek-1],monthName[month-1],year);
return 0;
}

Sample Input and Output 1:
Enter day of week (1-7) -> 4
Enter month of the year -> 10
Enter the year -> 2019
Finding: There exists five Wednesday dates in October of 2019.
Dates: 2 9 16 23 30

Sample Input and Output 2:
Enter day of week (1-7) -> 2
Enter month of the year -> 10
Enter the year -> 2019
Finding: There are not five Monday dates in October of 2019.

Screenshot of the code is given below:

If the answer helped please upvote, it means a lot and for any query please comment.


Related Solutions

Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
Python Programming #1 Below is an English algorithm for determining the day of the week (Sunday,...
Python Programming #1 Below is an English algorithm for determining the day of the week (Sunday, Monday, etc.) given the date (day, month and year). Take the last two digits of the year. Divide by 4, discarding any fraction. (keep quotient; ignore remainder) Add the day of the month. Add the month's key value: JFM AMJ JAS OND 144 025 036 146 [eg, Jan = 1; Feb = 4; March =4; etc] & see note [a] Subtract 1 for January...
What day of the week is a given date? i.e. On what day of the week...
What day of the week is a given date? i.e. On what day of the week was 5 August 1967? if it is given that 1 January 1900 was a tuesday Write a function is leap() which takes 1 input argument, an integer representing a year, and returns True if the year was a leap year, and False if it was not. .Then, write a function days_since() which takes 3 input integers day, month, year, representing a date (after 1st...
Create a un-order list that displays each day of the week with corresponding office hours. Set...
Create a un-order list that displays each day of the week with corresponding office hours. Set M-F for 9-5pm, Sat 10-2pm, Sun -closed. Use 3 spaces between the day and time.      Example: Monday [3 spaces] 9am – 5pm
Question 1 (7 marks) (This question is from the Week 1 Tutorial) James, a sole trader,...
Question 1 (This question is from the Week 1 Tutorial) James, a sole trader, is considering registering his business as a company. James wants to establish limited liability for himself as a director, shareholder, and employee of the company. Advise James on the following questions a) and b) as stated below: a) Does the creation of a company absolutely remove him from any potential liability for injury, loss, or contracts made by his company? (3.5 marks) b) Would James need...
A barbershop has been using a level workforce of barbers five days per-week, Tuesday through Saturday....
A barbershop has been using a level workforce of barbers five days per-week, Tuesday through Saturday. The barbers have considerable idle time on Tuesday through Friday, with certain peak periods during the lunch hours and after 4 p.m. each day. On Friday afternoon and all-day Saturday, all the barbers are very busy, with customers waiting a substantial amount of time and some customers being turned away. Describe at least three options that this barbershop should consider for aggregate planning? How...
Question 1 Week 7 (7 marks) Jaguar Ltd purchased a machine on 1 July 2016 at...
Question 1 Week 7 Jaguar Ltd purchased a machine on 1 July 2016 at the cost of $640,000. The machine is expected to have a useful life of 5 years (straight-line basis) and no residual value. For taxation purposes, the ATO allows the company to depreciate the asset over 4 years. The profit before tax for the company for the year ending 30 June 2017 is $600,000. To calculate this profit the company has deducted $60,000 entertainment expense, and $80,000...
Question 1 (7 marks) (This question is from the Week 3 Tutorial) You have reviewed the...
Question 1 (This question is from the Week 3 Tutorial) You have reviewed the work performed by your assistant, Raymond Snow, on the audit of Tin Ltd for the year ended 30 June 20X8 and you have noted the following two independent matters: (i) In testing investments in listed securities, Raymond selected all shareholdings with a market value above $200,000 and checked them to the closing market value reported by the Australian Stock Exchange (ASX) to determine the net realisable...
Question 1 (7 Marks) [Note this question is from the Week 2 Tutorial] The Tesco case...
Question 1 [Note this question is from the Week 2 Tutorial] The Tesco case was discussed in the Week 2 Interactive Tutorial. Companies like Tesco gather a massive amount of data through different loyalty card programs. Typically, the shoppers fill out an application form in the shop and receive a plastic card and a key fob* in the mail that is scanned before they make a purchase. In this way, the companies gather massive amounts of data about their customers’...
Given the following data, construct a three-sigma range control chart. Day of Sample Sample Values Saturday...
Given the following data, construct a three-sigma range control chart. Day of Sample Sample Values Saturday 22,19,20 Sunday 21,20,17 Monday 16,17,18 Tuesday 20,16,21 Wednesday 23,20,20 Thursday 19,16,21 a. If Friday's results are 15,14,21 is the process in control? b.Construct a three-sigma means control chart and determine if the process is still in control on Friday.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT