C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
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  
Old April 30th, 2008, 01:52 AM
jsseattle jsseattle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 1 jsseattle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 m 25 sec
Reputation Power: 0
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;
}

}
}
}

Reply With Quote
  #2  
Old May 6th, 2008, 06:08 AM
MaHuJa MaHuJa is offline
Contributing User
Click here for more information.
 
Join Date: Dec 2007
Posts: 338 MaHuJa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 14 h 13 m 10 sec
Reputation Power: 1
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Please help me out on this C++ heap using dynamic array.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway