C++ Homework

I am a beginner at coding, and was hoping someone could help me out with an assignment. These are the instructions:

Write a function called fillArray that will fill an array of any constantly declared array size variable with random numbers in the range of 1 - 100.

Write a 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 an automatic 10 points:
global variables
cin in any function other than main
cout in any function other than main and printArray
goto statements

This is all the code I have so far, I just have no idea how to write the countEvens function. Any and all help would be greatly appreciated :)

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
#include <iostream>
#include <ctime>
using namespace std;

void printArray(int arr[], int size);

int main()
{
	srand(time(0));
	const int size = 25;
	int fillArray[size];

	cout << endl;
	for (int i = 0; i < size; i++)
	{
		fillArray[i] = rand() % 99 + 1;
	}

	printArray(fillArray, size);

	return 0;
}

void printArray(int arr[], int size)
{
	cout << "Printing the array" << endl;
	for (int i = 0; i < size; i++)
	{
		cout << arr[i] << endl;
	}
	cout << "There are even numbers in the array" << endl;
}
Hey so for the countsEvens. think about that makes an even number even? and the If statement that can help you with that. at this point should should also know about counters and how you can use them to keep track of the number of times a loop or condition runs.
Last edited on
you can use modulus operator to figure out which ones are even and then count them!
I also think you need to use the #include <stdlib.h> library. on line 13 you can a
 
cout << endl;

that you dont really need.
Topic archived. No new replies allowed.