Advertisement
neogz

unos ispis i suma niza rekurzija

Apr 30th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void unosREK  (int niz[], int max)
  5. {
  6.     if (max <= 0) cin >> niz[0];
  7.     else
  8.     {
  9.         cin >> niz[max];
  10.         unosREK(niz, max - 1);
  11.     }
  12. }
  13. void ispisREK (int niz[], int max)
  14. {
  15.     if (max <= 0) cout << niz[0];
  16.     else
  17.     {
  18.         cout << niz[max] << "\t";
  19.         ispisREK(niz, max - 1);
  20.     }
  21.     cout << endl;
  22. }
  23. int  sumaREK  (int niz[], int max)
  24. {
  25.     int sum = 0;
  26.     if (max <= 0) sum += niz[0];
  27.     else
  28.     {
  29.        
  30.         return sum += niz[max] + sumaREK(niz, max - 1);
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     const int max = 7;
  37.     int niz[max];
  38.    
  39.     cout << "******** UNOS ELEMENATA ********  "    << endl << endl;
  40.     unosREK(niz, max-1);
  41.  
  42.     cout << "******** ISPIS ELEMENATA ********  "   << endl << endl;
  43.     ispisREK(niz, max-1);
  44.  
  45.     cout << "******** SUMA ELEMENATA ********  "    << endl << endl;
  46.     cout << sumaREK(niz, max-1);
  47.  
  48.  
  49.  
  50.     system("pause > null");
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement