Facebook
From Gruff Frog, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 186
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <random>
  5. //max liczba parzysta z vectora
  6. using namespace std;
  7.  
  8. int modul(int liczba)
  9. {
  10.         if (liczba < 0)
  11.         {
  12.                 liczba *= -1;
  13.         }
  14.  
  15.         return liczba;
  16. }
  17.  
  18. int losowaLiczba(int lmin, int lmax)
  19. {
  20.         return rand() % modul(lmax - lmin) + lmin;
  21. }
  22.  
  23. vector<int>losowyVector(int lmin, int lmax, int dlugosc)
  24. {
  25.         vector<int>wynik(dlugosc);
  26.  
  27.         for (int i = 0; i < dlugosc; i++)
  28.         {
  29.                 wynik[i] = losowaLiczba(lmin, lmax);
  30.         }
  31.  
  32.         return wynik;
  33. }
  34.  
  35. int max(vector<int>tab)
  36. {
  37.         int c = tab[0];
  38.  
  39.         for (int i = 1; i < tab.size(); i++)
  40.         {
  41.                 if (c < tab[i])
  42.                 {
  43.                         c = tab[i];
  44.                 }
  45.         }
  46.  
  47.         return c;
  48. }
  49.  
  50. bool CzyParzysta(int liczba)
  51. {
  52.         return liczba % 2 == 0;
  53. }
  54.  
  55. int maxParzysta(int lmin, int lmax, int dlugosc)
  56. {
  57.  
  58.         vector<int>tab = losowyVector(lmin, lmax, dlugosc);
  59.         vector<int>parzystyTab;
  60.        
  61.         for (int i = 0; i < tab.size(); i++)
  62.         {
  63.                 if (CzyParzysta(tab[i]))
  64.                 {
  65.                         parzystyTab.push_back(tab[i]);
  66.                 }
  67.         }
  68.  
  69.         int c = max(parzystyTab);
  70.  
  71.         return c;
  72. }
  73.  
  74.  
  75.  
  76. int main()
  77. {
  78.         cout << maxParzysta(100, 1000, 10);
  79. }