Question: Write a program that takes an input array of integers and returns the sum of all the positive numbers in the array.
Requirements:
sumPositiveNumbers
that takes two parameters: an integer array (arr
) and its size (size
).Example Output:
Enter the size of the array: 5
Enter the elements of the array:
Element 0: -1
Element 1: 4
Element 2: 0
Element 3: 7
Element 4: -3
Sum of positive numbers: 11
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
vector<int> arr;
int size;
cout << "Enter the size of the array: ";
cin >> size;
int number;
int total;
for(int i = 0; i < size; i++){
cout << "Element " << i <<": ";
cin >> number;
arr.push_back(number);
}
for(int i = 0; i < size; i++){
if(arr[i] > 0){
total += arr[i];
}
}
cout << "Sum of positive numbers: " << total;
}