In: Computer Science
In C++
Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature:
void printSome(const int array[], int size);
You do not need to demonstrate calling this function from main().
Hi,
Hope you are doing fine. I have written the above code in C++. The function has been written with the same signature. However, ihave written some additional code such i.e main function in order to test and demonstrate the output of printSome() function. You may avoid it if you do not need it. The program has been explained using comments which have been highlighted in bold.
Program:
#include <iostream>
using namespace std;
//function printSome takes two parameters: an array of
ints and an int which gives size of array
void printSome(const int array[], int size){
//traversing through the elements of
array
for(int i=0;i<size;i++)
{
//if element is greater
than 5 then
if(array[i]>5)
//print that element. I
have added a space after printing the element so that the output
looks neat.
cout << array[i] <<"
";
}
}
//Additional code to test if printSome() is
working
int main()
{
int arr[]={2,5,8,9,7,1,0,2,6,3};
int size=10;
//calling printSome() by passing an array of
ints and an int size
printSome(arr,size);
}
Executable code snippet of the code:
Output: