Помощ за C++ ?

mumiatabg

Registered
Здравейте, дадоха ми следната задача, обаче се мъча вече доста време и нищо не се получава.. Ако може някой да помогне ще съм много благодарен Given the array A of size M
(a) Find the K-th smallest element. Find the K-th largest element.
(b) Calculate the distance between every two consecutive elements.
(c) Output A as a list of components sorted in ascending order. For example:
A = [5, 9, 3, 3, 6,2, 1, 7, 8]
Sorted components = [5, 9,], [3,3,6], [2], [1,7,8]
Ето това е задачата.. :) Благодаря предварително и се извинявам ако не е в правилния раздел темата :)
 
Ако съм разбрал условието правилно, успях да измисля само а) и в) ето о кода, ако не е така, както трябва се извинявам :)
Код:
#include <iostream>

using namespace std;

int main()
{
    int a[9]= {5, 9, 3, 3, 6,2, 1, 7, 8};
    int max=0,min=100;
    int s1=0;

    for(int i=0;i<8;i++)
    {
        if(a[i]>max)
        max=a[i];
        if(a[i]<min)
        min=a[i];

        if(a[i+1]>a[i])
        {

        s1=a[i+1]-a[i];
        cout<<"The distance between "<<a[i]<<" and "<<a[i+1]<<" is "<<s1<<endl;
        }
        else
        {
            s1=a[i]-a[i+1];
            cout<<"The distance between "<<a[i]<<" and "<<a[i+1]<<" is "<<s1<<endl;
        }
    }
    cout<<"The smallest is : "<<min<<endl;
    cout<<"The largest is : "<<max<<endl;
    return 0;
}
 

Back
Горе