0
I did this right now. The input is the size of the array, and then the numbers. For example,
3
2
4
6
This was my attempt at the code:
#include <iostream>
using namespace std;
/*
Function for average of even numbers in array.
*/
double averageEvens(int array[], int size){
int total = 0;
int counter = 0;
for(int i = 0; i < size; i++){
if((array[i] % 2) == 0){
total += array[i];
counter++;
}
}
return (double)total/counter;
}
int main() {
int arraySize;
cout << "How many numbers? ";
cin >> arraySize;
cout << arraySize << endl;
int array[arraySize];
for(int i = 0; i < arraySize; i++){
cout << "Number " << i+1 << ": ";
cin >> array[i];
cout << array[i] << endl;
}
cout << "All numbers set." << endl;
cout << "The average of the even numbers: " << averageEvens(array, arraySize) << endl;
return 0;
}