pborawski

funkcje tablice losowanie

Jul 16th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. int wylosuj( int start, int stop )
  6. {
  7.    
  8.     return rand()%( ( stop - start ) + 1 ) + start;
  9. }
  10. void losujTablice( int t[], int dlugosc, int start, int stop )
  11. {
  12.     srand( time( NULL ) );
  13.     int tmp = 0;
  14.     do
  15.     {
  16.         t[ tmp ] = wylosuj( start, stop );
  17.         tmp++;
  18.     } while( tmp <= dlugosc );
  19. }
  20. void wypiszTablice( int t[], int dlugosc )
  21. {
  22.     int tmp = 0;
  23.     do
  24.     {
  25.         std::cout << "Liczba z tablicy o indeksie: " << tmp << " to: " << t[ tmp ] << std::endl;
  26.         tmp++;
  27.     } while( tmp <= dlugosc );
  28. }
  29. int obliczSume( int t[], int dlugosc )
  30. {
  31.     int tmp = 0, suma = 0;
  32.     do
  33.     {
  34.         suma += t[ tmp ];
  35.         tmp++;
  36.     } while( tmp <= dlugosc );
  37.     return suma;
  38. }
  39. int main()
  40. {
  41.     int liczby[ 999 ];
  42.     losujTablice( liczby, 999, 4, 10 );
  43.     wypiszTablice( liczby, 999 );
  44.     int iSuma = obliczSume( liczby, 999 );
  45.     std::cout << "Suma to: " << iSuma << std::endl;
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment