Advertisement
lopezz

Polje kao parametar [Funkcije 1.zadatak]

May 28th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. typedef unsigned int uint;
  5.  
  6. using namespace std;
  7.  
  8. bool IsNotAnInteger(int n)
  9. {
  10.     if (cin.fail())
  11.     {
  12.         cin.clear();
  13.         cin.ignore(256, '\n');
  14.         return true;
  15.     }
  16.     if (n < 1) return true;
  17.  
  18.     return false;
  19. }
  20.  
  21. uint argv(uint arr[], uint n)
  22. {
  23.     uint sum = 0;
  24.  
  25.     if (n == 1) return arr[--n]+=2;
  26.  
  27.     for (uint i = 0; i < n; i++)
  28.         sum += arr[i];
  29.    
  30.     return sum;
  31. }
  32.  
  33. int main()
  34. {
  35.     uint n;
  36.  
  37.     do
  38.     {
  39.         cout << "Unesite koliko brojeva: ";
  40.         cin >> n;
  41.     }
  42.     while (IsNotAnInteger(n));
  43.  
  44.     uint* arr = new uint[n];
  45.  
  46.  
  47.     for (uint i = 0; i < n; i++)
  48.     {
  49.         if (n==1)
  50.         {
  51.             cout << "Unesi broj: ";
  52.             cin >> arr[i];
  53.         }
  54.        
  55.         else
  56.         {
  57.             cout << "Unesi " << i + 1 << ". broj: ";
  58.             cin >> arr[i];
  59.         }
  60.     }
  61.  
  62.  
  63.     cout << argv(arr, n) << endl;
  64.  
  65.  
  66.     system("pause");
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement