Lovemore Kutseya

Lovemore Kutseya

  • NA
  • 21
  • 3.4k

passing parameters with ref

Jan 4 2019 5:40 AM
write the method called currentLargestSum which takes an array of integers and another integer result, passed using the ref keyword.
If the sum of the numbers in the array is larger than the current result then assign the sum to the result. Otherwise leave result unchanged
 
using System;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
int largestSum = 0;
int[] numbers1 = { 10, 12, 3, 2, 45, 6 };
currentLargestSum(numbers1, ref largestSum);
string result = largestSum + ", ";
int[] numbers2 = { 10, 1, 3, 2, 45, 6 };
currentLargestSum(numbers2, ref largestSum);
result += largestSum + ", ";
int[] numbers3 = { 104, 132, 13, 2, 45, 6 };
currentLargestSum(numbers3, ref largestSum);
result += largestSum;
Console.WriteLine(result);
}
}
}

 

Answers (2)