Hi!

Would you anybody please do check my code given below and say why I am getting Debug Assertion Failed.

I have used visual studio 6. But I think I have problems in my code. Please I do need urgent solution. Please help me.

#include <iostream.h>

int INITIAL = 6;//allocation size
int INCREMENT = 4;//if more allocation needed
int initialSize;//initial number of array elements

class Point
{
	int x;
	int y;

public:
	
	Point();
	Point ( int x, int y );
	~Point()
	{
		cout << "";
	}
	
	
	int get_x();
	int get_y();

	void set_x(int x);
	void set_y(int y);

	void print();
	
};

Point::Point()
{

}
Point::Point(int x, int y)
{
	this->x = x;
	this->y = y;
}

void Point::set_x(int x)
{
	this->x = x;
}

void Point::set_y(int y)
{
	this->y = y;
}

int Point::get_x()
{
	return x;
}

int Point::get_y()
{
	return y;
}

void Point::print()
{
	cout << " (" << get_x() << ", " << get_y() << ") " << endl;	
}


class Vector_
{
	Point *pArray;
	int size;

public:
	Vector_();
	Vector_(Point p[]);
	~Vector_()
	{
		cout << "";
	};
	
	int get_size();

	int insert(Point p);
	int insert(Point p[], int size);
	int insert(Point p[], int start, int size );

	int del (Point p);
	int delat (int Pos);
	int delat (int start, int length);

	int iselement (Point p);

	void displayVector_ ();//Defined
};


Vector_::Vector_()
{

}

Vector_::Vector_(Point p[])
{
	if ( initialSize > INITIAL )
	{	
		while (initialSize>INITIAL)
		{
			INITIAL += INCREMENT;
		}
	}
	pArray = new Point[INITIAL];
	pArray = p;
	size = initialSize;
}

int Vector_::insert( Point p)
{
		

	if (size+1 >= INITIAL)
	{
		while (size+1>=INITIAL)
		{
			INITIAL += INCREMENT;
		}
		Point *tempPArray;	
		
		tempPArray = new Point[INITIAL];
		tempPArray = pArray;
		tempPArray[size]=p;
		size++;
		
		//delete [] pArray;
		pArray = new Point[INITIAL];
		pArray = tempPArray;
		
	}
	else
	{
		pArray[size] = p;
		size++;
	}
	
	return size-1;//Return value: in which index the entry is inserted.
}

int Vector_::insert(Point p[], int size)
{
	int i, j;
	Point *tempPArray;
	
	if ((this->size+size) >= INITIAL)
	{
		while ((this->size+size) >= INITIAL)
		{
			INITIAL += INCREMENT;
		}
		
		tempPArray = new Point[INITIAL];
		tempPArray = pArray;
		
		for (i=0,j=this->size; i<size; i++, j++)
		{
			tempPArray[j] = p[i];
		}

		this->size += size;

		pArray = new Point[INITIAL];
		pArray = tempPArray;

	}
	else
	{
		for (i=0,j=this->size; i<size; i++, j++)
		{
			pArray[j] = p[i];
		}
		this->size += size;
	}

	return this->size-size;//index in which the first new entry is inserted.
}

int Vector_::insert(Point p[], int start, int size )
{
	//assuming start is a index. start+1 is the nth element
	int i, j;
	Point *tempPArray;


	if (start > this->size) 
	{
		cout << "Error: Start location must be <= " << this->size << endl <<endl;
	}
	
	else if ((start+size) >= this->size || (start+size) >= INITIAL)
	{
		while ((start+size) >= INITIAL)
		{
			INITIAL += INCREMENT;
		}
		
		tempPArray = new Point[INITIAL];
		tempPArray = pArray;
		
				
		for (i=0,j=start; i<size; i++, j++)
		{
			tempPArray[j] = p[i];
			//cout << p[i].get_x() << " " << p[i].get_y();
		}
	
		if ( start +size > this->size)
		{
			this->size = start +size;
		}
		//pArray = new Point[INITIAL];
		pArray = tempPArray;
	
	}
	else
	{
		for (i=0,j=start; i<size; i++, j++)
		{
			pArray[j] = p[i];

		}
	}

	
	return start;
}




