Urgent solution needed

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 2
Reputation: scsi_016 is an unknown quantity at this point 
Solved Threads: 0
scsi_016 scsi_016 is offline Offline
Newbie Poster

Urgent solution needed

 
0
  #1
Sep 25th, 2007
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.

  1. #include <iostream.h>
  2.  
  3. int INITIAL = 6;//allocation size
  4. int INCREMENT = 4;//if more allocation needed
  5. int initialSize;//initial number of array elements
  6.  
  7. class Point
  8. {
  9. int x;
  10. int y;
  11.  
  12. public:
  13.  
  14. Point();
  15. Point ( int x, int y );
  16. ~Point()
  17. {
  18. cout << "";
  19. }
  20.  
  21.  
  22. int get_x();
  23. int get_y();
  24.  
  25. void set_x(int x);
  26. void set_y(int y);
  27.  
  28. void print();
  29.  
  30. };
  31.  
  32. Point::Point()
  33. {
  34.  
  35. }
  36. Point::Point(int x, int y)
  37. {
  38. this->x = x;
  39. this->y = y;
  40. }
  41.  
  42. void Point::set_x(int x)
  43. {
  44. this->x = x;
  45. }
  46.  
  47. void Point::set_y(int y)
  48. {
  49. this->y = y;
  50. }
  51.  
  52. int Point::get_x()
  53. {
  54. return x;
  55. }
  56.  
  57. int Point::get_y()
  58. {
  59. return y;
  60. }
  61.  
  62. void Point::print()
  63. {
  64. cout << " (" << get_x() << ", " << get_y() << ") " << endl;
  65. }
  66.  
  67.  
  68. class Vector_
  69. {
  70. Point *pArray;
  71. int size;
  72.  
  73. public:
  74. Vector_();
  75. Vector_(Point p[]);
  76. ~Vector_()
  77. {
  78. cout << "";
  79. };
  80.  
  81. int get_size();
  82.  
  83. int insert(Point p);
  84. int insert(Point p[], int size);
  85. int insert(Point p[], int start, int size );
  86.  
  87. int del (Point p);
  88. int delat (int Pos);
  89. int delat (int start, int length);
  90.  
  91. int iselement (Point p);
  92.  
  93. void displayVector_ ();//Defined
  94. };
  95.  
  96.  
  97. Vector_::Vector_()
  98. {
  99.  
  100. }
  101.  
  102. Vector_::Vector_(Point p[])
  103. {
  104. if ( initialSize > INITIAL )
  105. {
  106. while (initialSize>INITIAL)
  107. {
  108. INITIAL += INCREMENT;
  109. }
  110. }
  111. pArray = new Point[INITIAL];
  112. pArray = p;
  113. size = initialSize;
  114. }
  115.  
  116. int Vector_::insert( Point p)
  117. {
  118.  
  119.  
  120. if (size+1 >= INITIAL)
  121. {
  122. while (size+1>=INITIAL)
  123. {
  124. INITIAL += INCREMENT;
  125. }
  126. Point *tempPArray;
  127.  
  128. tempPArray = new Point[INITIAL];
  129. tempPArray = pArray;
  130. tempPArray[size]=p;
  131. size++;
  132.  
  133. //delete [] pArray;
  134. pArray = new Point[INITIAL];
  135. pArray = tempPArray;
  136.  
  137. }
  138. else
  139. {
  140. pArray[size] = p;
  141. size++;
  142. }
  143.  
  144. return size-1;//Return value: in which index the entry is inserted.
  145. }
  146.  
  147. int Vector_::insert(Point p[], int size)
  148. {
  149. int i, j;
  150. Point *tempPArray;
  151.  
  152. if ((this->size+size) >= INITIAL)
  153. {
  154. while ((this->size+size) >= INITIAL)
  155. {
  156. INITIAL += INCREMENT;
  157. }
  158.  
  159. tempPArray = new Point[INITIAL];
  160. tempPArray = pArray;
  161.  
  162. for (i=0,j=this->size; i<size; i++, j++)
  163. {
  164. tempPArray[j] = p[i];
  165. }
  166.  
  167. this->size += size;
  168.  
  169. pArray = new Point[INITIAL];
  170. pArray = tempPArray;
  171.  
  172. }
  173. else
  174. {
  175. for (i=0,j=this->size; i<size; i++, j++)
  176. {
  177. pArray[j] = p[i];
  178. }
  179. this->size += size;
  180. }
  181.  
  182. return this->size-size;//index in which the first new entry is inserted.
  183. }
  184.  
  185. int Vector_::insert(Point p[], int start, int size )
  186. {
  187. //assuming start is a index. start+1 is the nth element
  188. int i, j;
  189. Point *tempPArray;
  190.  
  191.  
  192. if (start > this->size)
  193. {
  194. cout << "Error: Start location must be <= " << this->size << endl <<endl;
  195. }
  196.  
  197. else if ((start+size) >= this->size || (start+size) >= INITIAL)
  198. {
  199. while ((start+size) >= INITIAL)
  200. {
  201. INITIAL += INCREMENT;
  202. }
  203.  
  204. tempPArray = new Point[INITIAL];
  205. tempPArray = pArray;
  206.  
  207.  
  208. for (i=0,j=start; i<size; i++, j++)
  209. {
  210. tempPArray[j] = p[i];
  211. //cout << p[i].get_x() << " " << p[i].get_y();
  212. }
  213.  
  214. if ( start +size > this->size)
  215. {
  216. this->size = start +size;
  217. }
  218. //pArray = new Point[INITIAL];
  219. pArray = tempPArray;
  220.  
  221. }
  222. else
  223. {
  224. for (i=0,j=start; i<size; i++, j++)
  225. {
  226. pArray[j] = p[i];
  227.  
  228. }
  229. }
  230.  
  231.  
  232. return start;
  233. }
  234.  
  235.  
  236.  
  237.  
  238. int Vector_::del (Point p)
  239. {
  240. //delete first occurrence of p
  241.  
  242. int i, j;
  243.  
  244. Point *tempPArray;
  245. tempPArray = new Point [size];
  246. int firstOccFound = 0;
  247. int deletedIndex = -1;
  248.  
  249. if ( iselement(p))
  250. {
  251.  
  252. for (i=0, j=0; i<size; i++)
  253. {
  254. if (!firstOccFound &&pArray[i].get_x()==p.get_x() && pArray[i].get_y()==p.get_y())
  255. {
  256. firstOccFound = 1;
  257. deletedIndex = i;
  258. continue;
  259. }
  260. tempPArray[j] = pArray[i];
  261. j++;
  262. }
  263.  
  264. size = j;
  265. pArray = tempPArray;
  266.  
  267. cout << "Point : (" << p.get_x()<<", "<<p.get_y()<<")" << " is deleted." << endl <<endl;
  268. }
  269.  
  270. else
  271. {
  272. cout << "No entry deleted."<<endl;
  273. cout << "Error: Point : (" << p.get_x()<<", "<<p.get_y()<<")" << " is not in the vector."<<endl<<endl;
  274. }
  275.  
  276. return deletedIndex;
  277. }
  278.  
  279. int Vector_::delat (int Pos)
  280. {
  281. int i, j;
  282. Point *tempPArray;
  283. tempPArray = new Point [size];
  284.  
  285. if (Pos > size-1)
  286. {
  287. cout << "No entry deleted."<<endl;
  288. cout <<"Error: Position must be less than " << size << endl << endl;
  289. }
  290.  
  291. else
  292. {
  293. for (i=0, j=0; i<size; i++)
  294. {
  295. if ( i!= Pos)
  296. {
  297. tempPArray[j] = pArray[i];
  298. j++;
  299. }
  300. }
  301.  
  302. cout << "Point : (" << pArray[Pos].get_x()<<", "<<pArray[Pos].get_y()<<")" << " at Postion "<< Pos << " is deleted." << endl <<endl;
  303. size = j;
  304. pArray = new Point[INITIAL];
  305. pArray = tempPArray;
  306. }
  307.  
  308. return 1;
  309. }
  310.  
  311. int Vector_::delat (int start, int length)
  312. {
  313. int i, j;
  314. Point *tempPArray;
  315. tempPArray = new Point [size];
  316.  
  317. if (start >= size )
  318. {
  319. cout << "No entry deleted."<<endl;
  320. cout << "Error: start: "<<start<<" must be less than Vector Size. Vector size is " << size <<endl<<endl;
  321.  
  322. }
  323. else if (start+length >= size)
  324. {
  325. cout << "No entry deleted."<<endl;
  326. cout << "Error: ( start: " <<start<< " + length: "<< length << " ) = "<< start+length << " which must be less than the Vector size. Vector size = " << size <<endl<<endl;
  327. }
  328.  
  329. else
  330. {
  331. cout << "Deleted Point(s) : "<<endl ;
  332.  
  333. for (i=0, j=0; i<size; i++)
  334. {
  335. if ( i<start || i>start+length-1)
  336. {
  337. tempPArray[j] = pArray[i];
  338. j++;
  339. }
  340. else
  341. {
  342. cout << "\t"<<"("<<pArray[i].get_x()<<", "<<pArray[i].get_y()<<")"<<endl;
  343. }
  344. }
  345. size = j;
  346. pArray = new Point[INITIAL];
  347. pArray = tempPArray;
  348.  
  349. }
  350. return 1;
  351. }
  352.  
  353. int Vector_::iselement (Point p)
  354. {
  355. int i;
  356. int found = 0;
  357.  
  358. for (i=0; i<size; i++)
  359. {
  360. if ( pArray[i].get_x()==p.get_x() && pArray[i].get_y()==p.get_y())
  361. {
  362. found = 1;
  363. break;
  364. }
  365. }
  366. return found;
  367. }
  368.  
  369. int Vector_::get_size()
  370. {
  371. return size;
  372. }
  373.  
  374. void Vector_::displayVector_ ()
  375. {
  376. int i;
  377.  
  378. cout << "The Vector is: "<<endl<<endl;
  379. for (i=0; i<size; i++)
  380. {
  381. cout << "\t";
  382. pArray[i].print();
  383. }
  384. cout <<endl<<endl;
  385. }
  386.  
  387.  
  388.  
  389.  
  390. void main ()
  391. {
  392. int numPoints;
  393.  
  394. Point *p;
  395. int i, x, y;
  396. cout << "Enter number of points : ";
  397. cin >>numPoints;
  398. initialSize = numPoints;
  399.  
  400. p = new Point[initialSize];
  401.  
  402. for (i=0; i<initialSize;i++)
  403. {
  404. cout << "Point "<<i+1<<endl;
  405. cout << "\t x: ";
  406. cin >> x;
  407. cout << endl<<"\t y: ";
  408. cin >> y;
  409. cout << endl<<endl;
  410. p[i].set_x(x);
  411. p[i].set_y(y);
  412. }
  413.  
  414.  
  415. Vector_ v1 (p);
  416. v1.displayVector_();
  417.  
  418. cout << v1.get_size();
  419.  
  420. cout << endl << endl << endl;
  421.  
  422.  
  423. Point p1[5]=
  424. {
  425. Point(1,2),
  426. Point(3,4),
  427. Point(5,6),
  428. Point(7,8),
  429. Point(9,10),
  430.  
  431. };
  432.  
  433.  
  434. v1.insert(p1, 5);
  435. v1.displayVector_();
  436.  
  437.  
  438. Point p2 (11, 11);
  439. v1.insert(p2);
  440. cout << endl << endl << endl;
  441. v1.displayVector_();
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448. cout << endl << endl << endl;
  449. v1.insert(p1, 4, 5);
  450. v1.displayVector_();
  451.  
  452.  
  453. Point p3(9,10);
  454. cout << endl << endl << endl;
  455. cout << v1.iselement(p3);
  456.  
  457.  
  458.  
  459. v1.del(p3);
  460. cout << endl << endl << endl;
  461. v1.displayVector_();
  462.  
  463.  
  464.  
  465. v1.delat(3, 1);
  466. cout << endl << endl << endl;
  467. v1.displayVector_();
  468.  
  469.  
  470.  
  471.  
  472. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,342
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Urgent solution needed

 
0
  #2
Sep 25th, 2007
use your compiler's debugger and step through the code. At the line where you get the assert error check variable values.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 2
Reputation: scsi_016 is an unknown quantity at this point 
Solved Threads: 0
scsi_016 scsi_016 is offline Offline
Newbie Poster

Re: Urgent solution needed

 
0
  #3
Sep 28th, 2007
Dear friend!

I found my error. Thanks for your reply.

asfak
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC