My calculations are incorrect in the sense that my program thinks, for example, that 2022 is a leap year and 2024 is not. How do I fix this, please?? I've tried changing the bool statement but nothing seems to work.
#include <iostream>
int leapYear(int year)
{
return (((year % 400) == 0) || ((year % 4 == 0)&& !(year % 100 == 0)));
}
// extracted
}
Try like this in the leap Year function
return (((year % 400) == 0) || ((year % 4 == 0)&& !(year % 100 == 0)));
I'd just answer the title
with c++20 you can use std::chrono to do it
std::chrono::year{y}.is_leap()
godbolt link
Related
I get the error : line 56: Mismatched input 'if' expecting 'end of line without line continuation'. on my code BUT when I delete some line of code under it the error disappear and the code compile.
Here the code with the error (line 56 is the 3rd line) :
SL = if long == 'long' and (close[1]-low[1])/low[1] > 0.012
Truncate(low[1], 2)
else if long == 'long' and (close[1]-low[1])/low[1] < 0.012
Truncate(low[1]-low[1]*0.017, 2)
else if long == 'long_renversement' and renversement_red_golong == 'yes'
Truncate(math.avg(low[1], close[1]), 2)
else if long == 'long_renversement' and renversement_green_golong == 'yes'
Truncate(math.avg(low[1], open[1]), 2)
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) > 0.012
Truncate(math.avg(close[1], open[1]), 2)
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) < 0.012
Truncate(open[1], 2)
else if short == 'short' and (high[1]-close[1])/close[1] > 0.012
Truncate(high[1], 2)
else if short == 'short' and (high[1]-close[1])/close[1] < 0.012
Truncate(high[1]+high[1]*0.017, 2)
else if short == 'short_renversement' and renversement_red_goshort == 'yes'
Truncate(math.avg(high[1], open[1]), 2)
else if short == 'short_renversement' and renversement_green_goshort == 'yes'
Truncate(math.avg(high[1], close[1]), 2)
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] > 0.012
Truncate(math.avg(close[1], open[1]), 2)
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] < 0.012
Truncate(open[1], 2)
And here the code when i delete the end of it (run without any error) :
SL = if long == 'long' and (close[1]-low[1])/low[1] > 0.012
Truncate(low[1], 2)
else if long == 'long' and (close[1]-low[1])/low[1] < 0.012
Truncate(low[1]-low[1]*0.017, 2)
else if long == 'long_renversement' and renversement_red_golong == 'yes'
Truncate(math.avg(low[1], close[1]), 2)
else if long == 'long_renversement' and renversement_green_golong == 'yes'
Truncate(math.avg(low[1], open[1]), 2)
If I delete less than that, the error stay there... I'm so confused...
The compiler error is not helpful, but the issue is that you have several misplaced brackets in your if/else block (which is counted as a single block, which is why the compiler sends you to the line where the block starts instead of the actual line with the issue):
// Five opening brackets, three closing brackets
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) > 0.012
<...>
else if long == 'long_continuation' and (close[1]-(math.avg(close[1], open[1]))/(math.avg(close[1], open[1]) < 0.012
// Three opening brackets, two closing brackets
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] > 0.012
<...>
else if short == 'short_continuation' and ((math.avg(close[1], open[1])-close[1])/close[1] < 0.012
Fix these and the code should compile properly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Title. I need to know what !((n % 5 != 0) || (n % 20 == 0)) transforms into and why. I say transforms because it has the ! in the beginning.
I tried transforming it to ((n%5==0) || (n%20==0)) but I am pretty sure this is not the right answer.
Thanks!!
Negation is harder than it looks.
"A or B" is true if at least one of A and B is true.
Thus its negation, "not (A or B)", must be true if neither A nor B is true, which is the same as both A and B being false.
That is, the negation is equivalent to "(not A) and (not B)".
And that leads you to !(n % 5 != 0) && !(n % 20 == 0), or (n % 5 == 0) && (n % 20 != 0).
This is one of DeMorgan's laws, which you can memorise, but they are not diffult to "discover" for yourself, and you just need to remember to "invert" the operation as well as the operands.
Assuming you mean to use DeMorgan's Law, you can distribute the NOT into the expressions by NOT'ing each expression and flipping OR's to AND's (and vice versa).
So
!((n % 5 != 0) || (n % 20 == 0))
Can become
(!(n % 5 != 0) && !(n % 20 == 0))
Which can become
((n % 5 == 0) && (n % 20 != 0))
Original: !((n % 5 != 0) || (n % 20 == 0))
Applying De Morgan's laws: (!(n % 5 != 0) && !(n % 20 == 0))
Making it clearer (assuming n is something like int and operators are not overloaded):
((n % 5 == 0) && (n % 20 != 0))
Now you have the result.
bool leap = false;
int leapd = 28;
if (year % 100 == 0) {
if (year % 400 == 0) {
leap = true;
}
else { leap = false; }
}
else if (year % 4 == 0) {
leap = true;
}
else { leap = false; }
if (leap = true) {
leapd++;
}
This program decides if the year entered is a leap year or not. I'm not sure why, but the program will always set leap to true in the end.
If the year is divisible by 100, than it is a leap year if it also is divisible by 400.
If it is not divisible by 100, it is only a leap year if you can divide the year by 4.
It should be if (leap == true) for your last if statement. If you fix that, it should work. = is the assignment variable. So in your last if statement, instead of comparing leap and true using == you are assigning leap to true using =.
So your final if statement should actually be:
if (leap == true)
{
leapd++;
}
The last loop should look like
if (leap == true)
{
leapd++;
}
The way you currently have it will set leap to True and then evaluate the if. Which will always be True. The condition check should be ==.
Apologies, a bit of a beginner here:
I'm working on a practice program to determine cost of a long distance call based on hour of the day and day of the week...and I've gotten to my do-while loop and am attempting to use nested branches to split up weekdays with the weekend.
However, when I compile and run, both the if and else if nested statements are skipped no matter if I put in the Chars corresponding to the booleans I'm attempting to evaluate. I'm struggling to understand what I'm missing here. The instructions clearly state that the days of the week should be stored in two char variables: Mo Tu We Th Fr Sa Su.
do
{
//have user input day of week of call
printf("\nOn what day was the call made? (Mo, Tu, We, Th, Fr, Sa, or Su) ");
scanf("%c%c", &day1, &day2);
//branch for weekday vs weekend vs invalid input
if (((day1 == 'M') && (day2 == 'o')) || ((day1 == 'T') && (day2 == 'u')) || ((day1 == 'W') && (day2 == 'e')) || ((day1 == 'T') && (day2 == 'h')) || ((day1 == 'F') && (day2 == 'r')))
{
//determine if phone call was made at hi or low rate times
printf("At what time was your call made? (HH MM - with 08 00 representing 8:00 AM and 18 30 representing 6:30 PM) ");
scanf("%f %f", &call_time_hour, &call_time_minute);
call_time_hour = call_time_hour + (call_time_minute / 60);
printf("%f", call_time_hour);
}
else if (((day1 == 'S') && (day2 == 'a')) || ((day1 == 'S') && (day2 == 'u')))
{
printf("What was the duration of your call? ");
scanf("%d", &call_duration);
//calculate total cost of call
cost_of_call = call_duration * 0.15;
printf("%s %.2lf", "The cost of this call was $", cost_of_call);
}
calls_made--;
printf("%d", calls_made);
}while (calls_made > 0);
So, for example, when I compile and run the program and enter "Sa" as the day that the call was made, it then moved directly to the 'calls_made--' step.
The problem is because previous scanf. Debug and look what characters are really scanned. I'm sure that day1 would be '\n'.
In this case clear the input stream before scanning the day of week:
while (getchar() != '\n');
I am new in programming and I need a little help at a problem in C++ .
The problem is :
I need to read 3 numbers and to determinate if this numbers can be a date or not . I need to say "YES" if the numbers can be a date or "NOT" if they can`t be a date.
I've tried this :
#include <iostream>
using namespace std;
int main(){
unsigned int z, l, a;
cin >> z >> l >> a;
if((z<32 && l==1) || (z==29 && l==2 && a%4==0) ||
(z<29 && l==2 && a%4>0) ||(z<32 && l==3) ||
(z<31 && l==4) || (z<32 && l==5) || (z<31 && l==6) ||
(z<32 && l==7) || (z<31 && l==8) || (z<32 && l==9) ||
(z<31 && l==10) || (z<31 && l==1) || (z<31 && l==12)) cout << "YES";
else cout << "NO";
return 0;
}
Question:
Could you help me find the missed cases?
Note:
My teacher commented that "It is almost done but you miss some cases". I tried to find this cases 2 hours but I didn't succeed ...
Quick answer:
The missed cases probably are due to the missed leap years as your code currently doesn't use correct checking for that. The right check for leap year is at the end of the answer.
Firstly, try and figure out what are the cases on a piece of paper:
date is consisted of positive numbers
start date of Gregorian calendar (add comment to inform the user for interval of valid dates)
Formulate the format, e.g. it could be: dd/mm/yyyy or mm/dd/yyyy, i.e. 1.03.2015
which months have 30, 31, 28, 29 (in which years) days
leap years and February
Regarding the code:
1.Use meaningful variables that have names that explain their purpose, i.e.:
replace unsigned int z, l, a; with variables like: int month, day, year;
2.Create separate if- else if statements for each of the above cases and add comments to indicate them (it will make your code easy to read and understand).
// check if January has 31 days
if(z<32 && l==1){
// check if February has 29 days and it isn't a leap year
} else if (z==29 && l==2 && a%4==0){
} //...
Bugs in your code:
1.Your current check for leap year: a % 4 == 0 is not entirely correct. A proper check for leap year looks something like:
if year modulo 400 is 0 then
is_leap_year
else if year modulo 100 is 0 then
not_leap_year
else if year modulo 4 is 0 then
is_leap_year
else
not_leap_year
and in code:
if(((year % 4) == 0) && (((year % 100)!=0) || ((year % 400) == 0)){
//leap year
}
2.The check for November is not right: (z < 31 && l == 1). It should be:
(z < 31 && l == 11)