Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, Write a function called fillArray that will fill an array of any size wi

ID: 3704340 • Letter: I

Question

In C++, Write a function called fillArray that will fill an array of any size with random numbers in the range of 1 - 100.

Write a separate function called printArray that will print an array of any size.

Write a third function called countEvens which will count and return the number of even numbers in the array.

In main create an array that holds 25 ints. Use your functions to fill, print, and count the number of even numbers. You should

Fill the array

Print the array

Print the number of even numbers in the array

What not to do

Using any of the following will drop your grade for this assignment by 70%

global variables

cin in any funciton other than main

cout in any funciton other than main and printArray

goto statements

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void fillArray (int a[], int n) {
for(int i=0;i<n;i++) {
a[i]=rand() % 100 + 1;
}
}

void printArray(int a[], int n) {
cout<<"Array elements: "<<endl;
for(int i=0;i<n;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
int countEvens(int a[], int n) {
int count = 0;
for(int i=0;i<n;i++) {
if(a[i]%2 == 0) {
count++;
}
}
return count;
}
int main()
{
srand (time(NULL));
int a[25];
fillArray(a, 25);
printArray(a,25);
cout<<"Number of evens: "<<countEvens(a,25)<<endl;
return 0;
}

Output: