have the function largest four (arr) take the array of integers stored in arr and find the four largest elements and return their sum.for example : arr is [4,5,-2,3,1,2,6,6] then the four largest elements in this array are 6,6,4 and 5 and the total sum of these number ia 21 c++ programming
Answers
Answered by
1
Answer:
1234566467560565988834668556
Answered by
0
Explanation:
public int sumOfLargest4(int [ ]a)
{// Sort the Array in Descending order. Exchange Sort is used here
int i, j, temp, n=a.length;
for(i=0; i<n-1; i++)
{for(j=i+1; j<n; j++)
{if(a[i] < a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
// Now return sum of first 4 in the Array
int sum=0;
for(i=0; i<4; i++)
{ sum+=a[i];
}
return sum;
}// End function
Similar questions