1
Reply

Converting Pseudocode to C#

sam ferguson

sam ferguson

Mar 23 2017 12:44 PM
291
 I need help converting this pseudocode to c# for an assignment and im stuck. Any help would be appreciated.
 
Pseudocode:
 
Set start position to 0, total to 0
For every profit value... // index from 0 to end of array as end position
{
Add value to total
Keep sequence info (start, end, total) if total exceeds current best
If total is less than 0,
set start position to next index and set total to 0
}
 
 
 
Context (base) code that i'm given:
 
using System;
using System.IO;
namespace ProfitCalculator
{
/// ==============================================================================
public class RangeFinder
{
///
/// Performs calculations to find the start and end of the "best" data sequence
/// which has the greatest sum of data values over that sequence.
///
/// the data to be examined
/// the start point found by the search
/// the end point found by the search
/// the sum of data values over that range
/// the number of executions of the inner loop
public static void MaxSum(double [] data, out int bestStart,
out int bestEnd, out double bestTotal, out int loops)
{
bestTotal = 0 ;
bestStart = 0;
bestEnd = 0;
loops=0;
/// TODO - put your process code here
}
}
/// ==============================================================================
///
/// Tests the Profits Calculator
///
class Test
{
///
/// 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.MaxSum(
data, out bestStart, out bestEnd, out bestTotal, out loops);
Console.WriteLine ( "Start : {0} End : {1} Total {2} Loops {3}",
bestStart, bestEnd, bestTotal, loops);
}
}
}
 

Answers (1)