+ 2
Calendar
I asked this before but is there anyone can help me to code where the first day of any year given falls into days? Like if its saturday or monday. Something like that?
18 ответов
+ 3
Hi Jelly Baker Since this is a learning platform its best when you have some code that you have written but are uncertain about specific parts of it. If you can share your code, our community would be happy to lend a hand in guiding you through any confusing sections.
However, please keep in mind that it may require more time and effort to assist you from the beginning, which is beyond the scope of what this platform is intended for. In case you need inspiration or references, feel free to browse online and explore other resources. For instance, I came across a similar question on Stack Overflow: https://stackoverflow.com/questions/40517192/c-day-of-week-for-given-date
I believe you will find what you're looking for there.
If you have any more questions feel free ask.
+ 2
Where is your code??
+ 2
You can log into sololearn.com via your web browswer and use the compiler-playround to copy and paste your code. You can share the links when you create your question or use the (+) symbol to share your code.
https://www.sololearn.com/compiler-playground
+ 2
https://code.sololearn.com/cWXjrQ9jFqo7/?ref=app
It was my try to learn CPP concepts.
Hope it will answer your queries.
+ 2
Jelly Baker, JarinP
yep, my code seems to be having issues, maybe you guys can help me debug it? 😅
I suspect my leapyear calculation is off...
The cTime based one seems ok.
I think I pulled a defective formula from SO... the calculation can't be this simple...
--edit--
ok I updated the weekday algorithm
for all the codes I posted to use something called zellersAlgorithm.
Jan 01 2020 is a Wednesday now.
+ 1
Here is my code: I am breaking it into two parts since there are limited characters allowed here.
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void space(int i)
{
while (i > 0)
{
cout << " ";
i = i - 1;
}
}
void quitProgram()
{
cout << "\nInvalid input. Program Ending." << "\n";
exit(-1);
}
bool leap_year(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
int years() {
int _year;
cout << "Please enter the year you want your appointment: ";
cin >> _year;
if (_year < 2000 || _year >2100) {
cout << "Invalid output. Please choose between years 2000 - 2100. " << endl;
years();
}
return _year;
}
int firstDay(int year)
{
int start;
int x1, x2, x3;
x1 = (year - 1) / 4;
x2 = (year - 1) / 100;
x3 = (year - 1) / 400;
start = (year + x1 - x2 + x3) % 7;
return start;
}
int menu() {
int choice;
cout << "Please choose between the following: " << endl;
cout << "1.Pick a month." << "\n2.Show all appointments. " << endl;
cout << "3.Set up appointment." << "\n4.Quit the program." << endl;
cin >> choice;
if (choice < 1 || choice >4) {
cout << "Invalid output.";
menu();
}
return choice;
}
int pickAmonth() {
int month;
cout << "Pick a month you want your appointment as a number. i.e 1 is for January. " << endl;
cin >> month;
if (month < 1 || month >12) {
cout << "Invalid input. Please choose between 1-12, when 1 is for January and 12 is for December. " << endl;
pickAmonth();
}
return month;
}
int days_month(int m, bool leap)
{
if (m == 1) return(31);
else if (m == 2) if (leap) return(29); else return(28);
else if (m == 3) return(31);
else if (m == 4) return(30);
else if (m == 5) return(31);
else if (m == 6) return(30);
else if (m == 7) return(31);
else if (m == 8) return(31);
else if (m == 9) return(30);
else if (m == 10) return(31);
else if (m == 11) return(30);
else if (m == 12) return(31);
else quitProgram();
}
void print_month_name(int m)
{
if (m == 1)
{
space(7);
cout << "January" << "\n";
}
+ 1
Heres the second part.
else if (m == 2) { space(7); cout << "February" << "\n"; }
else if (m == 3) { space(7); cout << "March" << "\n"; }
else if (m == 4) { space(7); cout << "April" << "\n"; }
else if (m == 5) { space(7); cout << "May" << "\n"; }
else if (m == 6) { space(7); cout << "June" << "\n"; }
else if (m == 7) { space(7); cout << "July" << "\n"; }
else if (m == 8) { space(7); cout << "August" << "\n"; }
else if (m == 9) { space(7); cout << "September" << "\n"; }
else if (m == 10) { space(7); cout << "October" << "\n"; }
else if (m == 11) { space(7); cout << "November" << "\n"; }
else if (m == 12) { space(7); cout << "December" << "\n"; }
else quitProgram();
cout << " S M T W T F S" << "\n";
cout << "____________________" << "\n";
}
void print_month(int number_days, int& weekDay)
{
int day = 1;
while (day <= number_days)
{
cout << setw(2) << day << " ";
if (weekDay == 6)
{
cout << "\n";
weekDay = 0;
}
else weekDay = weekDay + 1;
day = day + 1;
}
}
int printDaysOfMonth(int nmDaysInMonth, int& startDay) {
int day = 1;
int i = 1;
while (i <= startDay) {
cout << " ";
i++;
}
while (day <= nmDaysInMonth) {
cout << setw(2) << day << " ";
if (startDay == 6) {
cout << endl;
startDay = 0;
day++;
}
else {
startDay++;
day++;
}
}
return startDay;
}
int main()
{
int year, number_days, current_month = 1, pickAmenu, pickMonth;
bool leap;
int start_day_month;
cout << "Enter the year : ";
cin >> year;
pickAmenu = menu();
pickMonth = pickAmonth();
start_day_month = getDayFirst(year);
leap = leap_year(year);
space(9);
cout << year << "\n";
while (current_month = pickMonth)
{
number_days = days_month(current_month, leap);
print_month_name(current_month);
print_month(number_days, start_day_month);
cout << "\n\n\n";
current_month;
break;
}
return 0;
+ 1
use the codeplayground
+ 1
Where do I locate it on my phone?
+ 1
I just got it. Here it is.
+ 1
Jelly Baker I modified your code
https://code.sololearn.com/cGvfbgrAR1lf/?ref=app
+ 1
Oh lol I probably looked at a different month. But the code did show sunday for January 1st of 2020.
0
Oh yeah sorry. I can post my code I just have to transfer it from my laptop. Since I’m using my phone for sololearn.
0
Here's an example in Python:
```python
import datetime
def get_first_day_of_year(year):
first_day = datetime.date(year, 1, 1)
return first_day.strftime("%A")
year = int(input("Enter the year: "))
first_day_of_year = get_first_day_of_year(year)
print("The first day of {year} is {first_day_of_year}.")
```
In this code, we use the `datetime` module in Python to handle date-related operations. The `get_first_day_of_year` function takes a year as input and returns the first day of that year as a string.
We use the `datetime.date` function to create a date object for the given year, with month set to 1 and day set to 1 (i.e., January 1st of the given year).
Then, we use the `strftime` method with the format code `%A` to retrieve the day of the week as a string.
Finally, we prompt the user to enter a year, calculate the first day of that year using the `get_first_day_of_year` function, and display the result.
0
Jelly Baker
here is are 2 simple day of the week codes, for years after 1900. One is for any date, the other for Jan 1
https://code.sololearn.com/c5j08OpczsS0/?ref=app
https://code.sololearn.com/c57BlCumuRt6/?ref=app
and my calendar code:
https://code.sololearn.com/ciHgtiFdI75z/?ref=app
0
Bob_Li Thank you so much for all the codes and taking time to modify mine. However, I ram the codes and the 1st day of january each year I put falls on a different day than what the code shows. The code shows that January 1st 2020 is a sunday but its actually saturday. Thank you.
0
Hi Jelly Baker , just to be clear 1st Jan of 2020 is Wednesday.
😅
this year's(2023) Jan 1st is Sunday.