Question
I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!
Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds t
int getMonthValue (int month, int year): This function should return a value based isLeapYear function: on the table below an
Write a program that asks the user to enter a date (e.g. July 4, 2008) and will return the day of the week that corresponds to that date. Your program should take the input in numeric form, thus the input prompt should be as follows: Please enter a date below: Enter month (1-12): Enter day (1-31) Enter year (0000-9999): The following algorithm is from http://en.wikipedia.org/wiki/Calculating the day of the _week The implementation will require several functions: bool isLeapYear (int year)i This function should return true if year is a leap year and false if it is not. Here is pseudocode to determine a leap year: leap_year ((year divisible by 400) or (year divisible by 4 and year not divisible by 100) int getCenturyValue (int year): This function should take the first two digits of the year (i.e. the century), divide by 4, and save the remainder. Subtract the remainder from 3 and return this value multiplied by 2. For example, the year 2008 becomes: (20/4) -5 remainder 0. 3-0-3. Return 3 * 2 6. int getYearValue (int year) This function computes a value based on the years since the beginning of the century. First, ct the last two digits of the year. For example, 08 is extracted for 2008. Next, factor in leap years. Divide the value from the previous step by 4 and discard the remainder. Add the two results together and return this value. For example, from 2008 we extract 08. Then (8/4) 2 remainder 0. Return 2+8-10
int getMonthValue (int month, int year): This function should return a value based isLeapYear function: on the table below and will require invoking the Month Return Value 0 (6 if year is a leap year) 3 (2 if year is a leap year) anuary February3 March April May June 4 August September October November December Finally, to compute the day of the week, compute the sum of the date's day plus the values returned by getMonthValue, getYearValue, and getCenturyValue. Divide the sum by 7 and compute the remainder. A remainder of 0 corresponds to Sunday, 1 corresponds to Monday, etc. up to 6 which corresponds to Saturday. For example, the date July 4,2008 should be computed as: (day of month) + (getMonthValue) + (get Yearvalue) + (getCenturyvalue)-4 + 6 + 10 + 6 -26. 26/7 3 remainder 5. The fifth day of the week corresponds to Friday Your program should allow the user to enter any date (numerically) and output the corresponding day of the week in English. Bonus: Adjust your program so the user could re-enter a new date as many times as they wish without having to restart the program.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is mine code in C++:

#include <iostream>
#include <stdlib.h>
using namespace std;

bool checking_year(int year){

if ((year>999) && (year < 10000)){
return true;
}else{

return false;
}

}

bool checking_month(int month){
if ((month>0) && (month<13)){
return true;
}else{
return false;
}
}

bool checking_date(int day, int month){
if ((month==2) && (day>0) && (day<30)){
return true;
}
if ((
(month==1) ||
(month==3) ||
(month==5) ||
(month==7) ||
(month==8) ||
(month==10)||
(month==12)
) && ((day>0) && (day<32)))
{
return true;
}
if ((
(month==4) ||
(month==6) ||
(month==9) ||
(month==11)
) && ((day>0) && (day<31)))
{
return true;
}
return false;
}

bool isLeapYear(int year){
if (((year % 4)==0) && ((year % 100)!=0)){
return true;
}else if ((year % 400)==0){
return true;
}else {
return false;
}

}

// http://en.wikipedia.org/wiki/Julian_day#Calculation
const char *get_day(int day, int month, int year){
int JMD;
JMD = (day + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) +
(365 * (year + 4800 - ((14 - month) / 12))) +
((year + 4800 - ((14 - month) / 12)) / 4) -
((year + 4800 - ((14 - month) / 12)) / 100) +
((year + 4800 - ((14 - month) / 12)) / 400) - 32045) % 7;
const char *weekday[] = {"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"};
return weekday[JMD];
}