int Vector_::del (Point p)
{
	//delete first occurrence of p
	
	int i, j;
	
	Point *tempPArray;
	tempPArray = new Point [size];
	int firstOccFound = 0;
	int deletedIndex = -1;

	if ( iselement(p))
	{
	
		for (i=0, j=0; i<size; i++)
		{
			if (!firstOccFound &&pArray[i].get_x()==p.get_x() && pArray[i].get_y()==p.get_y())
			{
				firstOccFound = 1;
				deletedIndex = i;
				continue;
			}
			tempPArray[j] = pArray[i];
			j++;
		}

		size = j;
		pArray = tempPArray;

		cout << "Point : (" << p.get_x()<<", "<<p.get_y()<<")" << " is deleted." << endl <<endl;
	}

	else
	{
		cout << "No entry deleted."<<endl;
		cout << "Error: Point : (" << p.get_x()<<", "<<p.get_y()<<")" << " is not in the vector."<<endl<<endl;
	}

	return deletedIndex;
}

int Vector_::delat (int Pos)
{
	int i, j;
	Point *tempPArray;
	tempPArray = new Point [size];
	
	if (Pos > size-1)
	{
		cout << "No entry deleted."<<endl;
		cout <<"Error: Position must be less than " << size << endl << endl;
	}
	
	else
	{
		for (i=0, j=0; i<size; i++)
		{
			if ( i!= Pos)
			{		
				tempPArray[j] = pArray[i];
				j++;
			}
		}
	
		cout << "Point : (" << pArray[Pos].get_x()<<", "<<pArray[Pos].get_y()<<")" << " at Postion "<< Pos << " is deleted." << endl <<endl;
		size = j;
		pArray = new Point[INITIAL];
		pArray = tempPArray;		
	}	
	
	return 1;
}

int Vector_::delat (int start, int length)
{
	int i, j;
	Point *tempPArray;
	tempPArray = new Point [size];

	if (start >= size )
	{
		cout << "No entry deleted."<<endl;
		cout << "Error: start: "<<start<<" must be less than Vector Size. Vector size is " << size <<endl<<endl;

	}
	else if (start+length >= size)
	{
		cout << "No entry deleted."<<endl;
		cout << "Error: ( start: " <<start<< " + length: "<< length << " ) = "<< start+length << " which must be less than the Vector size. Vector size = " << size <<endl<<endl;
	}

	else
	{
		cout << "Deleted Point(s) : "<<endl ;

		for (i=0, j=0; i<size; i++)
		{
			if ( i<start || i>start+length-1)
			{		
				tempPArray[j] = pArray[i];
				j++;
			}
			else
			{
				cout << "\t"<<"("<<pArray[i].get_x()<<", "<<pArray[i].get_y()<<")"<<endl;
			}
		}
		size = j;
		pArray = new Point[INITIAL];
		pArray = tempPArray;		

	}
	return 1;
}

int Vector_::iselement (Point p)
{
	int i;
	int found = 0;

	for (i=0; i<size; i++)
	{
		if ( pArray[i].get_x()==p.get_x() && pArray[i].get_y()==p.get_y())
		{
			found = 1;
			break;
		}
	}
	return found;
}

int Vector_::get_size()
{
	return size;
}

void Vector_::displayVector_ ()
{
	int i;
	
	cout << "The Vector is: "<<endl<<endl;
	for (i=0; i<size; i++)
	{
		cout << "\t";
		pArray[i].print();
	}
	cout <<endl<<endl;
}




void main ()
{
	int numPoints;

	Point *p;
	int i, x, y;
	cout << "Enter number of points : ";
	cin >>numPoints;
	initialSize = numPoints;

	p = new Point[initialSize];

	for (i=0; i<initialSize;i++)
	{
		cout << "Point "<<i+1<<endl;
		cout << "\t x: ";
		cin  >> x;
		cout << endl<<"\t y: ";
		cin >> y;
		cout << endl<<endl;
		p[i].set_x(x);
		p[i].set_y(y);
	}

	
	Vector_ v1 (p);
	v1.displayVector_();

	cout << v1.get_size();
		
	cout << endl << endl << endl;	


	Point p1[5]=
	{
		Point(1,2),
		Point(3,4),
		Point(5,6),
		Point(7,8),
		Point(9,10),
		
	};


	v1.insert(p1, 5);
	v1.displayVector_();
	

	Point p2 (11, 11);
	v1.insert(p2);
	cout << endl << endl << endl;	
	v1.displayVector_();


	

	
	
	cout << endl << endl << endl;	
	v1.insert(p1, 4, 5);
	v1.displayVector_();


	Point p3(9,10);
	cout << endl << endl << endl;	
	cout << v1.iselement(p3);
	

	
	v1.del(p3);
	cout << endl << endl << endl;	
	v1.displayVector_();



	v1.delat(3, 1);
	cout << endl << endl << endl;	
	v1.displayVector_();




}

Recommended Answers

All 2 Replies

use your compiler's debugger and step through the code. At the line where you get the assert error check variable values.

Dear friend!

I found my error. Thanks for your reply.

asfak

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.