473,837 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the problem with this???

Helo friends
Could any of you please help me out with this problem.
To me this program of heap sort seems logically perfect but still the
output of this is not coming right,
I dont want to follow any other solution.Could any of you please take
out some precious time of yours in solving this big problem for me,
where m i going wrong???
Thanks in advance.
// heapsort111.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<iostre am>
#include<stdlib .h>

using namespace std;

int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;

void swap(int a,int b)
{
int temp=s[b];
s[b]=s[a];
s[a]=temp;
}

void keepheap(int i)
{
int l,r,p,q,t,large st,m;

l = 2*i;
r = 2*i + 1;

p = s[l];
q = s[r];
t = s[i];

if(l<heapsize && p > t)
largest = l;
else
largest = i;

m = s[largest];

if(r<heapsize && q > m)
largest = r;

if(largest != i)
{
swap(i, largest);
keepheap(larges t);
}
}
void makeheap(int n)
{
heapsize=n;
for(int i=(n/2)-1; i>=0; i--)
keepheap(i);
}

void heapsort()
{
makeheap(n);

for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

return 0;
}

Jun 1 '06 #1
4 2105

<co******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Helo friends
Could any of you please help me out with this problem.
To me this program of heap sort seems logically perfect but still the
output of this is not coming right,
I dont want to follow any other solution.Could any of you please take
out some precious time of yours in solving this big problem for me,
where m i going wrong???
Thanks in advance.
// heapsort111.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<iostre am>
#include<stdlib .h>

using namespace std;

int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;

void swap(int a,int b)
{
int temp=s[b];
s[b]=s[a];
s[a]=temp;
}

void keepheap(int i)
{
int l,r,p,q,t,large st,m;

l = 2*i;
r = 2*i + 1;

p = s[l];
q = s[r];
t = s[i];

if(l<heapsize && p > t)
largest = l;
else
largest = i;

m = s[largest];

if(r<heapsize && q > m)
largest = r;

if(largest != i)
{
swap(i, largest);
keepheap(larges t);
}
}
void makeheap(int n)
{
heapsize=n;
for(int i=(n/2)-1; i>=0; i--)
keepheap(i);
}

void heapsort()
{
makeheap(n);

for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

return 0;
}


Someone posts code and says "To me this program of heap sort seems logically
perfect but still the output of this is not coming right" I'll glance at the
code for obvious errors and go on may way.

If you want quick answers, show us what you expect the output to be, and
what the actual output is.
Jun 2 '06 #2
co******@gmail. com wrote:
int _tmain(int argc, _TCHAR* argv[])


What on Earth is this?

--
Ian Collins.
Jun 2 '06 #3

"Ian Collins" <ia******@hotma il.com> wrote in message
news:4e******** *****@individua l.net...
co******@gmail. com wrote:
int _tmain(int argc, _TCHAR* argv[])


What on Earth is this?


Microsoft junk.

He simply needs to not use precompiled headers.
Jun 2 '06 #4

<co******@gmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
Helo friends
Could any of you please help me out with this problem.
To me this program of heap sort seems logically perfect but still the
output of this is not coming right,
We don't know the "logic" of the program, since you haven't specified it in
comments anywhere.

Regarding the output: tell us what the output is, and what you expect it to
be, not just that it "is not coming right".
I dont want to follow any other solution.Could any of you please take
out some precious time of yours in solving this big problem for me,
where m i going wrong???
Thanks in advance.
// heapsort111.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include<iostre am>
#include<stdlib .h>

using namespace std;

int n=10,s[10]={11,2,9,13,57, 25,17,1,90,3},h eapsize;
Why are you declaring n as a global variable here? Why not just pass 10 to
the function heapsort()? You're likely to have troubles (at least using
your debugger) with a local variable named n and a global variable also
named n. At the least, you might forget which one you're referring to at
any given time.

void swap(int a,int b)
{
int temp=s[b];
s[b]=s[a];
s[a]=temp;
}