int main(){

int year, month, date, x, y;
cout << "\nPlease Enter The Year [YYYY] : ";
cin >> year;

if (checking_year(year)){
cout << "\n\tYear Verified";
}else{
cout << "\n\t Wrong Year Format\n";
exit(0);
}


cout << "\nPlease Enter The Month [MM] : ";
cin >> month;

if (checking_month(month)){
cout << "\n\tMonth Verified";
}else{
cout << "\n\t Wrong Month Format";
exit(0);
}
cout << "\nPlease Enter The Date [DD] : ";
cin >> date;

if (checking_date(date, month)){
cout << "\n\tDate Verified\n\n";
}else{
cout << "\n\t Wrong Date Format";
exit(0);
}
cout << "Date : " << date << ", Month : " << month << ", Year : " << year << " [ ";
if (isLeapYear(year)){
cout << "Leap Year" << " ]";
}else{
cout << "Not Leap Year" << " ]";
}
cout << "\n\tWeekday : ";
cout << get_day(date, month, year);
cout << "\n\n";

return 0;
}

Output:-

Please Enter The Year [YYYY] : 2019 Year Verified Please Enter The Month [MM]: 3 Month Verified Please Enter The Date [DD]: 2

Here is my code for Swift 3 and later:-

import Foundation
import Glibc
 
func gettingDayOfWeek(_ today:String) -> Int? {
    let formatter  = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"
    guard let todayDate = formatter.date(from: today) else { return nil }
    let myCalendar = Calendar(identifier: .gregorian)
    let weekDay = myCalendar.component(.weekday, from: todayDate)
    return weekDay
}
if let weekday = gettingDayOfWeek("2019-03-17") {
    print(weekday)
} else {
    print("bad input")
}
Add a comment
Know the answer?
Add Answer to:
I need help with a project, I’ve seen many answers in C++, but i need it to be in swift, thank you!
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 C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:             0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • C++ code: Days in current month: Write a program that can determine the number of days...

    C++ code: Days in current month: Write a program that can determine the number of days in a month for a specified month and year. The program should allow a user to enter two integers responding a month and a year, and it should determine how many days are in the specified month. The integers 1 through 12 will be used to identify the months of January through December. The user indicates the end of days in the current month...

  • Has to be written in C++ Write a program that asks the user to enter the...

    Has to be written in C++ Write a program that asks the user to enter the month (1-12) and the year (0-2019). Validate the input and print an error and stop the program if an invalid month or year was entered. The program should then display the number of days in that month. Use the following criteria to identify leap years: Determine whether the year is divisible by 100. If it is, then it is a leap year if and...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes...

    A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are: 1) The year must be divisible by 4 2) If the year is a century year...

  • leap year C++

    the instructions are: A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To account for the difference in time, every 4 years, a leap year takes place. A leap year is when a year has 366 days: An extra day, February 29th. The requirements for a given year to be a leap year are:1) The year must be divisible by 42) If the year is a century year...

  • Must use a function to compute the day. All cin and cout statements must appear in...

    Must use a function to compute the day. All cin and cout statements must appear in main(). Has to be in c++. lab 10 Problem write a program that prints the day number of the year, given the date in the form month-day year. For example, if the input is 1-1-2006, the day number is 1; if the input is 12 the day number is 359. The program should check for a leap year. A year is a leap year...

  • Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for...

    Write a class named MonthDays. The class’s constructor should accept two arguments: l An integer for the month (1 = January, 2 February, etc). l An integer for the year The class should have a method named getNumberOfDays that returns the number of days in the specified month. The method should use the following criteria to identify leap years: 1. Determine whether the year is divisible by 100. If it is, then it is a leap year if and if...

  • Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java,...

    Description: This project focuses on creating a Java class, Date.java, along with a client class, DateCleint.java, which will contain code that utilizes your Date.java class. You will submit both files (each with appropriate commenting). A very similar project is described in the Programming Projects section at the end of chapter 8, which you may find helpful as reference. Use (your own) Java class file Date.java, and a companion DateClient.java file to perform the following:  Ask user to enter Today’s...

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