ChameL1oN

Лаба5_Задача1(Правильно)

Dec 10th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <locale>
  3. #include <fstream>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int* form_mas(int count){
  11. int* massive = new int[count];
  12. int a=0;
  13. time_t t;
  14. srand((unsigned)time(&t));
  15. while (a < count){
  16. massive[a] = rand();
  17. cout << "massive [" << a << "] = " << massive[a] << endl;
  18. a++;
  19. }
  20. return massive;
  21. }
  22. void init_mas(int massive[],int count){
  23. int a = 0;
  24. for (int i = -1; i < count; i++){
  25. if ((count - 1) % 2 == 0){
  26. for (int j = count - 1; j >= i; j -= 2){
  27. if (massive[j] < massive[j - 2])
  28. {
  29. int r = massive[j];
  30. massive[j] = massive[j - 2];
  31. massive[j - 2] = r;
  32. }
  33. }
  34. }
  35. else {
  36. for (int j = count - 2; j >= i; j -= 2){
  37. if (massive[j] < massive[j - 2])
  38. {
  39. int r = massive[j];
  40. massive[j] = massive[j - 2];
  41. massive[j - 2] = r;
  42. }
  43. }
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. }
  52. void print_mas(int massive[], int count){
  53. cout << "Массив после обработки" << endl;
  54. for (int i = 0; i <= count - 1; i++){
  55. cout << "massive [" << i << "] = " << massive[i] << endl;
  56. }
  57. }
  58.  
  59. void main(){
  60. int num, k;
  61. setlocale(LC_ALL, "rus");
  62. cout << "Введите кол-во элементов в массиве ";
  63. cin >> num;
  64. int* mas = form_mas(num);
  65. init_mas(mas, num);
  66. print_mas(mas, num);
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment