C++ Programming/Exercises/Static arrays

From Wikibooks, open books for an open world
Jump to navigation Jump to search


  1. include <iostream>

using namespace std;

void enterNumbers(int arr[], int n) {

   cout << "Enter " << n << " numbers:\n";
   for (int i = 0; i < n; ++i) cin >> arr[i];

}

void countAndTotal(int arr[], int n, int& pos, int& neg, int& total) {

   pos = neg = total = 0;
   for (int i = 0; i < n; ++i) {
       if (arr[i] > 0) pos++;
       else if (arr[i] < 0) neg++;
       total += arr[i];
   }

}

int main() {

   int n, arr[100], pos, neg, total;
   cout << "Enter the number of elements: ";
   cin >> n;
   enterNumbers(arr, n);
   countAndTotal(arr, n, pos, neg, total);
   cout << "Positives: " << pos << "\nNegatives: " << neg 
        << "\nTotal: " << total << endl;
   return 0;

}