I just downloaded a document containing some pointers exercises. The statement of One of its questions is as follows:

Write a function that returns a pointer to the maximum value of an array of double's. If the array is empty, return NULL.
double* maximum(const double* a, int size);


It confused me a little. I am wondering what is meant by an empty array? Does it mean that all elements of the array are 0 or 0.0? Or if it means the number of elements in the array is zero? If the case is second then is it possible to declare such an array?
Or there is some third case??

Sorry for such an awkward question though ..... :icon_confused:

Thanks for your time!!!!!

I just downloaded a document containing some pointers exercises. The statement of One of its questions is as follows:

Write a function that returns a pointer to the maximum value of an array of double's. If the array is empty, return NULL.
double* maximum(const double* a, int size);


It confused me a little. I am wondering what is meant by an empty array? Does it mean that all elements of the array are 0 or 0.0? Or if it means the number of elements in the array is zero? If the case is second then is it possible to declare such an array?
Or there is some third case??

Sorry for such an awkward question though ..... :icon_confused:

Thanks for your time!!!!!

When an array is empty, generally, it means if its size is 0, but under certain context, it could mean something else.

And your right when you say you can't declare a static array with size 0. As narue pointed out, it mean when size <= 0.

Edited 5 Years Ago by firstPerson: n/a

You don't have to actually declare the array to be of size 0 in order for it to be considered empty. The size parameter may just mean the number of elements in the array that have valid entries. The array itself could have been declared with any size > 0. The question is a bit ambiguous about that.

Well ... I am still confused!
How could the size of an array be zero or even less than zero? Doesn't it mean that the array consists of zero or less elements. I am not familiar with any concept like this. Please explain it in a little more detail.

Actually, if the array under consideration is an array of characters than we can consider it an empty array if all of it's elements are null character('\0'). But the problem here is that the array needs to be of doubles.

Or
Do you think that this problem is not clearly defined so it wouldn't be worth solving it???

@Ancient Dragon
No, I don't think that the size parameter represents the number of valid entries in the array. Because, inside the function we will have to use this variable to iterate through the whole array.

How could the size of an array be zero or even less than zero?

The two function parameters are different. Nothing forces callers to give you anything remotely similar to the size of the actual array:

double a[] = {1.0, 2.0, 3.0, 4.0, 5.0};
double *max = maximum(a, -235);

In this case the size of the array is 5, but I passed a completely random and bogus value for the size argument. One might expect a reasonable call to look like this:

double *max = maximum(a, sizeof a / sizeof *a);

But that's not something the function itself can control, so it needs to do a sanity check on the arguments:

double *maximum(const double *a, int size)
{
    assert(a != NULL); // Validate the pointer as best we can

    if (size <= 0)
        return NULL; // "Empty" array

    // ...
}

The array is considered to be empty because the maximum function trusts the caller not to be a complete idiot. As such, the maximum function assumes the size parameter is an accurate representation of the number of elements in the array that the caller wants to process. If that size is less than one, the assumption is that there are no elements.

Do you think that this problem is not clearly defined so it wouldn't be worth solving it???

I think it's defined well enough to come up with a reasonable implementation.

Because, inside the function we will have to use this variable to iterate through the whole array.

And what's meant by the "whole array"? Riddle me this, how do any of the calls to my sum function below not process the "whole array" from the function's perspective:

#include <iostream>

int sum(int a[], int size)
{
    int sum = 0;

    while (--size >= 0)
        sum += a[size];

    return sum;
}

int main()
{
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};

    std::cout<< sum(a, 1) <<'\n';
    std::cout<< sum(a, 2) <<'\n';
    std::cout<< sum(a, 3) <<'\n';
    std::cout<< sum(a, 4) <<'\n';
    std::cout<< sum(a, 5) <<'\n';
    std::cout<< sum(a, 6) <<'\n';
}

Edited 5 Years Ago by Narue: n/a

OK!!!
finally I have come up with this program while keeping in mind the points set by you. Please take a look at it and tell me do you think it is a reasonable implementation??
Have I correctly understood what you were trying to explain??

#include <iostream>
using namespace std;

double* maximum(const double*, int);

int main()
{
	double numbers[10];
	int no_of_elements;
	double *maxnum;
	cout << "Enter 10 integers to find the maximum:\n";
	for (int i=0; i<10; i++)
		cin >> numbers[i];
	cout << "In how many first elements do you want to carry out the search? ";
	cin >> no_of_elements;
	maxnum = maximum(numbers, no_of_elements);
	if (maxnum == 0)
		cout << "The array is ampty!\n";
	else
		cout << "The number with maximum value is " << *maxnum << endl;

	system("pause");
	return 0;
}

double* maximum(const double* a, int size)
{
	double *max=(double*)(a);
	if (size <= 0)
		max = 0;
	else
	{
		max = (double*)a;
		for (int i=1; i<size; i++)
		{
			if (*(a+i) > *max)
				max = (double*)(a+i);
		}
	}
	return max;
}
This article has been dead for over six months. Start a new discussion instead.