9
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 can’t find anything saying that this is functional.
Follows 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;
}
To return more than one value, you must return an array or a Pointer.
– Bruno
Does the first part have any relation to the second part? Explain better why the second part is important. You are talking about this
return (low, high, Receita[low].Chave)
?– Maniero
If you create a struct (composite data type) you can use it to package multiple values in function return.
– hugomg
Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?
– Maniero