Help... runtime error in my maxheap sort program


 
Thread Tools Search this Thread
Top Forums Programming Help... runtime error in my maxheap sort program
# 1  
Old 04-06-2011
Help... runtime error in my maxheap sort program

#include<iostream>
using namespace std;
int *a,size,heapsize;
void maxheap(int *a,int j)
{
int l,r,largest,temp;
l=2*j;
r=(2*j)+1;
for(j=0;j<size;++j)
{
if(l<=heapsize && a[l]>a[j])
largest=l;
else
largest=j;
if(r<=heapsize && a[r]>a[largest])
largest=r;
if(largest!=j)
{
a[temp]=a[j];
a[j]=a[largest];
a[largest]=a[temp];
maxheap(a,largest);
}
}
}
void buildmax(int *a)
{
heapsize=size;
int n=size/2;
for(int i=n;i>0;--i)
maxheap(a,i);
}
void heapsort(int *a)
{
int tempo;
buildmax(a);
for(int i=heapsize;i>0;--i)
{
a[tempo]=a[0];
a[0]=a[i];
a[i]=a[tempo];
--heapsize;
maxheap(a,0);
}
}
int main()
{
cout<<"\nEnter the number of elements you want to enter in the array: ";
cin>>size;
int *a= new int [size];
cout<<"\nEnter an array of elements: ";
for(int i=0;i<size;++i)
cin>>a[i];
heapsort(a);
/* cout<<"\nThe corresponding array is: ";
for(int i=0;i<size;++i)
cout<<a[i]; */
buildmax(a);
cout<<"\nThe sorted array is: ";
for(int i=0;i<size;++i)
cout<<a[i]<<"\t";
return 0;
}
# 2  
Old 04-06-2011
1) Please use code tags when you post code
2) What error do you get?
# 3  
Old 04-06-2011
I'm getting an error said as:
"Segmentation fault" after entering the array.
# 4  
Old 04-06-2011
Code:
#include<iostream>
using namespace std;
int *a,size,heapsize;
void maxheap(int *a,int j)
{
    int l,r,largest,temp;
    l=2*j;
    r=(2*j)+1;
    for(j=0;j<size;++j)
    {
        if(l<=heapsize && a[l]>a[j])
            largest=l;
        else
            largest=j;
        if(r<=heapsize && a[r]>a[largest])
            largest=r;
        if(largest!=j)
        {
            a[temp]=a[j];
            a[j]=a[largest];
            a[largest]=a[temp];
            maxheap(a,largest);
        }
    }    
}
void buildmax(int *a)
{
    heapsize=size;
    int n=size/2;
    for(int i=n;i>0;--i)
        maxheap(a,i);
}
void heapsort(int *a)
{
    int tempo;
    buildmax(a);
    for(int i=heapsize;i>0;--i)
    {
        a[tempo]=a[0];  
        a[0]=a[i];
        a[i]=a[tempo];
        --heapsize;
        maxheap(a,0);
    }
}
int main()
{
    cout<<"\nEnter the number of elements you want to enter in the array: ";
    cin>>size;
    int *a= new int [size];
    cout<<"\nEnter an array of elements: ";
    for(int i=0;i<size;++i)
        cin>>a[i];
    heapsort(a);
   /* cout<<"\nThe corresponding array is: ";
    for(int i=0;i<size;++i)
        cout<<a[i]; */
    buildmax(a);
    cout<<"\nThe sorted array is: ";
    for(int i=0;i<size;++i)
        cout<<a[i]<<"\t";
    return 0;
}

What do you consider "entering the array".
Anyways, in function heapsort you make use of an uninitialized int tempo, so I assume you will most likely have the error there
# 5  
Old 04-06-2011
Error in my heapsort program.

