Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napišite program koji će omogućiti kreiranje niza od 7 cijelih brojeva, te uz pomoć rekurzivnih funkcija omogućiti:
- unos elemenata,
- ispis elemenata,
- izračunati sumu svih elemenata niza,
- sumu pozitivnih elemenata niza,
- sumu elemenata niza s parnim indeksom.
- */
- #include <iostream>
- using namespace std;
- void unos(int niz[], int max)
- {
- if (max == 0)
- cin >> niz[max];
- else
- {
- cin >> niz[max];
- unos(niz, max - 1);
- }
- }
- void ispis(int niz[], int max)
- {
- if (max == 0)
- cout << niz[max] << endl;
- else
- {
- cout << niz[max] << "\t";
- ispis(niz, max - 1);
- }
- }
- int suma_elemenata(int niz[], int max)
- {
- if (max == 0) return niz[max];
- return niz[max] + suma_elemenata(niz, max - 1);
- }
- int suma_poz_elemenata(int niz[], int max)
- {
- if (max <= 0)
- {
- if (niz[max] % 2 == 0) return niz[max];
- else return 0;
- }
- else
- {
- if (niz[max] % 2 == 0) return niz[max] + suma_poz_elemenata(niz, max - 1);
- else return suma_poz_elemenata(niz, max - 1);
- }
- }
- int suma_paran_index(int niz[], int max)
- {
- if (max == 0) return niz[max];
- else
- {
- if (max % 2 == 0) return niz[max] + suma_paran_index(niz, max - 1);
- else return suma_paran_index(niz, max - 1);
- }
- }
- int main()
- {
- const int max = 7;
- int niz[max];
- cout << "Unesi clanove niza: \n";
- unos(niz, max - 1);
- cout << "\nClanovi niza su: \n";
- ispis(niz, max - 1);
- cout << "\nSuma clanova niza je: " << suma_elemenata(niz, max - 1) << endl;
- cout << "Suma pozitivnih clanova niza je: " << suma_poz_elemenata(niz, max - 1) << endl;
- cout << "Suma clanova niza sa parnim indexom je: " << suma_paran_index(niz, max - 1) << endl;
- system("pause>null");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement