Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.IO;

namespace ProfitCalculator
{
/// ==============================================================================

public class RangeFinder
{
/// <summary>
/// Performs calculations to find the start and end of the "best" data sequence
/// which has the greatest sum of data values over that sequence.
///
/// <param name="data">the data to be examined
/// <param name="bestStart">the start point found by the search
/// <param name="bestEnd">the end point found by the search
/// <param name="bestTotal">the sum of data values over that range
/// <param name="loops">the number of executions of the inner loop
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;
}

/// ==============================================================================
/// <summary>
/// Tests the Profits Calculator
///
class Test
{
/// <summary>
/// The main entry point for the application.
///
static void Main()
{
double[] data;
int bestStart, bestEnd;
double bestTotal;
int loops;

/// name of the file and the number of readings
string filename = "week52.txt";
int items = 52;

data = new double[items]; /// create the data array

try
{
TextReader textIn = new StreamReader(filename);
for (int i = 0; i < items; i++) /// input and store the data values
{
string line = textIn.ReadLine();
data[i] = double.Parse(line);
}
textIn.Close();
}
catch
{
Console.WriteLine("File Read Failed");
return;
}

/// ---------------------------------------------------------------------
/// call the process method to find the best profit period
/// ---------------------------------------------------------------------

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]);
}

}
Posted
Updated 20-Mar-18 4:06am
v3
Comments
Patrice T 20-Mar-18 9:36am    
Looks like the requirement is not complete.
Can you post it and link if it comes from a site like codechef.
Patrice T 20-Mar-18 9:48am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
You also need to complete the requirement

1 solution

Read the instructions, get a paper and pencil, and try it manually.
Combined with the rest of the question - which we don't have - it should make some sense.

We can't work out from that fragment of your homework exactly what your tutor is asking you do to - it seems to be searching for a "highest profit subtotal" but we have no idea what the rules for that are.

So read the whole question, and try it for yourself - when you have it working it should be easy to translate into computer code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900