ToniDev

Ex 2 \ Pb diverse

Oct 23rd, 2023
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int getMaxFromArray(int array[], int n)
  5. {
  6.     int max = array[1];
  7.     int poz = 1;
  8.     for (int i = 1; i <= n; i++)
  9.     {
  10.         if (array[i] > max) {
  11.             max = array[i];
  12.             poz = i;
  13.         }
  14.     }
  15.  
  16.     return poz;
  17. }
  18.  
  19. void getMinFromArray(int array[], int n, int& result)
  20. {
  21.     int min = array[1];
  22.     int poz = 1;
  23.     for (int i = 1; i <= n; i++)
  24.     {
  25.         if (array[i] < min) {
  26.             min = array[i];
  27.             poz = i;
  28.         }
  29.     }
  30.  
  31.     result = poz;
  32. }
  33.  
  34. void swap(int& a, int& b)
  35. {
  36.     int aux = a;
  37.     a = b;
  38.     b = aux;
  39. }
  40.  
  41. void afisare(int vector[], int pozInit, int pozFin)
  42. {
  43.     for (int i = pozFin; i <= pozInit; i++)
  44.     {
  45.         cout << vector[i] << " ";
  46.     }
  47.  
  48.     cout << endl;
  49. }
  50.  
  51. void sortare(int vector[], int pozInit, int pozFin)
  52. {
  53.     for (int i = pozInit; i <= pozFin - 1; i++)
  54.         for (int j = i + 1; j <= pozFin; j++)
  55.             if (vector[i] < vector[j])
  56.                 swap(vector[i], vector[j]);
  57. }
  58.  
  59. int main()
  60. {
  61.     int vector[10], n, pozInit, pozFin;
  62.  
  63.     cout << "n = "; cin >> n;
  64.  
  65.     for (int i = 1; i <= n; i++)
  66.     {
  67.         cout << "vector[" << i << "] = ";
  68.         cin >> vector[i];
  69.     }
  70.  
  71.     pozFin = getMaxFromArray(vector, n);
  72.     getMinFromArray(vector, n, pozInit);
  73.     sortare(vector, pozInit, pozFin);
  74.  
  75.     cout << "Poz init: " << pozInit << endl;
  76.     cout << "Poz fin: " << pozFin << endl;
  77.     afisare(vector, pozInit, pozFin);
  78.  
  79.     return 0;
  80. }
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment