add by 4

Apr 30, 2023 at 6:18pm
Write your question here.

1
2
3
4
5
6
7
8
  int incrementer = 1;
for ( int i = 1; i < someLength; i += incrementer )
{
    cout << i << endl;
    ++incrementer;
}
//////_///_///////_/




looking for a way to increase i in a for loop by 4 up to 3000
Last edited on Apr 30, 2023 at 6:24pm
Apr 30, 2023 at 6:32pm
I've tried swapping out the 1s for 4s...doesn't work.
Apr 30, 2023 at 6:43pm
You're close, your mistake was that you incremented the incrementer in the for loop

1
2
3
4
5
6
7
8
9
10
constexpr int incrementer = 4;
constexpr int someLength = 3000;

for (int i = 1; i < someLength; i += incrementer )
{
    cout << i << endl;

    // Don't do this!
    // ++incrementer;
}
Last edited on Apr 30, 2023 at 6:44pm
Apr 30, 2023 at 7:30pm
if you want 4, 8, 12 ... then start at 4 (or maybe 0?) on line 4: for (int i = 4... )
you are gonna get 5, 9, ... as-is.
May 1, 2023 at 12:24am
Okay, thank you. It works, Malibor...now I need to use i to recognize when a year(starting with 2020) is a multiple of 4. This is what I have...I'm definitely missing something.

if (year == 2020 || year == 2020 + i){
days_per_year = 366;
}
else {
days_per_year = 365;
}
cout << days_per_year;


as you can see, I'm hunting leap years
May 1, 2023 at 2:52am
1
2
3
4
5
6
7
8
9
// Gregorian calender; extra leap day occurs in each year that is 
// an integer multiple of 4 (except for years evenly divisible by 100, but not by 400)
// https://en.wikipedia.org/wiki/Leap_year
bool is_leap( unsigned int year )
{
    if( year%400 == 0 ) return true ; // leap year if multiple of 400
    else if( year%100 == 0 ) return false ; // not leap year if multiple of 100, but not multiple of 400
    else return ( year%4 == 0 ) ; // else leap year if multiple of 4
}
May 1, 2023 at 9:02am
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
	for (unsigned y { 2020 }; y <= 3000; y += 4)
		if ((y % 100) || (y % 400 == 0))
			std::cout << y << ' ';

	std::cout << '\n';
}

May 1, 2023 at 4:03pm
Thanks, seeplus...this works, but it only seems to spit out leap years. I also need it to print whether there are 365 or 366 days in a user inputted year. My code above accomplished that, it just can't find the leap years like yours can. Is there a way to marry the two?

Nevermind...I figured it out. Thanks for your help!
Last edited on May 1, 2023 at 4:18pm
May 1, 2023 at 4:19pm
You didn't say about user inputted year. You only mentioned years 2020 to 3000.

If you want to use it with a given year, then use JLBorges function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

bool is_leap(unsigned year) {
	if (year % 400 == 0) return true; // leap year if multiple of 400
	else if (year % 100 == 0) return false; // not leap year if multiple of 100, but not multiple of 400
	else return (year % 4 == 0); // else leap year if multiple of 4
}

int main() {
	unsigned year {};

	std::cout << "Enter a year: ";
	std::cin >> year;

	std::cout << year << " has " << 365 + is_leap(year) << " days\n";
}


May 2, 2023 at 8:43am
This std::chrono thingy is a bit weird.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

bool is_leap( unsigned y )
{
   return chrono::year(y).is_leap();
}

int main()
{
   for (int y = 1900; y <= 2100; y++ ) cout << y << '\t' << boolalpha << is_leap( y ) << '\n';
}


or, the hard way,

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

bool is_leap( unsigned y )
{
   using namespace std::chrono;
   return year_month_day_last(year(y),month_day_last(month(February))).day() == day(29);
}

int main()
{
   for (int y = 1900; y <= 2100; y++ ) cout << y << '\t' << boolalpha << is_leap( y ) << '\n';
}
Last edited on May 2, 2023 at 9:39am
Topic archived. No new replies allowed.