im a little bit lost here about the array

im a little bit lost here about the array since i took the intro of c++ 2 years ago. Can someone give me hint of how i should start? im a beginner. thanks!




Write a function named countNum2s that takes as input an array of integers and an integer that specifies how many entries are in the array. The function should return the number of 2’s in the array. Test your function with arrays of different length and with varying number of 2’s.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int countNum2s(int arr[], int size)
{
	int count = 0;//start count at 0
	for (int i = 0; i < size; i++) //count up to size to check for 2's
	{
		if (arr[i] == 2) //if number we're checking is a 2
		{
			count += 1; //add 1 to count
		}
	}
	return count; //return count
}

int main()
{
	int myarray[] = { 2, 3, 1, 2 };
	int numberof2s = countNum2s(myarray, 4);
	std::cout << "Number of 2s: " << numberof2s << std::endl;
	return 0;
}
Topic archived. No new replies allowed.