0

Let's assume I have an array of years.

int year[20];

The plan is to make an if condition and work with a segment of the months in the year vector.

The segment I want to work with is from September to February. Therefore the range of year I always want to pick is (year >=9 && year <3)

Therefore I have two vectors. One is for the year, and another one is for that I count to count the entry.

For example:

vector <int> year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
vector <int> list{1,2,3,2,1,1,2,3,2,1,5,3,2,5,6,5,3,2,5,6};

the size of both the above vectors is 20. What I want to do is to count the vector entry from list in each year segment I'm looking into.

If we look at both vectors above, the first month segment in year vector would be: from year[0] =1 to year[1] =2. and the count is 2.

The next segment would be: from year[4] =10 to year[5] =2. So the print out entries would be from the vector list: list[4]=1, and list[5] =1. and the total count is 2.

After the segment work is done I want to reassign the count to zero to start it again and iterate through out the whole vector of year.

Here is the some work I did. My problem is that, I can make the segment in the if statement, but I'm trying to assign the count =0 each time each year segment is complete.

#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;
int main()
{
    unsigned int count =0;
     vector <int> year{1,2,3,5,10,2,10,12,11,12,2,2,3,5,8,2,8,12,8,12};
    vector <int> list;

    for (int x =0; x<20; x++)
    {
        //cout << "random number" << (1+ rand()%12)<< endl;
        list.push_back((1+ rand()%4));
    }

    for (int j=0; j<=20; j++)
    {
        if (year[j] >=9 && year[j]<3)
        {
            count++;
            cout <<list[j]<< "counting the entry of each segment"<< count<<endl;
        }
    }
}

The way I want to select the segment if the entries in the vector year for example, 1= January, 2 = February satisfies the September to February conditions. So each segment would be greater or equal to 9 and less than 3.
As I increment to the vector year, I find a segment that satisfies the condition and goes to the next segment that satisfies the condition again.

Each time I' done with the operation in if condition with each segment, I want to assign the count =0, so I can recount how many entries in the next segment.

Since the month goes like in ascending order, from the example {11, 10} will count as 1 with two segments instead of two. The segments will be {11}, and {10}, instead of {11, 10} where it counts the elements entry =2.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
zero_field
  • 335
  • 1
  • 11
  • This looks like a condition which will ALWAYS be false: `(year >=9 && year <3)`. Same for `(year[j] >=9 && year[j]<3)`. I don't completly get your question yet, but you probably want `(year[j] >=9 || year[j]<3)`. – Yunnosch Jan 03 '20 at 06:03
  • 1
    The first range of numbers which matches (my understanding of) your condition would be [0] to [1], both are smaller than 3. Why do you skip them? – Yunnosch Jan 03 '20 at 06:14
  • That's correct. I should consider them. I'm going to edit the question accordingly. thanks. – zero_field Jan 03 '20 at 06:17
  • I think you might like looking at `(year[j]+9)%12`, compare it to be greater or equal 6 and also higher than previous index. That shifts the interval to start at 0 with the first month you want and allows to avoid the awquard conditions at January. You can detect the next segment (i.e. reset the counter) by the increasing check, so that e.g. 10, 2, 10, 12 is detected as two segments, instead of one. – Yunnosch Jan 03 '20 at 06:30
  • you can't name a variable `list` because it's already used in `std::list`. That's one of the reasons why [“using namespace std;” is considered a bad practice](https://stackoverflow.com/q/1452721/995714) – phuclv Jan 03 '20 at 07:10

1 Answers1

2

I propose to look at (year[j]+9)%12 instead of directly at year[j].
Everything >= 6 is in your range (6,7,8,9,10,11) , i.e. the unwanted other 6 months (0,1,2,3,4,5) are outside. That also makes detecting the start of a new segment much easier.

year[j]  (year[j]+9)%12    wanted
 1       10                 x
 2       11                 x
 3        0
 4        1
 5        2
 6        3
 7        4
 8        5
 9        6                 x
10        7                 x
11        8                 x
12        9                 x

The values in your vector become:

          wanted   reset
 1  10    x        
 2  11    x
 3   0             r
 5   2
10   7    x    
 2  11    x
10   7    x        r
12   9    x
11   8    x        r
12   9    x
 2  11    x
 2  11    x
 3   0             r
 5   2
 8   5
 2  10    x
 8   5             r
12   9    x
 8   5             r
12   9    x

This shows where you want to reset the count and start working on a new segment, which is whenever (year[j]+9)%12 is not greater than at previous index.

The formula (year[j]+9)%12 works by shifting the "virtual month" you are looking at, by using the +9 part and making sure that the result still is in the 0-11 range for twelve months, by the %12 part. I.e. that "1" (seen as "10") is seen higher than "12" (seen as "9"). So that going from 12 (9) to 1 (10) does not trigger the reset, but going from 2 (11) to 3 (0) does trigger the reset, because the segment obviously ended. Also going from , 2 (11) to 10 (7), triggers the reset; which is needed, because (3-8) occur in between and hence a new segment needs to be started, even though 10 is higher than 2.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54