In: Computer Science
Write a C++ function which takes an int array and its size as parameters. It returns an int indicating how many multiples of 3 are contained in the array. For example, if the array contains {2, 6, 8} your function should return 1. If the array contains {3, 10, 5, 6} your function should return 2. Here is the function prototype:
// int array[]: array to search // size: number of elements in the array int countMult(const int array[], int size);
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include<iostream>
using namespace std;
/*function prototype*/
int countMult(const int array[],int size);
/*main function*/
int main()
{
/*variables*/
int i,size,count;
/*read size from the user*/
cout<<"Enter the size of an array: ";
cin>>size;
/*declare array*/
int array[size];
/*read elements from the user*/
cout<<"Enter the elements:"<<endl;
for(i=0;i<size;i++)
cin>>array[i];
/*function call*/
count=countMult(array,size);
/*print count*/
cout<<"The number of elements are multiples of 3
are: "<<count;
return 0;
}
/*function definition*/
int countMult(const int array[],int size)
{
int c=0;
for(int i=0;i<size;i++)
{
/*check for multiple of 3 and
increment count if true*/
if(array[i]%3==0)
c++;
}
/*return count*/
return c;
}