Hi all,
I was hoping for some help and hints to the problem i'm facing now.
Right now i have a a 2D array which stores the elements:

22Jan11 180
11Feb11 111
22Jan11 90
22Jan11 30
25Jan11 110

I'm trying to basically add up the the value of the duplicate records and store it in the text file which will then be:

22Jan11 300
11Feb11 111
25Jan11 110

I'm not sure as how to go about adding and replacing the records. I've tried the following nested for loop but it's not working properly

for( int x = 0; x < arraysize; x++)
    {
         for( int j = x+1; j < i; j++)
         {
              if( array2[x][0] == array2[j][0] )
              {
                  add1 = atoi(array2[x][1].c_str());
               
                  add2 = atoi(array2[j][1].c_str());
           
                  add3 = add1 + add2;
                  ss << add3 << endl;
                  myfile1<<array2[x][0]<<":"<< ss.str();
                  ss.str("");
                 }
              else
               ;
         }

Hoping for some hints or direction and hopefully I'm still able to use arrays to get it done. Thanks alot!

You're going to need either a new array to represent the combined values or you need to use the old array and delete some elements. You're sending data to the file too early. What if there are THREE records with the same date, not TWO.

Assuming you don't need to save the original array, I would use a struct instead of the 2-D string array you are using. Make it 1-D, not 2-D.

struct record
{
    string theDate;
    int value;
};

or

struct record
{
    time_t theDate; /* or use struct tm instead of time_t */
    int value;
};

Sort the array based on theDate. Now go through the sorted array. If two adjacent elements have the same value for theDate, combine them into one by adding the value parts and delete the second element. You can use an array, but consider using a vector.

By the end, you'll have everything combined, so traverse the array again and send it to the file.

I'd suggest you convert your current structure to a

std::map< std::string, int >

and use that to handle your duplicates. The logic would flow something like:

// pseudocode
void convert(array, map) {
   for name,value in array {
      map[name] += convert_to_int(value)
   }
}

You can then output to file by iterating over the new container

try the following:

std::map<string,int> dateValueMap;
for(int i=0; i<arraysize; i++)
{
   dateValueMap[array2[x][0]] += atoi(array2[x][1].c_str());
}

// output sorted by date (lexicographically)
for(std::map<string,int>::iterator mapIt = dateValueMap.begin();
    mapIt != dateValueMap.end();
    mapIt++)
{
    cout << "date=" << mapIt->first << " value=" << mapIt->second << endl;
}

I haven't debugged it and so it might be buggy, but you get the gist.

you could sort the the array by the date column (if you write an adequate comparison function/function object you can even use the std::sort - function) .
Then get the equal-ranges in a loop ( - also best achieved with a STL algrorithm std::equal_range). Then add the values in the second column (e.g. std::accumulate)
and then use remove/erase construct to get rid of unwanted copies. Well worth looking into this if you got some time, coz it'll teach you a lot about how the STL can help you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.