Advertisement
neogz

REK - Niz operacije

Aug 21st, 2014
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. /*
  2. Napišite program koji će omogućiti kreiranje niza od 7 cijelih brojeva, te uz pomoć rekurzivnih funkcija omogućiti:
  3. unos elemenata,
  4. ispis elemenata,
  5. izračunati sumu svih elemenata niza,
  6. sumu pozitivnih elemenata niza,
  7. sumu elemenata niza s parnim indeksom.
  8. */
  9.  
  10. #include <iostream>
  11. using namespace std;
  12.  
  13. void unos(int niz[], int max)
  14. {
  15.  
  16.     if (max == 0)
  17.         cin >> niz[max];
  18.     else
  19.     {
  20.         cin >> niz[max];
  21.         unos(niz, max - 1);
  22.     }
  23. }
  24. void ispis(int niz[], int max)
  25. {
  26.     if (max == 0)
  27.         cout << niz[max] << endl;
  28.     else
  29.     {
  30.         cout << niz[max] << "\t";
  31.         ispis(niz, max - 1);
  32.     }
  33.  
  34. }
  35. int suma_elemenata(int niz[], int max)
  36. {
  37.     if (max == 0) return niz[max];
  38.     return niz[max] + suma_elemenata(niz, max - 1);
  39. }
  40. int suma_poz_elemenata(int niz[], int max)
  41. {
  42.     if (max <= 0)
  43.     {
  44.         if (niz[max] % 2 == 0) return niz[max];
  45.         else return 0;
  46.     }
  47.     else
  48.     {
  49.         if (niz[max] % 2 == 0) return niz[max] + suma_poz_elemenata(niz, max - 1);
  50.         else return suma_poz_elemenata(niz, max - 1);
  51.     }
  52. }
  53. int suma_paran_index(int niz[], int max)
  54. {
  55.     if (max == 0) return niz[max];
  56.     else
  57.     {
  58.         if (max % 2 == 0) return niz[max] + suma_paran_index(niz, max - 1);
  59.         else return suma_paran_index(niz, max - 1);
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     const int max = 7;
  66.     int niz[max];
  67.  
  68.     cout << "Unesi clanove niza: \n";
  69.     unos(niz, max - 1);
  70.  
  71.     cout << "\nClanovi niza su: \n";
  72.     ispis(niz, max - 1);
  73.  
  74.     cout << "\nSuma clanova niza je: " << suma_elemenata(niz, max - 1) << endl;
  75.     cout << "Suma pozitivnih clanova niza je: " << suma_poz_elemenata(niz, max - 1) << endl;
  76.     cout << "Suma clanova niza sa parnim indexom je: " << suma_paran_index(niz, max - 1) << endl;
  77.  
  78.     system("pause>null");
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement