summation

Hello all

I need help with the code below

string v;
for(int i = 20; i <= 22; i++)
{
for(int j = i; j <= 1992; j++)
{
s.insert(v);
}
}

characters starting from 8th of v till the end of v are always a "number" (if converted to int). I want to add the "number" of previous iteration to the "number" of the current iteration.

Can anyone please help

Thanks
Last edited on
int sum = 0;
for(int j = i; j <= 1992; j++)
{
if ( 6 < j ) sum += std::stoi( value11 );
setStr11.insert(value11);
}
vlad from moscow (1472)

Could you please explain to me the code below and especially this line ( 6 < j )

int sum = 0;
for(int j = i; j <= 1992; j++)
{
if ( 6 < j )
sum += std::stoi( v);

s.insert(v);
}

Thanks
Last edited on
I did not understand your original task. It is the result that you are unable clear to describe your problem. So the code I showed early is not what you need. Now I have rewritten it the following way

1
2
3
4
5
6
7
int sum = 0;
for(int j = i; j <= 1992; j++)
{
     sum += std::stoi( value11.substr( 7 ) );
     
     setStr11.insert(value11); 
}


Or maybe you need the following

1
2
3
4
5
6
7
int sum = 0;
for(int j = i; j <= 1992; j++)
{
     sum += std::stoi( value11.substr( 7 ) );
     
     setStr11.insert(  sum ); 
}
Thanks it is working
Last edited on
Hello Vlad

int sum = 0;
for (it= s.begin(); it!= s.end(); ++it)
{
sum += std::atoi( ((*it).substr( 8 )).c_str() );
cout << (*it).substr( 0,7 ) << " : " << sum << endl;
}


The first 8 characters of the string type elements of the set represent two concatenated yrs:

Could you please help me do the summation.

Thanks[/code]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
setStr11.insert("19921992110.0");
setStr11.insert("19921993170.0");
setStr11.insert("19931993200.0");

int sum = 0;
std::string s;
for (iter11 = setStr11.begin(); iter11 != setStr11.end(); ++iter11)
{
    if ( s != iter1->substr( 0, 4 ) ) { s = iter1->substr( 0, 4 ); sum = 0; }
    sum += std::atoi( ((*iter11).substr( 8 )).c_str() );
    cout << (*iter11).substr( 0,7 ) << " : " << sum << endl;
}
Last edited on

That’s perfect. Thanks. data read from a file:

A,yryrno
A,yryrno
A,yryrno
A,yryrno

B,yryrno
B,yryrno
B,yryrno

Could you please add to the solution provided by yourself that these yryrno were against A and these yryrno were against B.
Thanks again
Last edited on
The code could look something as below. But I did not test it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
iter11 = setStr11.begin(); 

while ( iter1 != setStr11.end() )
{
   std::string s = iter1->substr( 0, 4 );
   std::cout << s << ' ';
   std::string s2 = iter1->substr( 5, 4 );
   int sum = 0;
   do
   {
      if ( s2 != iter1->substr( 5, 4 ) ) 
      {
         std::cout << sum << ' ';             
         s2 = iter1->substr( 5, 4 ); 
         sum = 0; 
      }
      sum += std::atoi( (iter11->substr( 13, 8 )).c_str() );
   } while ( ++iter1 != setStr11.end() && s == iter1->substr( 0, 4 ) );

   std::cout << sum << std::endl;
}         
Thanks
Last edited on
Topic archived. No new replies allowed.