The first one is correct.
For the second, there are an infinite number of solutions :), but that would be mine:
Week Week::highestSalesWeek(Week aYear[52])
{
Week max = aYear[0];
for(int i = 1; i < 52; i++)
{
if (aYear[i].getSales() > max.getSales()) max = aYear[i];
}
return max;
}
If max is a link, you change the first aYear element every time:
max = aYear[i]
Alternatively, you can use the pointer to return the link for the week:
Week & Week::highestSalesWeek(Week aYear[52])
{
Week* max = &aYear[0];
for(int i = 1; i < 52; i++)
{
if (aYear[i].getSales() > max->getSales()) max = &aYear[i];
}
return *max;
}
source
share