using System;
using System.IO;
namespace ProfitCalculator
{
public class RangeFinder
{
public static void MaxSum1(double[] data, out int bestStart,
out int bestEnd, out double bestTotal, out int loops)
{
bestTotal = 0;
bestStart = 0;
bestEnd = 0;
loops = 0;
for
(int i = 0; i < data.Length; i++)
{
Console.WriteLine("Value {0}", data[i]);
}
}
public static void MaxSum2(double[] data, out int bestStart,
out int bestEnd, out double bestTotal, out int loops)
{
bestTotal = 0;
bestStart = 0;
bestEnd = 0;
loops = 0;
}
public static void MaxSum3(double[] data, out int bestStart,
out int bestEnd, out double bestTotal, out int loops)
{
bestTotal = 0;
bestStart = 0;
bestEnd = 0;
loops = 0;
}
class Test
{
static void Main()
{
double[] data;
int bestStart, bestEnd;
double bestTotal;
int loops;
string filename = "week52.txt";
int items = 52;
data = new double[items];
try
{
TextReader textIn = new StreamReader(filename);
for (int i = 0; i < items; i++) {
string line = textIn.ReadLine();
data[i] = double.Parse(line);
}
textIn.Close();
}
catch
{
Console.WriteLine("File Read Failed");
return;
}
RangeFinder.MaxSum1(
data, out bestStart, out bestEnd, out bestTotal, out loops);
Console.WriteLine("Start : {0} End : {1} Total {2} Loops {3}",
bestStart, bestEnd, bestTotal, loops);
RangeFinder.MaxSum2(
data, out bestStart, out bestEnd, out bestTotal, out loops);
Console.WriteLine("Start : {0} End : {1} Total {2} Loops {3}",
bestStart, bestEnd, bestTotal, loops);
RangeFinder.MaxSum3(
data, out bestStart, out bestEnd, out bestTotal, out loops);
Console.WriteLine("Start : {0} End : {1} Total {2} Loops {3}",
bestStart, bestEnd, bestTotal, loops);
the question is how to decipher this the following pseudo code:
For every possible start position // every array index in turn
For every possible end position // rest of array from current start
{ Set subtotal to 0 For every value in subseq // between current start and end Add profit value to subtotal
Update subseq info when subtotal exceeds current best total
}
Initially you can ignore the loops variable shown as an output parameter of the MaxSum method, and just concentrate on creating a program which returns the bestStart, bestEnd and bestTotal values. Note that you can use the Length property of the data array to determine how large it is
im struggling to understand or decipher this can anyone help
What I have tried:
{
bestTotal = 0;
bestStart = 0;
bestEnd = 0;
loops = 0;
for
(int i = 0; i < data.Length; i++)
{
Console.WriteLine("Value {0}", data[i]);
}
}