Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6. int losowa(int przedzial);
  7. int wypisz(int x);
  8.  
  9. int main()
  10. {
  11.     int przedzial;
  12.     int i = 0;
  13.     int suma = 0;
  14.     const int n = 5;
  15.     int random[n];
  16.    
  17.     cout << "Program dodajacy 5 losowych liczb z wybranych przedzialow\n\n";
  18.  
  19.     for (i; i < n; i++)
  20.     {
  21.             cout << "Wybierz przedzial:\n\n [1] 1-100\n [2] 101-200\n [3] 201-300\n" << endl;
  22.             cin >> przedzial;
  23.             cout << "Wybrano przedzial: " << przedzial << "\n" << endl;
  24.  
  25.             if ((przedzial > 0) && (przedzial < 4))
  26.         {
  27.             cout << "Wylosowano: " << losowa(przedzial) << "\n\n";
  28.             suma = suma + losowa(przedzial);
  29.             cout << "Suma po " << i + 1 << " losowaniu = " << suma << "\n\n";
  30.             random[i] = losowa(przedzial);
  31.         }
  32.                  
  33.         else
  34.         {
  35.                    cout << "Nie ma takiego przedzialu" << endl;
  36.                    cout << "Suma nie ulegla zmianie = " << suma;
  37.                    cout << "\n\n";
  38.                    i = i - 1;
  39.         }
  40.        
  41.     }
  42.  
  43.     cout << "Wylosowane liczby: " << endl;
  44.     for (i = 0; i < n; i++)
  45.     {
  46.         cout << random[i];
  47.         if(i != n - 1)
  48.         {
  49.             cout << ", ";
  50.         }
  51.     }
  52.     cout << "\n\n";
  53.     cout << "Suma wszystkich losowan = " << suma << "\n\n";
  54.  
  55.     system("pause");
  56.     return 0;
  57. }
  58.  
  59. int losowa(int przedzial)
  60. {
  61.     srand(time(NULL));
  62.  
  63.     if (przedzial == 1)
  64.     {
  65.         return (rand() % 100) + 1;
  66.     }
  67.     else if (przedzial == 2)
  68.     {
  69.         return (rand() % 100) + 100;
  70.     }
  71.     else if (przedzial == 3)
  72.     {
  73.         return (rand() % 100) + 200;
  74.     }
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement