Well, this is for the same assignment as my last post. It's just a simple implementation of a bunch of Algorithm functions. It throws a runtime exception at the end saying the stack around the b variable was corrupted! I commented out all the lines with that use the b array, and it throws the same exception but for the 'a' variable. What am I doing wrong?!

#include "Drawer.h"
#include <iostream>
using namespace std;
#include <Algorithm>

class CTest
{
	int _iHeight;
	COLORREF _Color;
public:
	CTest(int iH = 0, COLORREF Col = RGB(rand() % 255, rand() % 255, rand() % 255)):_iHeight(iH), _Color(Col) {}
	//accessors
	int GetY()const {return _iHeight;}
	COLORREF GetColor()const {return _Color;}
	//mutators
	void Set()
	{
		_Color = RGB(255, 0, 0);
	}
	bool operator<(CTest const & RHS) const
	{
		return _iHeight < RHS._iHeight;
	}
	bool operator==(CTest const & RHS)const
	{
		return _iHeight == RHS._iHeight;
	}

	~CTest(void);
};

CDrawer gCanvas(RGB(0,0,0));
void Draw(CTest [], int);
bool myCountIf(CTest const &);
bool myCountIf2(CTest const &);

CTest myTransform( CTest const & trans) // double and return the value
{ return CTest(trans.GetY() / 2); }

// ICA  19 ** Fill in the required blocks, labels and pauses are supplied
int main( void )
{
  CTest a[150];
  CTest b[150];
  CTest c[150];

  for (int i(0); i < 150; ++i)
  {
	  a[i] = CTest(300);
	  b[i]= CTest((i + 1) * 4);
	  c[i] =CTest(rand() % 600);
  }

  cout << "a - Fixed 300\n";
  Draw( a, 150 );
  cin.get();
  cout << "b - Increment\n";
  Draw( b, 150 );
  cin.get();
  cout << "c - Random\n";
  Draw( c, 150 );
  cin.get();

// Min and Max of c here
  cout << "Min of c : " << min_element(c, c + 150)->GetY() << endl;
  cout << "Max of c : " << max_element(c, c + 150)->GetY() << endl;
  cin.get();

// Count of 200, Count > 300 here
  cout << "Count of b == 200 : " << count_if(b, b + 150, myCountIf) << endl;
  cout << "Count of b > 300 : " << count_if(b, b + 150, myCountIf2) << endl;
  cin.get();

// Find and Set() 200 in b
  cout << "Found 200 in b, setting to RED\n";
  find_if(b, b + 150, myCountIf)->Set();
  Draw( b, 150 );
  cin.get();

// Find and Set() all > 300 in c here
  cout << "Found > 300 in c, setting to RED\n";
  CTest * lp = c - 1; //last found pointer (start at 1 before c)
  //this loop is a bit confusing... did i do this right?
  while (lp < c + 150)
	  (lp = find_if(++lp, c + 150, myCountIf2))->Set();
  Draw( c, 150 );
  cin.get();

// Shuffle b here
  cout << "Shuffled b\n";
  random_shuffle(b, b + 150);
  Draw( b, 150 );
  cin.get();

// Transform b here
  cout << "Transformed b in Half\n";
  transform(b, b + 150, b, myTransform);
  Draw( b, 150 );
  cin.get();

// Sort c here
  cout << "Sorted c\n";
  sort(c, c + 150);
  Draw( c, 150 );
  cin.get();

// Display common elements of re-generated a and b here
  sort(a, a+150);
  //sort(b, b + 150);
  cout << "Intersection of a and b into c\n";
  set_intersection(a, a+150, b, b+150, c );
  Draw( c, 150 );
  cin.get();
	return 0;
}



void Draw(CTest Array[], int iCount)
{
	gCanvas.Clear();
	for (int i(0); i < iCount; ++i)
		gCanvas.AddLine(i * 5, 600, i * 5, 600 - Array[i].GetY(),5, Array[i].GetColor());
	gCanvas.Render();
}

bool myCountIf(CTest const & iVal)
{
	return iVal == CTest(200);
}
bool myCountIf2(CTest const & iVal)
{
	return !(iVal < CTest(300));
}

bah nvm i fixed it

If possible, please post the solution.

This part needed a fixin.

CTest * lp = c - 1; //last found pointer (start at 1 before c)
  //this loop is a bit confusing... did i do this right?
  while (lp < c + 150)
	  (lp = find_if(++lp, c + 150, myCountIf2))->Set();

Im not too sure what was wrong with my solution, but on a different post, Narue recommended using this. And it works fine now :P . My guess is that i was accessing a point one before or after the array bound by c without an index, so it didn't cause a problem then, but later it did (probably on the dtor call?).

//...

// Find and Set() all > 300 in c here
  cout << "Found > 300 in c, setting to RED\n";
  CTest * lp = c - 1; //last found pointer (start at 1 before c)
  //this loop is a bit confusing... did i do this right?
CTest *it = c;
CTest *end = c + 150;
while ( ( it = find_if ( it, end, myCountIf2 ) ) != end ) {
it->Set();
++it;
}
 	
//...
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.