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.