| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Please help me out on this C++ heap using dynamic array.
I'm almost done with the code, but it gives me an Debug error; invalid allocation size: 4295967295 bytes.
I don't know why D shows up as a stupid green smile icon. anyway send me email Visual Studio 2005. Here's my header file "heap.h" #include <iostream> #include <iomanip> using namespace std; class Heap{ public: Heap(); void Insert(int value); int Delete(); //returning whatever you delete void Display(); private: int *vheap; //vheap = variable of heap int max, size, value; // void CompareParent(int index); // with inserting void CompareChild(int index); // with deleting void Increase(); void Decrease(); }; ---------------------------------------- Here's the implementation file #include "heap.h" Heap::Heap() { vheap = new int[max]; size=0; max=7; } void Heap::Insert(int value) // inserting the index, { if(size == max) // check if the size if it's full then increase it. Insert that index of the size. Increase(); vheap[size]=value; // actual insertion of the data, CompareParent(size); size++; } int Heap: elete() // deleting the top one, to keep the balance, bottom one up to the top.{ int temp=0; //use temp to return the value I deleted if(size == 0) cout<<"Error : Empty heap"<<endl; if(size != 0) { temp=vheap[0]; // vheap[0]=vheap[size-1];//heap size is zero EQUALS TO heap size-1 //vheap[size-1]=temp; size--; // decreasing size CompareChild(0); } if(size < max/3 && max >7) Decrease(); return vheap[size-1]; } void Heap: isplay(){ int temp=2; int space = 6*((max+1)/2); for(int i=0; i<size; i++) { cout<<setw(space/temp) <<vheap[i]; cout<<setw(space/temp) <<" "; if(temp-2 == i) { cout<<endl; temp = temp*2; } } cout<<endl; } void Heap::CompareParent(int index) { int temp; if(vheap[index] < vheap[(index-1)/2]) // until the index is less than its parent { temp=vheap[index]; vheap[index]=vheap[(index-1)/2];// move it to its parent vheap[(index-1)/2]=temp; index=(index-1)/2; CompareParent(index); //it makes it a recursive function. Basically it causes the function to repeat until the if statement is false //Every time it runs it also runs with different data getting passed to it. } } void Heap::CompareChild(int index) // check both right and left. { int temp; int child=1; if(vheap[(index*2)+1]> vheap[(index*2)+2]) child = 2; if(index*2+child && vheap[index]>vheap[index*2+child]) // check parent with the size temp=vheap[index]; vheap[index]=vheap[index*2+child]; vheap[index*2+child]=temp; index=index*2+child; CompareChild(index); } void Heap::Increase() // increase size { max=max*2+1; int * temp; temp=new int[max]; // create a new array for(int i=0; i<size; i++) //moves everything from heap array into the new array., new array is the bigger one temp[i] = vheap[i]; delete[] vheap; // delete the old array vheap=temp; // set the heap pointer into the new array } void Heap: ecrease(){ max=(max-1)/2; // cutting off the level as decreased int *temp; temp = new int[max]; // new array for(int i=0; i<size; i++) // keep moving the value over arrays temp[i] = vheap[i]; delete [] vheap; // delete heap vheap =temp; } ------------------------------- and Main program for user #include "heap.h" int main() { char choice='1'; int data; Heap a; while (choice != 'q') { cout << endl << endl; cout << "This is a program to make heap, you can start with insertion, and deletion, and display heap on your screen"<<endl; cout << "--------------------------------"<<endl; cout << "1. Insert a value." << endl; cout << "2. Delete a value." << endl; cout << "3. Display the heap." << endl; cout << "q. Quit." << endl; cout << "--------------------------------"<<endl; cin >> choice; switch (choice) { case '1': { cout << "Enter data to insert into heap:"; cin >> data; a.Insert(data); break; } case '2': { cout << "Enter data to delete:"; cin >> data; a.Delete(); break; } case '3': { a.Display(); break; } } } } |
|
#2
|
|||
|
|||
|
The D only becomes
when there is a : in front of the D - because :-D minus the - is something replaced with a smiley by the forum software. Unless you tell it not to when submitting; there's a checkbox for it.Check that all ints are properly initialized and that it doesn't get set to -1. When a 32-bit unsigned int gets set to -1 the answer is at least close to 4295967295. Edit: That constructor first allocates new int[max] and then sets max only later. Which means that max is uninitialized (and set to -1 or so) when first used. Last edited by MaHuJa : May 6th, 2008 at 06:14 AM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Please help me out on this C++ heap using dynamic array. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|