Jump to content

задача C++


Препоръчан пост

Добър ден!

Може ли,някои да ми помогне с една задача по програмиране С++?

Да се състави програма,която да сортира едномерен масив от реални числа тип double с име  P състоящ се от 26 елемента.

Сортирането да се извърши в низходящ ред чрез метода клатене.

 

С уважение Н.Петрова

Link to comment
Сподели другаде


#include <iostream>
using namespace std;

void print_ar (double ar[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << ar[i] << " ";
}
cout << endl;
}

void shaker_sort (double ar[], int size)
{
for ( int i = 1; i < size; ++i)
{
for (int j = i - 1; j < size - i; ++j)
{
if (ar[j] < ar[j + 1])
{
swap (ar[j], ar[j + 1]);
}
}

for (int j = size - i - 1; j >= i; --j)
{
if (ar[j] > ar[j - 1])
{
swap (ar[j], ar[j - 1]);
}
}
}
}

int main()
{
double ar [] = {1,2,8,10,63,51,0,14,11.2,11.4,88,66,35,90,68,99,12,34,21,24,27,18,28,43,75};

cout << "Masiva predi sortiraneto: " << endl;
print_ar (ar, 25);
cout << endl;
shaker_sort (ar, 25);
cout << endl;
cout << "Masiva sled sortiraneto: " << endl;
print_ar (ar, 25);
system("PAUSE");
return 0;
}
Link to comment
Сподели другаде

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Гост
Отговори на тази тема

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   Не можете да качите директно снимка. Качете или добавете изображението от линк (URL)

Loading...
×
×
  • Създай ново...