Implement a function that finds the number of values that are less than 0 and the number of values greater than 0 stored in an array of integers. Assume the array is the partially filled array from the previous question, and uses 0 to indicate the end of values stored in it. (18 pts) Note: the results should be passed back to the caller, not directly displayed in the function. Also note, as the following code template requires, the function’s return type is void. Please finish the function header, and function implementation. Hint.. In the beginning, set the lessThan and greaterThan values to be 0. Then as you scan through the array elements one by one, compare the next array element with 0. If the element is greater, update your greaterThan count; otherwise, if the element is less, update the lessThan count, otherwise do nothing. Continue until you see the value 0. GIVEN CODE: #include using namespace std; // Find the number of values stored that is less than 0 and the number greater than // 0 in a partially filled array of int, which uses the special sentinel value 0 to // indicate the end of the array // @param a: int array // @param less: // @param greater: void findLessAndGreater (/* TODO: declare the parameters */) { //To do: implement the function ... } int main() { int a[] = { -15, 23, 32, -12, 10, 9, -43, 5, -999, 0 }; int less, greater; // ToDo: Call your function // Print the results cout << "Number of values less than 0 is " << less << endl; cout << "Number of values greater than 0 is " << greater << endl; return 0; }
Implement a function that finds the number of values that are less than 0 and the number of values greater than 0 stored in an array of integers. Assume the array is the partially filled array from the previous question, and uses 0 to indicate the end of values stored in it. (18 pts)
Note: the results should be passed back to the caller, not directly displayed in the function. Also note, as the following code template requires, the function’s return type is void.
Please finish the function header, and function implementation.
Hint.. In the beginning, set the lessThan and greaterThan values to be 0. Then as you scan through the array elements one by one, compare the next array element with 0. If the element is greater, update your greaterThan count; otherwise, if the element is less, update the lessThan count, otherwise do nothing. Continue until you see the value 0.
GIVEN CODE:
#include <iostream>
using namespace std;
// Find the number of values stored that is less than 0 and the number greater than
// 0 in a partially filled array of int, which uses the special sentinel value 0 to
// indicate the end of the array
// @param a: int array
// @param less:
// @param greater:
void findLessAndGreater (/* TODO: declare the parameters */)
{
//To do: implement the function ...
}
int main()
{
int a[] = { -15, 23, 32, -12, 10, 9, -43, 5, -999, 0 };
int less, greater;
// ToDo: Call your function
// Print the results
cout << "Number of values less than 0 is " << less << endl;
cout << "Number of values greater than 0 is " << greater << endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images