Comments as to what each function is supposed to do would really help here.
(And they should be part of your design process, before you actually add
code to the functions, not an afterthought.)
void keepheap(int i)
{
int l,r,p,q,t,large st,m;
Ok, "largest" is a pretty good name, but the rest of those are meaningless,
and won't help you (or anyone else) figure out what they're used for.

l = 2*i;
r = 2*i + 1;

p = s[l];
q = s[r];
t = s[i];

if(l<heapsize && p > t)
largest = l;
else
largest = i;

m = s[largest];

if(r<heapsize && q > m)
largest = r;

if(largest != i)
{
swap(i, largest);
keepheap(larges t);
}
}
void makeheap(int n)
{
heapsize=n;
for(int i=(n/2)-1; i>=0; i--)
keepheap(i);
}

void heapsort()
{
makeheap(n);
You only call makeheap once, from here, and pass it the global variable n.
Why have a separate function makeheap at all? Why not just put the loop
from there in this function? I think that _might_ help you see what's going
on better in this case, (especially in relation to the variables n and
heapsize and the loops you're executing).

(I know it's good to break down code into single tasks, but you're not being
consistent when you put the first loop into makeheap, but keep the second
loop in this function.)

for(int i=(n/2)-2; i>=0; i--)
{
swap(0,i);
heapsize--;
keepheap(i-1);
}
}
int _tmain(int argc, _TCHAR* argv[])
You should consider not using precompiled headers, removing the include of
stdafx.h above, and using a standard main function here.
{
int i;
cout<<"The Initial Array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

heapsort();

cout<<"\nTHe Sorted array is : ";
for(i=0;i<n;i++ )
cout<<s[i]<<"; ";

return 0;
}


Use a debugger, and step through the code. See if it's doing what you
expect. Write down what you _expect_ to see at each step on paper, then
copmare against that. At some point, what you see in the debugger won't
match what you expected. That should point out the error in your code.

-Howard


Jun 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2386
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't help but say i was totally impressed. I needed an open source wikiblog/wikilog, whatever you wanna call it, basically a hybrid of a blog and a wiki. I checked out snipsnap, which uses java, it was said on their site to be a clone of vanilla, a...
8
3893
by: Randell D. | last post by:
I have just recompiled, upgraded to PHP 4.3.4. As an exercise (and curiosity) I've decided to test out PDF functions and got the test in the PHP online manual working. I had one problem whereby the example refered to Times New Roman - I didn't have this, but I did have Times-Roman which worked. My question - How do I know what fonts I *do* have available? I've run phpinfo() and gd_info() which I hope is enough to tell some wise person...
2
3114
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is burns. Here is the code: $frank = "burns";
220
19234
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it seems that Ruby has the potential to compete with and displace Python. I'm curious on what basis it...
699
34360
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
11
23550
by: Leo | last post by:
hi there for somebody who wants tostart small/medium GUI apps with python: what's the best toolkit: tkinter, wxPython or what? stability, ease of use and portability between mac and windows are the main criteria. thanks, leo
3
1392
by: Kenneth McDonald | last post by:
One of my current "fun" goals is to start playing with 3D related things, purely because I find it of interest. Since I also use and love Python in more serious endeavours, it would seem to make sense to try to find 3D related things that involve Python. In fact, there seem to be quite a few--so many that I'm not really sure what is active and growing, and what is dying off. So I was wondering if there are Pythonistas out there with...
9
2683
by: Martin Maney | last post by:
In my copious spare time I've been dabbling at getting a computerized version of a board game working. After deciding that tk just made me want to vomit, and wx was like swimming through concrete slurry, and others I have mercifully forgotten about, I stumbled across pygame. (maybe not for the first time; I think I set it aside earlier because it was described as being aimed at a different sort of game, and besides, I had hoped wx might...
3
2432
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be usefull. Interesting in any case. Making decorators with this class is a snap!
30
15456
by: James Conrad StJohn Foreman | last post by:
After 3 years of using DB2 on Linux, I'm leaving my current employers to go work for a SQL Server shop instead. In order to find my replacement, they're trying to put together a set of questions to get both some understanding of how wide candidates knowledge is, and how much DB2 specifics they know. Of the questions below, how many do you think are useful in determining if you've got somebody capable of keeping a DB2 instance up,...
0
10883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10578
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10631
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9409
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7813
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5670
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.