help plz plz

This is a discussion on help plz plz within the C++ Programming forums, part of the General Programming Boards category; I have a programming for heapsorting put it not work good plz help my and chack why not work prefact ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    Angry 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;

    }}}

  2. #2
    Unregistered
    Guest
    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.

  3. #3
    Unregistered
    Guest
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. plz help me...
    By sweetchakri in forum C Programming
    Replies: 1
    Last Post: 03-03-2009, 10:50 PM
  3. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 08:54 AM
  4. Anyone plz help me
    By Rose_Flowers in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-17-2003, 12:01 PM
  5. plz help me fix this problem
    By Joe100 in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2003, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21