:
#include<iostream>
using namespace std;
int a[100],size,heapsize;
void maxheap(int *a,int j)
{
int l,r,largest,temp;
l=2*j;
r=(2*j)+1;
for(j=0;j<size;++j)
{
if(l<=heapsize && a[l]>a[j])
largest=l;
else
largest=j;
if(r<=heapsize && a[r]>a[largest])
largest=r;
if(largest!=j)
{
a[temp]=a[j];
a[j]=a[largest];
a[largest]=a[temp];
maxheap(a,largest);
}
}
}
void buildmax(int *a)
{
heapsize=size;
int n=size/2;
for(int i=heapsize;i<n;--i)
maxheap(a,i);
}
void heapsort(int *a)
{
int tempo;
buildmax(a);
for(int i=size;i>0;--i)
{
tempo=a[0];
a[0]=a[i];
a[i]=tempo;
--heapsize;
maxheap(a,0);
}
}
int main()
{
cout<<"\nEnter the number of elements you want to enter in the array: ";
cin>>size;
cout<<"\nEnter an array of elements: ";
for(int i=0;i<size;++i)
cin>>a[i];
heapsort((int *)a[size]);
cout<<"\nThe corresponding array is: ";
for(int i=0;i<size;++i)
cout<<a[i];
buildmax((int *)a[size]);
cout<<"\nThe sorted array is: ";
for(int i=0;i<size;++i)
cout<<a[i];
return 0;
}

---------- Post updated at 06:57 PM ---------- Previous update was at 06:50 PM ----------

Why is my code tags not working when I'm trying to post a code?

Last edited by poonam.gaigole; 04-06-2011 at 10:25 AM..
# 6  
Old 04-06-2011
Quote:
Why is my code tags not working when I'm trying to post a code?
Quote your own post and you'll see what you did wrong. Your code tags aren't around your code, they're just kind of sitting somewhere by themselves.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending output of program into subsequent commands (i.e. awk/sort)

Hi, I have identified how to use command chaining as per below on a file, to capture the header of a file, as well as the line containing the C: drive. $ cat test.txt Filesystem Size Used Avail Use% Mounted on rootfs 237G 153G 84G 65% / none 237G 153G 84G ... (6 Replies)
Discussion started by: sand1234
6 Replies

2. Red Hat

Runtime Error Enable user directory apache

Hi I am exactly according to this link CentOS 6 - Apache httpd - Enable Userdir : Server World I Enabled userDirectory Server version: Apache/2.2.15 CentOS release 6.8 (Final) But Iget this Error Forbidden You don't have permission to access /~mn/index.html on this server Goal... (2 Replies)
Discussion started by: mnnn
2 Replies

3. Programming

Fortran runtime error: Insufficient data descriptors in format after reversion

Hello, I have some Fortran code that is providing the following error: At line 1165 of lapc_shells_2.f Fortran runtime error: Insufficient data descriptors in format after reversion Here are the lines just above and including 1165: WRITE (*,"('ATTEMPTING TO READ PLATE... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

4. UNIX for Dummies Questions & Answers

No Java runtime environment (JRE) error

Hi all, I am trying to install a .bin file for that it requires IBMJava2-AMD64-142-JRE-1.4.2-13.8.x86_64.rpm to be installed. I have installed this rpm but when i try to install .bin file, it complains that no JRE found. How to solve this. Thanks in advance! #... (0 Replies)
Discussion started by: lramsb4u
0 Replies

5. Programming

Runtime error in my code...

INFIX TO POSTFIX CONVERSION : //Convert an infix expression to postfix expression... #include<iostream> #include<cstring> #include<cstdlib> using namespace std; char ifx,pfx,stk; int top=-1,n; void push(char ch) { if(top!=n-1) { top++; stk=ch; } ... (3 Replies)
Discussion started by: poonam.gaigole
3 Replies

6. Shell Programming and Scripting

How to give runtime arguments to different program

I have a shell script that is attempting to call a c program. I call the c program with ./dictool dictool accepts arguments at runtime. It works by prompting the user for various commands, acting on those commands, spitting out an output, and then prompting for more commands. My question is,... (1 Reply)
Discussion started by: aarongoldfein
1 Replies

7. Programming

Runtime error in C++ Program - Help

All, I am getting this when i try to ran a program in HP unix. Things i came across 1. i have a system HP-UNIX where this same exe is working. 2. We have set a new HP_UNIX with the same configration and copied the exe to the new system we are getting this error. 3. Only... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

8. UNIX for Dummies Questions & Answers

Runtime Error...

My system did stay appears the error Run Time Library Error. What itīs? When the error appear, iīve to reboot my system and lost all I did. Is there the UNIX System problem? Please. I need help!!! (4 Replies)
Discussion started by: marpin
4 Replies

9. Solaris

Runtime error...

My interprise use a UNIX mainform for a instrumentation process control. The control use the FOXBORO INVENSYS system and they donīt gonna solve the problem the run time error. The run time error happen without logic explication. When everything itīs run perfectely and happenly appears the run time... (1 Reply)
Discussion started by: marpin
1 Replies
Login or Register to Ask a Question