Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // unos pomoću rekurzije
- 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]<<"\t";
- else {
- cout << niz[max] << "\t";
- ispis(niz, max - 1);
- }
- }
- //suma neparnih elemenata
- int sne(int niz[], int max)
- {
- // bazni slucaj tj, ako je brojac < 0 vrati nulu
- if (max < 0)return 0;
- else { //ako je neparan saberi
- if (niz[max] % 2 == 1) return niz[max] + sne(niz, max - 1);
- else return sne(niz, max - 1);
- }
- }
- //suma elemenata sa neparnim indexom
- int sni(int niz[], int max)
- {
- if (max == 0)return 0;
- else{
- if (max % 2 == 0)return sni(niz, max - 1);
- else return niz[max] + sni(niz, max - 1);
- }
- }
- int main()
- {
- const int max = 7;
- int niz[max];
- cout << "Unesite vrijednosti vaseg niza: \n";
- unos(niz,max-1);
- cout << endl;
- cout << "Vas niz je: \n";
- ispis(niz, max - 1);
- cout << endl;
- cout << "Suma neparnih elemenata niza je: " << sne(niz, max-1) << endl;
- cout << "Suma elemenata sa neparnim indexom je: " << sni(niz, max - 1) << endl;
- system("pause >nul");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment