Specify the cycle
-
#include <iostream> #include <cmath> #include <windows.h> #include <stdlib.h> #include <iomanip> using namespace std; int main(){ srand(time(NULL)); const int n = 22; float Arr[n]; for (int i = 0; i < n; i++) { Arr[i] = (rand() % 200 - 99)-(float)(rand() % 201) / 100.0; cout << fixed << setprecision(2) << " " << Arr[i]; } cout << endl; for (int i=1; i < n - 1; i += 2){ //сортировка for (int j = 1; j < n - i - 1; j += 2){ if (Arr[j] < Arr[j+2]){ swap(Arr[j], Arr[j+2]); } } } for (int i = 0; i < n; i++) { cout << " " << Arr[i]; } cout << endl; system("pause"); }
Hello, everyone! There's such a prophecy, it generates 22 numerals for the mass. Then, in the cycle, the idea is to place elements that are standing on steam (as I understand the elements that are standing on steamers with non-parallel indices) in order to decrease, while other elements remain in place. A bubble class.
First question, is it correctly written?
And second, please, who can decode the cycle, write how it works.
Everyone will be very grateful for your help.
-
There's a lot of questions to the code. I don't know why there's any windows.h?
system
from there, not a portable function even within the Windows platform, should be used from stdlib. And it's not worth it to use for such purposes, it's called some kind of foreign program that one admin knows who put it.The calculation cycle is simulated by extra-routine operations and reverse passage in the applied cycle when the bubble is sorted. Measured turf or beard?
(int i=1; i < n - 1; i += 2)
The initial value of i = 1 shall be compared each time to n-1 and shall be increased by 2.(int j = 1; j < n - i - 1; j += 2)
The bypass begins with 1 and ends on the element of the residual mass on i.if (Arr[j] < Arr[j+2]) swap(Arr[j], Arr[j+2])
"shoots" a smaller element at the end of the body.Here's a few different versions of the programme.
#include <iostream> #include <cmath> #include <cstdlib> // В С++ содержимое stdlib.h не определено, определен cstdlib #include <iomanip>
int main ()
{
using namespace std; // Правило хорошего тона - using namespace в глобальном
// пространстве не использовать
srand (time (NULL)); // Текущее время для ГСЧ. я даже затрудняюсь сказать - зачем??
const int n = 22;
// тестовый случай для проверки
float Arr[n] = { 31, 2, 32, 1, 33, 3, 34, 4, 35, 5, 36, 7, 37, 11, 38, 9, 39, 8, 40, 6, 41, 10};for (int i = 0; i < n; i++) {
//Arr[i] = (rand () % 200 - 99) - (float) (rand () % 201) / 100.0;
cout << fixed << setprecision (2) << " " << Arr[i];
}
cout << endl;// Классическая запись "пузырька"
for (int i = 1, endi = n-1; i < endi; i += 2) { // от 1 с шагом 2
for (int j = i + 2; j < endi; j += 2) { // от значения , следующего за i-м
if (Arr[i] < Arr[j]) {
swap (Arr[i], Arr[j]);
}
}
}for (int i = 0; i < n; i++) {
cout << " " << Arr[i];
}
cout << endl;// можно обойтись без system
cout << "Press Enter to exit";
cin >> noskipws;
return EXIT_SUCCESS;
}
Conclusion:
33.00 2.00 31.00 1.00 33.00 3.00 34.00 4.00 35.00 5.00 36.00 7.00 37.00 11.00 38.00 9.00 39.00 8.00 40.00 6.00 41.00 10.00
33.00 11.00 31.00 9.00 33.00 8.00 34.00 7.00 35.00 6.00 36.00 5.00 37.00 4.00 38.00 3.00 39.00 2.00 40.00 1.00 41.00 10.00
Press Enter to exit
Pleasing the programme better to use predetermined cases before starting the event of generosity is so simple. Better get the program to take user's input.