Start New Discussion within our Software Development Community

Hey this is from my c++ assignment at Uni. I'm fairly new to programming so i need some help. The program has separate functions after the main() which i have to call everytime i need to use them.

So I've sorted an array using a fuction with an algorithm of 3 loops which print the 3 largest numbers. Now i have to add another algorithm in my function to print the 3 smallest numbers and i dont understand how to do that.

Additionally i have an arrady with 16 entries in my main() and i'm really not sure how to get some output going for the 3 smallest values. This is what i have so far:

This is my fuction for sorting the array:

int i, k, j, m;
    j=0;

     for(i=0; i<n; i++)
    {
        o[i]=i;
    }

    for(k=0; k<mt; k++)
    {
       j=k;

       for(i=k; i<n; i++)

       if(q[o[i]]>q[o[j]]) j=i;

    fo10ggm_swap(o[j],o[k]);
     }

The swap is another fuction that just swaps the two values.

This is the output statement that i've got:

const int x[16]={21, 11, 22, 25, 24, 11, 20, 19, 18, 18, 13,  1, 24,  1, 24,  6};
     int o[16];
     int mt=3, mb=3;
     cout << "Displaying the largest 3 and smallest 3 numbers: " ;

     fo10ggm_selection_sort(16, x, o, mt, mb);
     for(i=0; i<=2; i++)
     {
        cout << x[o[i]] << " " ;
     }

mt are the 3 Top Values (Largest) and mb are the 3 Bottom Values (Smallest)

Any kind of help will be appreciated.

Just print the 3 last elements of the sorted array:

//this prints the first three elements (largest elements)
     for(i=0; i<=2; i++)
     {
        cout << x[o[i]] << " " ;
     }
     //this prints the last three elements (smallest elements)
     for(i=0; i<=2; i++)
     {
        cout << x[o[15-i]] << " " ;
     }
This article has been dead for over six months. Start a new discussion instead.