-
help plz plz
I have a programming for heapsorting put it not work good
plz help my and chack why not work prefact .
#include<iostream.h>
#include<conio.h>
void in_arr();
void out_arr();
void buildheap();
void adjustheap(int m);
int A[100];
int n,m,i,j,k,item,el,s,done;
// **************************************************
********************
main()
{
m=n;
in_arr();
getch();
buildheap();
out_arr();
getch();
adjustheap(m);
out_arr();
cout<<"\n\n\n End of the program ";
}
// **************************************************
********************
void in_arr()
{
cout<<"\n Enter the size of the array :";
cin>>n;
cout<<"\n Enter all the elements :\n";
for(i=1;i<=n;i++)
cin>>A[i];
cout<<endl;
}
// **************************************************
*********************
void out_arr()
{
cout<<"\n The elements are :\n";
for(i=1;i<=n;i++)
cout<<A[i]<<" ";
cout<<endl;
}
// **************************************************
********************
void buildheap()
{
for(k=2;k<=n;k++)
{
i=k;
j=i/2;
while((i!=1)&&(A[i]<A[j]))
{
item=A[j];
A[j]=A[i];
A[i]=item;
i=j;
if(i>j)
j=i/2;
}}}
// **************************************************
********************
void adjustheap(int m)
{
el=A[i];
done=0;
i=1;
s=2;
while((s<m)&&(done==0))
{
if((s+1)<m)
{
if(A[s+1]<A[s])
s=s+1;
}
if(el>=A[s])
done=1;
else
{
A[i]=A[s];
A[s]=el;
i=s;
s=s*i;
}}}
-
debugging is an integral part of producing a viable program. Learn to use the error messages produced by your compiler, they are invaluable to finding compile time errors. If you have a debugging program, it can help you find run time errors. If you don't have a debugging program, then describing what type of problem is occurring and tracking variable values through a theoretical loop of the program will often help you track down the bugs. A series of cout statements will allow you to do this, if you can't/don't have software to assist you.
If you ask for help in this task, then providing the same information you need will help the assistant immensely.
-
here's a common run time error. The following snippet will compiler okay, but the results will be erratic.
cout<<"\n Enter the size of the array :";
cin>>n;
cout<<"\n Enter all the elements :\n";
for(i=1;i<=n;i++)
cin>>A[i];
The error is that if you have n elements in an array, the largest valid index is n - 1, not n. Therefore the teriminating condition for the for loop is in error. If n is less than 100 (the size of A) then you can get by with adding elements up to index i = n, but the results will be screwy, because there will be n + 1 elements in the array, not n elements. If n = 100, then element i = n will have overwritten the array, and will probably crash your program.
The key here is that element indexes in arrays are zero based, meaning the first index is 0, not 1, like when you start counting on your fingers.