Thread: c++

  1. #1
    Unregistered
    Guest

    c++

    I messed up the first thread...this is the one i was talking about..

    can someone help me with this program? You do not have to go through the entire code. Just the return statement back to the recursive function isnt working. when I cout<<mm..it does it twice and puts garbage in it the second time. I want .it to store simply the first number it displays....this is the header file. the main.cpp simply reads in an array of numbers, stores it in an array and calls the select function......



    #ifndef Kth_h
    #define Kth_h

    class Kth
    {
    private:
    int n;
    int Bsize;
    int B_array[100];
    int mm; //median of medians
    int m;
    int u, v;


    public:

    Kth (void)
    {
    n=0;
    mm=0;
    Bsize=0;
    m=0;
    u=v=0;

    }

    int Select(int *A, int i, int j, int k)
    {


    n=j-i+1;


    if(n%5==0)
    Bsize=n/5;
    else
    Bsize=(n/5)+1;

    int p, jj, ii;

    if ((j-i+1)<=5)
    {
    for( ii=1; ii<n; ii++)
    {
    p=A[ii];
    jj=ii;

    while(A[jj-1]>p){
    A[jj]=A[jj-1];
    jj=jj-1;
    if(jj<=0) break;
    }
    A[jj]=p;
    }

    cout<<A[k-1]<<endl;
    return A[k-1];

    }

    //////////////////////////////////////

    ii=jj=p=0;
    int index=0;
    int median=0;
    int counter=1;
    int temp=0;


    for(int y=0; y<n/5; y++)
    {
    for(ii=counter; ii<counter+4; ii++)
    {
    p=A[ii];
    jj=ii;

    while(A[jj-1] >p){
    A[jj]=A[jj-1];
    jj=jj-1;
    if (jj<=counter-1) break;
    }

    A[jj]=p;
    }


    median=temp+2;
    temp+=5;
    counter= counter+5;
    B_array[index]=A[median];
    index++;

    }

    /////////////////////////////////////////////
    int vv=0;
    int c=0;
    if((n%5)==1)
    {
    B_array[index]=A[counter-1];
    index++;
    }

    if((n%5)==2 || (n%5)==3 || (n%5)==4)
    {
    for(int b=counter; b<((counter+(n%5))-1); b++)
    {
    vv=A[b];
    c=b;

    while(A[c-1] >vv){
    A[c]=A[c-1];
    c=c-1;
    if (c<=counter-1) break;
    }

    A[c]=vv;

    }
    if(n%5==2)
    B_array[index]=A[counter-1];
    else
    B_array[index]=A[counter];
    index++;

    }

    //////////////////////////////////////////////////


    int t=0;

    if(Bsize%2==0)
    t=Bsize/2;
    else
    t=(Bsize/2)+1;


    mm=Select(B_array, 1, Bsize, t);
    cout<<mm<<endl;



    }




    };

    #endif

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    use code tags, please

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    And change those variable names to something a bit more understandable than "jj" and the like =P

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    First, look at the Everyone Read thread up there that explains about code tags. Then edit your post and use them.
    And then the code...

    Code:
    while(A[c-1] >vv){ 
    A[c]=A[c-1]; 
    c=c-1; 
    if (c<=counter-1) break; 
    }
    Are you expecting anyone other then yourself to understand this?
    Use more clarifying variable names!

Popular pages Recent additions subscribe to a feed