1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
using namespace std;
void oddOrEven(int arr[], int size, int& sumOfEvens, int& sumOfOdds);
int main(){
int arr[5] = { 1, 2, 3, 4, 5 };
int sumOfEvens = 0, sumOfOdds = 0;
oddOrEven(arr, 5, sumOfEvens, sumOfOdds);
std::cout << "Sum of evens: " << sumOfEvens << "\nSum of odds: " << sumOfOdds << endl;
return 0;
}
void oddOrEven(int arr[], int size, int& sumOfEvens, int& sumOfOdds){
for (int i = 0; i < size; i++)
{
if (arr[i] % 2 == 0) // if it is even
{
sumOfEvens += arr[i]; // add it to the sum of the even numbers
}
else
{
sumOfOdds += arr[i]; // otherwise, put it in the sum of odd numbers
}
}
}
|