How would you handle this question?

Dec 13, 2011 at 7:56pm
Hello,
I am going over practice questions for my final exam for an intro c++ class that I am taking. The one question is:

Write a function named prcoessArray that takes an array of doubles as it's first parameter, an array of values as it's second parameter and the length of the array as it's third parameter. The function should print all values in the array that are less than the second parameter.

How would you go about doing this if you were given this question?
Dec 13, 2011 at 8:51pm
Assuming the 2nd parameter is meant to be a single value instead of an array, the function can be done in a simple loop as following.
1
2
3
4
5
6
void foo(double * ptr, double val, int len)
{
  for (int i = 0; ptr != NULL && i < len; ptr += 8, i++)
    if (*ptr < val)
      cout << *ptr << endl;
}
Dec 14, 2011 at 3:50am
This might be easier to read than wjee's.

1
2
3
4
5
6
void foo(double* arr, double val, int size)
{
  for (int i = 0; arr && i < size; i++)
    if (arr[i] < val)
      cout << arr[i] << endl;
}

Topic archived. No new replies allowed.