Is it possible to return multiple values? For example:
umafuncao()
{
int x = 1, y = 2;
return x,y;
}
void main()
{
int a, b;
a, b = umafuncao();
}
I'm asking this question, because I built a code using this structure for a task and passed all the tests, but I find nothing saying that this is functional.
Follow the code:
int findMaxCrossingSubArray(TItem *Receita, int low, int mid, int high)
{
int i, j, maxLeft, leftSum = -100000, maxRight, rightSum = -1000000;
int sum = 0;
for( i = mid; i >= low; i--)
{
sum = sum + Receita[i].Chave;
if( sum > leftSum)
{
leftSum = sum;
maxLeft = i;
}
}
sum = 0;
for(j = mid + 1; j <= high; j ++)
{
sum = sum + Receita[j].Chave;
if(sum > rightSum)
{
rightSum = sum;
maxRight = j;
}
}
return (maxLeft, maxRight, leftSum + rightSum);
}
int findMaxSubArray(TItem *Receita, int low, int high)
{
int mid;
int leftLow, leftHigh, leftSum;
int rightLow, rightHigh, rightSum;
int crossLow, crossHigh, crossSum;
if(high == low)
{
return (low, high, Receita[low].Chave);
}
else
{
mid = (low + high)/2;
leftLow, leftHigh, leftSum = findMaxSubArray(Receita, low, mid);
rightLow, rightHigh, rightSum = findMaxSubArray(Receita, mid+1, high);
crossLow, crossHigh, crossSum = findMaxCrossingSubArray(Receita, low, mid, high);
if((leftSum >= rightSum) && (leftSum >= crossSum))
return (leftLow, leftHigh, leftSum);
else if((rightSum >= leftSum) && (rightSum >= crossSum))
return (rightLow, rightHigh, rightSum);
else
return (crossLow, crossHigh, crossSum);
}
}
int main()
{
TItem *Receita;
int low = 0, ndias, *taxa, lucro;
taxa = malloc(sizeof(int));
ndias = Carrega(&Receita, taxa);
//Aqui começa a lógica para calculo do lucro maximo:
low, ndias, lucro = findMaxSubArray(Receita, low, ndias-1);
printf("%d", lucro > 0 ? lucro : 0);
Libera(&Receita);
return 0;
}