Infinity in interest calculation? - c#

Thanks to Laurence Burke in my other question for the isMaybeMoney function, I am able to determine whether an input is money or not.
What I'm doing now is trying to calculate the total after interest but I keep getting Infinity written to the screen. What in the world is wrong with my interestsaccrued function? It's supposed to be $3,522.55 when I use $1,234 as the starting balance with 3.5% interest.
Can someone please help me out?
static float money;
static void Main()
{
string[] myMaybeBalances = Accounts.GetStartingBalances();
myIsMaybeMoneyValidator Miimv = new myIsMaybeMoneyValidator();
ArrayList interests = Miimv.interestsAccrued(myMaybeBalances);
foreach (object interest in interests)
{
Console.WriteLine(interest);
}
Console.ReadLine();
}
public ArrayList interestsAccrued(string[] myMaybeBalances)
{
ArrayList interests = new ArrayList();
foreach (string myMaybeBalance in myMaybeBalances)
{
bool myResult = isMaybeMoney(myMaybeBalance);
if (myResult == true)
{
decimal[] rates = Accounts.GetRates();
for (int i = 0; i < rates.Length; i++)
{
decimal rate = rates[i];
float total = 1;
int n_X_t = 360;
while (n_X_t != 0)
{
rate = (1 + rates[i] / 12);
float myRate;
float.TryParse(rate.ToString(), out myRate);
total = total * myRate;
total = total * money;
n_X_t = n_X_t - 1;
}
interests.Add(total);
}
}
}
return interests;
}
public bool isMaybeMoney(object theirMaybeMoney)
{
string myMaybeMoney = theirMaybeMoney.ToString();
float num;
bool isValid = float.TryParse(myMaybeMoney,
NumberStyles.Currency,
CultureInfo.GetCultureInfo("en-US"), // cached
out num);
money = num;
return isValid;
}

You are multiplying total by the rate each step through the while loop, which seems reasonable enough, but you also multiply total by the value of the variable "money", which as far as I can tell is the starting balance.
So you multiply by the starting balance 360 times. If only my savings accounts worked like that! I'm not sure if the rest of the logic is correct, but for a start, try moving the
total = total * money;
to be under the line
float total = 1;
(or better yet just change from
float total = 1;
to
float total = money;
and get rid of the line
total = total * money;
altogether)

The codes you have is not evaluate. NO BENEFIT of construct loop for interest calculate!
This is not needful yet introduce much of risk for high complication
Here is codes you want for use of FUNCTIONARY encapsulate :
static void Main()
{
var interests = new List<decimal>();
foreach (string possibleBalance in Accounts.GetStartingBalances())
foreach (decimal rate in Accounts.GetRates())
{
decimal balance;
if (!decimal.TryParse(possibleBalance, NumberStyles.Currency, CultureInfo.CurrentCulture, out balance))
continue;
decimal interest = CalculateInterestAccrued(balance, rate, 12, 30);
interests.Add(interest);
}
foreach (decimal interest in interests)
Console.WriteLine(interest);
Console.ReadKey();
}
static decimal CalculateInterestAccrued(decimal principal, decimal rate, int compoundsPerYear, int years)
{
return principal * (decimal)Math.Pow((double)(1 + rate / compoundsPerYear), compoundsPerYear * years);
}
thanks,
PRASHANT :)

Related

Calculate discount price

I want to make my application to calculate a discount price. This is how I find my discount price, but I have a little problem (logic problem):
private void UpdateDiscount(object sender, EventArgs e)
{
decimal actualPrice = 0;
decimal discount = 0;
int calculateDiscount = 0;
int totalDiscount = 0;
int afterDiscount = 0;
int totalAfterDiscount = 0;
int total = 0;
if (numericTextBox1.TextLength == 6)
{
this.numericUpDown2.Enabled = true;
discount = Convert.ToInt32(this.numericUpDown2.Value);
calculateDiscount = Convert.ToInt32(discount / 100);
totalDiscount = calculateDiscount;
if (!string.IsNullOrEmpty(this.numericTextBox3.Text.ToString()))
{
actualPrice = Convert.ToDecimal(this.numericTextBox3.Text);
}
else
{
numericTextBox3.Text = "";
}
afterDiscount = Convert.ToInt32(actualPrice * totalDiscount);
totalAfterDiscount = Convert.ToInt32(actualPrice);
total = Convert.ToInt32(totalAfterDiscount - afterDiscount);
if (numericUpDown2.Value > 0)
{
this.numericTextBox6.Text = total.ToString();
}
}
else if (numericTextBox1.TextLength != 6)
{
this.numericUpDown2.Enabled = false;
this.numericUpDown2.Value = 0;
this.numericTextBox6.Text = "";
}
else
{
actualPrice = 0;
discount = 0;
calculateDiscount = 0;
totalDiscount = 0;
afterDiscount = 0;
totalAfterDiscount = 0;
total = 0;
MessageBox.Show("There is no data based on your selection", "Error");
}
}
This is the result, the total after discount still same with the total price, even though I already give it discount value.
Given
a price P such that 0 <= P, and
a discount percentage D such that 0 <= D <= 100
you can compute the discount (markdown) MD that needs to be applied as
MD = P * (D/100)
You can then get the discounted price DP as
DP = P - MD
Given that, this should do you:
public static class DecimalHelpers
{
public static decimal ComputeDiscountedPrice( this decimal originalPrice , decimal percentDiscount )
{
// enforce preconditions
if ( originalPrice < 0m ) throw new ArgumentOutOfRangeException( "originalPrice" , "a price can't be negative!" ) ;
if ( percentDiscount < 0m ) throw new ArgumentOutOfRangeException( "percentDiscount" , "a discount can't be negative!" ) ;
if ( percentDiscount > 100m ) throw new ArgumentOutOfRangeException( "percentDiscount" , "a discount can't exceed 100%" ) ;
decimal markdown = Math.Round( originalPrice * ( percentDiscount / 100m) , 2 , MidpointRounding.ToEven ) ;
decimal discountedPrice = originalPrice - markdown ;
return discountedPrice ;
}
}
Don't use int (or Convert.ToInt32) when dealing with money.
See this
decimal discount = 10;
var calculateDiscount = Convert.ToInt32(discount / 100);
MessageBox.Show(calculateDiscount.ToString());
calculateDiscount will be 0 because 0.1 will be converted 0.
I suspect the problem is mostly due to the fact that you're using integers for everything. In particular, these two lines:
discount = Convert.ToInt32(this.numericUpDown2.Value);
calculateDiscount = Convert.ToInt32(discount / 100);
When you use 10 for "10%" as the discount, the second line there is actually resulting in a zero. This is because you are doing integer mathematics: integers can only be whole numbers, and when they have a number that is not whole they truncate it. In this case, discount / 100 in your example would be 0.1, which would get truncated to zero.
Instead of using int, I recommend using decimal for all financial transactions. I would replace most of your integer variable types throughout that function with decimal.
I think you're going to have problems with...
calculateDiscount = Convert.ToInt32(discount / 100);
Int (Integers) are whole numbers, they will be rounded down. If discount is less than 100 it will always be zero.
Avoid using double or floats for financial transactions as they can produce considerable floating point errors.
Using integers is good, as they are accurate and quick, HOWEVER you MUST always consider how numbers will be rounded, ensure the operands and results are always whole numbers.
If they are not whole numbers use the Decimal structure, which under the covers is comprised of three integers, but three times slower than using integers.
In most cases being 3 times slower than blindingly quick, still ends up being blindingly quick so when in doubt use Decimals.
if you want to get the Original Price from the Discounted Price and Discount Value. you can use this code
using System;
public class Program
{
public static void Main()
{
decimal discountedPrice = 280;
decimal discount = (decimal)0.20;
decimal originalPrice = discountedPrice / (decimal)(1-discount);
Console.WriteLine(originalPrice);
}
}```

Trying to calculate weighted moving average but getting zero as result always

I am trying to get out the "Weighted moving average" of a array of double values.
I have tried to get all peaces from some internet examples together but i always getting zero as result.
Problem is the calulation of "weight", its being zero but it should not be zero, example 1 / 107 = 0,0093457943925234 but the weight double values getting zero, i have tried change to long and decimal and getting the same problem.
Any ideas?
public static double WeighteedMovingAverage(double[] data)
{
double aggregate = 0;
double weight;
int item = 1;
int count = data.Count();
foreach (var d in data)
{
weight = item / count;
aggregate += d * weight;
count++;
}
return (double)(aggregate / count);
}
weight = (double)item / (double)count;
need to be double to avoid casting before operation
public static double WeighteedMovingAverage(double[] data)
{
double aggregate = 0;
double weight;
int item = 1;
int count = data.Count();
foreach (var d in data)
{
//replace with line below weight = item / count;
weight = (double)item / (double)count;
aggregate += d * weight;
count++;
}
//replace with line below return (double)(aggregate / count);
return (double)(aggregate / (double)count);
}

Mean and Standard Deviation wrong c#

I've been doing an app since few days ago but it's wrong and I do not know why.
I've done the same operation in various ways. I've searched here on the blog, but I still get the incorrect result.
I hope you can help me:
I'm calculating the ** Mean and Standard Deviation**. The Mean is OK. The Standard Deviation is wrong. This is my code:
LinkedList<Double> lista = new LinkedList<Double>();
int contador = 0;
private void btnAgregar_Click(object sender, EventArgs e)
{
lista.AddLast(Convert.ToDouble(txtNum.Text));
MessageBox.Show("Se agregó el número: " + txtNum.Text);
contador++;
txtNum.Text = "";
txtNum.Focus();
}
Double media;
Double desviacionE = 0;
Double suma = 0;
private void btnCalcular_Click(object sender, EventArgs e)
{
media = 0;
calculaMedia();
calculaDesviacionE();
}
public void calculaMedia()
{
foreach (var item in lista)
{
String valorItem = item.ToString();
suma = suma + Convert.ToDouble(valorItem);
}
media = suma / contador;
txtMedia.Text = "" + media;
}
public void calculaDesviacionE()
{
Double average = lista.Average();
Double sum = 0;
foreach (var item in lista)
{
sum += ((Convert.ToDouble(item.ToString()))*(Convert.ToDouble(item.ToString())));
}
Double sumProm = sum / lista.Count();
Double desvE = Math.Sqrt(sumProm-(average*average));
txtDesv.Text = "" + desvE;
}
I hope You can help me!
Thank You
Following the rules for standard deviation found at http://en.wikipedia.org/wiki/Standard_deviation
LinkedList<Double> list = new LinkedList<Double>();
double sumOfSquares = 0;
double deviation;
double delta;
list.AddLast(2);
list.AddLast(4);
list.AddLast(4);
list.AddLast(4);
list.AddLast(5);
list.AddLast(5);
list.AddLast(7);
list.AddLast(9);
double average = list.Average();
Console.WriteLine("Average: " + average);
foreach (double item in list)
{
delta = Math.Abs(item - average);
sumOfSquares += (delta * delta);
}
Console.WriteLine("Sum of Squares: " + sumOfSquares);
deviation = Math.Sqrt(sumOfSquares / list.Count());
Console.WriteLine("Standard Deviation: " + deviation); //Final result is 2.0
You need to subtract the average before squaring.
// Population Standard Deviation
public void populationStandardDev()
{
Double average = lista.Average();
Double sum = 0;
foreach (var item in lista)
{
Double difference = item - average;
sum += difference*difference;
}
Double sumProd = sum / lista.Count(); // divide by n
Double desvE = Math.Sqrt(sumProd);
}
// Standard deviation
public void standardDev()
{
Double average = lista.Average();
Double sum = 0;
foreach (var item in lista)
{
Double difference = item - average;
sum += difference*difference;
}
Double sumProd = sum / (lista.Count()-1); // divide by n-1
Double desvE = Math.Sqrt(sumProd);
}
The formula depends on the set of data you have.
Is it the entire population? Then you should use the Population Standard Deviation (divisor: n).
Is the data a sample of a set? Then you should use the Sample Standard Deviation (divisor: n - 1)
You may find an easier-to-understand guide here: Laerd Statistics - Standard Deviation, which also has a handy Calculator for both solutions.
So, it is as #Greg answered, though I would first check if the list holds any values to avoid division by zero.
double stdDeviation = 0;
if (lista.Any())
{
var avg = lista.Average();
var sumOfSquares = lista.Sum(item => Math.Pow(item - avg, 2));
stdDeviation = Math.Sqrt(sumOfSquares / [divisor goes here]);
}
return stdDeviation;
Where the divisor be lista.Count() for population or (lista.Count() - 1) for samples.

How do I determine the standard deviation (stddev) of a set of values?

I need to know if a number compared to a set of numbers is outside of 1 stddev from the mean, etc..
While the sum of squares algorithm works fine most of the time, it can cause big trouble if you are dealing with very large numbers. You basically may end up with a negative variance...
Plus, don't never, ever, ever, compute a^2 as pow(a,2), a * a is almost certainly faster.
By far the best way of computing a standard deviation is Welford's method. My C is very rusty, but it could look something like:
public static double StandardDeviation(List<double> valueList)
{
double M = 0.0;
double S = 0.0;
int k = 1;
foreach (double value in valueList)
{
double tmpM = M;
M += (value - tmpM) / k;
S += (value - tmpM) * (value - M);
k++;
}
return Math.Sqrt(S / (k-2));
}
If you have the whole population (as opposed to a sample population), then use return Math.Sqrt(S / (k-1));.
EDIT: I've updated the code according to Jason's remarks...
EDIT: I've also updated the code according to Alex's remarks...
10 times faster solution than Jaime's, but be aware that,
as Jaime pointed out:
"While the sum of squares algorithm works fine most of the time, it
can cause big trouble if you are dealing with very large numbers. You
basically may end up with a negative variance"
If you think you are dealing with very large numbers or a very large quantity of numbers, you should calculate using both methods, if the results are equal, you know for sure that you can use "my" method for your case.
public static double StandardDeviation(double[] data)
{
double stdDev = 0;
double sumAll = 0;
double sumAllQ = 0;
//Sum of x and sum of x²
for (int i = 0; i < data.Length; i++)
{
double x = data[i];
sumAll += x;
sumAllQ += x * x;
}
//Mean (not used here)
//double mean = 0;
//mean = sumAll / (double)data.Length;
//Standard deviation
stdDev = System.Math.Sqrt(
(sumAllQ -
(sumAll * sumAll) / data.Length) *
(1.0d / (data.Length - 1))
);
return stdDev;
}
The accepted answer by Jaime is great, except you need to divide by k-2 in the last line (you need to divide by "number_of_elements-1").
Better yet, start k at 0:
public static double StandardDeviation(List<double> valueList)
{
double M = 0.0;
double S = 0.0;
int k = 0;
foreach (double value in valueList)
{
k++;
double tmpM = M;
M += (value - tmpM) / k;
S += (value - tmpM) * (value - M);
}
return Math.Sqrt(S / (k-1));
}
The Math.NET library provides this for you to of the box.
PM> Install-Package MathNet.Numerics
var populationStdDev = new List<double>(1d, 2d, 3d, 4d, 5d).PopulationStandardDeviation();
var sampleStdDev = new List<double>(2d, 3d, 4d).StandardDeviation();
See PopulationStandardDeviation for more information.
Code snippet:
public static double StandardDeviation(List<double> valueList)
{
if (valueList.Count < 2) return 0.0;
double sumOfSquares = 0.0;
double average = valueList.Average(); //.NET 3.0
foreach (double value in valueList)
{
sumOfSquares += Math.Pow((value - average), 2);
}
return Math.Sqrt(sumOfSquares / (valueList.Count - 1));
}
You can avoid making two passes over the data by accumulating the mean and mean-square
cnt = 0
mean = 0
meansqr = 0
loop over array
cnt++
mean += value
meansqr += value*value
mean /= cnt
meansqr /= cnt
and forming
sigma = sqrt(meansqr - mean^2)
A factor of cnt/(cnt-1) is often appropriate as well.
BTW-- The first pass over the data in Demi and McWafflestix answers are hidden in the calls to Average. That kind of thing is certainly trivial on a small list, but if the list exceed the size of the cache, or even the working set, this gets to be a bid deal.
I found that Rob's helpful answer didn't quite match what I was seeing using excel. To match excel, I passed the Average for valueList in to the StandardDeviation calculation.
Here is my two cents... and clearly you could calculate the moving average (ma) from valueList inside the function - but I happen to have already before needing the standardDeviation.
public double StandardDeviation(List<double> valueList, double ma)
{
double xMinusMovAvg = 0.0;
double Sigma = 0.0;
int k = valueList.Count;
foreach (double value in valueList){
xMinusMovAvg = value - ma;
Sigma = Sigma + (xMinusMovAvg * xMinusMovAvg);
}
return Math.Sqrt(Sigma / (k - 1));
}
With Extension methods.
using System;
using System.Collections.Generic;
namespace SampleApp
{
internal class Program
{
private static void Main()
{
List<double> data = new List<double> {1, 2, 3, 4, 5, 6};
double mean = data.Mean();
double variance = data.Variance();
double sd = data.StandardDeviation();
Console.WriteLine("Mean: {0}, Variance: {1}, SD: {2}", mean, variance, sd);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
public static class MyListExtensions
{
public static double Mean(this List<double> values)
{
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
}
public static double Mean(this List<double> values, int start, int end)
{
double s = 0;
for (int i = start; i < end; i++)
{
s += values[i];
}
return s / (end - start);
}
public static double Variance(this List<double> values)
{
return values.Variance(values.Mean(), 0, values.Count);
}
public static double Variance(this List<double> values, double mean)
{
return values.Variance(mean, 0, values.Count);
}
public static double Variance(this List<double> values, double mean, int start, int end)
{
double variance = 0;
for (int i = start; i < end; i++)
{
variance += Math.Pow((values[i] - mean), 2);
}
int n = end - start;
if (start > 0) n -= 1;
return variance / (n);
}
public static double StandardDeviation(this List<double> values)
{
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
}
public static double StandardDeviation(this List<double> values, int start, int end)
{
double mean = values.Mean(start, end);
double variance = values.Variance(mean, start, end);
return Math.Sqrt(variance);
}
}
}
/// <summary>
/// Calculates standard deviation, same as MATLAB std(X,0) function
/// <seealso cref="http://www.mathworks.co.uk/help/techdoc/ref/std.html"/>
/// </summary>
/// <param name="values">enumumerable data</param>
/// <returns>Standard deviation</returns>
public static double GetStandardDeviation(this IEnumerable<double> values)
{
//validation
if (values == null)
throw new ArgumentNullException();
int lenght = values.Count();
//saves from devision by 0
if (lenght == 0 || lenght == 1)
return 0;
double sum = 0.0, sum2 = 0.0;
for (int i = 0; i < lenght; i++)
{
double item = values.ElementAt(i);
sum += item;
sum2 += item * item;
}
return Math.Sqrt((sum2 - sum * sum / lenght) / (lenght - 1));
}
The trouble with all the other answers is that they assume you have your
data in a big array. If your data is coming in on the fly, this would be
a better approach. This class works regardless of how or if you store your data. It also gives you the choice of the Waldorf method or the sum-of-squares method. Both methods work using a single pass.
public final class StatMeasure {
private StatMeasure() {}
public interface Stats1D {
/** Add a value to the population */
void addValue(double value);
/** Get the mean of all the added values */
double getMean();
/** Get the standard deviation from a sample of the population. */
double getStDevSample();
/** Gets the standard deviation for the entire population. */
double getStDevPopulation();
}
private static class WaldorfPopulation implements Stats1D {
private double mean = 0.0;
private double sSum = 0.0;
private int count = 0;
#Override
public void addValue(double value) {
double tmpMean = mean;
double delta = value - tmpMean;
mean += delta / ++count;
sSum += delta * (value - mean);
}
#Override
public double getMean() { return mean; }
#Override
public double getStDevSample() { return Math.sqrt(sSum / (count - 1)); }
#Override
public double getStDevPopulation() { return Math.sqrt(sSum / (count)); }
}
private static class StandardPopulation implements Stats1D {
private double sum = 0.0;
private double sumOfSquares = 0.0;
private int count = 0;
#Override
public void addValue(double value) {
sum += value;
sumOfSquares += value * value;
count++;
}
#Override
public double getMean() { return sum / count; }
#Override
public double getStDevSample() {
return (float) Math.sqrt((sumOfSquares - ((sum * sum) / count)) / (count - 1));
}
#Override
public double getStDevPopulation() {
return (float) Math.sqrt((sumOfSquares - ((sum * sum) / count)) / count);
}
}
/**
* Returns a way to measure a population of data using Waldorf's method.
* This method is better if your population or values are so large that
* the sum of x-squared may overflow. It's also probably faster if you
* need to recalculate the mean and standard deviation continuously,
* for example, if you are continually updating a graphic of the data as
* it flows in.
*
* #return A Stats1D object that uses Waldorf's method.
*/
public static Stats1D getWaldorfStats() { return new WaldorfPopulation(); }
/**
* Return a way to measure the population of data using the sum-of-squares
* method. This is probably faster than Waldorf's method, but runs the
* risk of data overflow.
*
* #return A Stats1D object that uses the sum-of-squares method
*/
public static Stats1D getSumOfSquaresStats() { return new StandardPopulation(); }
}
We may be able to use statistics module in Python. It has stedev() and pstdev() commands to calculate standard deviation of sample and population respectively.
details here: https://www.geeksforgeeks.org/python-statistics-stdev/
import statistics as st
print(st.ptdev(dataframe['column name']))
This is Population standard deviation
private double calculateStdDev(List<double> values)
{
double average = values.Average();
return Math.Sqrt((values.Select(val => (val - average) * (val - average)).Sum()) / values.Count);
}
For Sample standard deviation, just change [values.Count] to [values.Count -1] in above code.
Make sure you don't have only 1 data point in your set.

Simple File I/O - Read a new line

I have a file that stores exam scores for a class of students. I am trying to write a program that successfully opens the file, reads in the exam scores, finds the average score, the highest score, and the lowest score, and prints these out. The average score should be printed with 2 digits after the decimal point.
This is what I have so far:
static void Main()
{
string myData = "";
int temp = 0;
int max = 0;
int min = 0;
double average = 0;
StreamReader fileReader = new StreamReader("data.txt");
do
{
myData = fileReader.ReadLine();
if (myData != null)
{
max = int.Parse(myData);
temp = int.Parse(myData);
if (temp > max)
temp = max;
}
} while (myData != null);
fileReader.Close();
Console.ReadLine();
}//End Main()
I don't exactly know how to proceed. How do I read in a new line and assign it to temp? I don't think I'm doing it right.
Here is one way that will make your teacher sad :P
static void Main(string[] args)
{
var file = new StreamReader("scores.txt");
var split = file.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
IEnumerable<int> ints = split.Select(x => int.Parse(x));
Console.WriteLine("Total Scores:" + ints.Count());
Console.WriteLine("Max:" + ints.Max());
Console.WriteLine("Min:" + ints.Min());
Console.WriteLine("Average:" + ints.Average().ToString("0.00"));
Console.ReadLine();
}
While this is technically correct, this is counter-productive to understanding algorithms (however basic they may be), and I suggest you look at the other answers. But this demonstrates how versatile the .NET framework is.
<3 LINQ
The first error I found is that max will always be exactly equal to temp because you assign them the same value, so this if will never be true:
max = int.Parse(myData);
temp = int.Parse(myData);
if (temp > max)
Not bad. Here's some pseudocode:
File.ReadAllLines to get an array of strings (call this lines)
Foreach line in lines
Parse the double (? is each line guaranteed to be an int?)
if max < parsed, max = parsed
if min > parsed, min = parsed
sum += parsed
Print out min, max, (sum / lines.count).ToString("000.00")
Assumes the file looks like:
25
12
33.5
100
75
...
Since this is homework, I'll just give you some clues.
Right now, you're setting "max" each time through the loop. Try only setting temp, and see what happens. You might want to consider defaulting max (when you create it) to a very small number instead of 0.
Also, you'll need to do something similar for "min", but default it to a very large number.
int max = int.MinValue;
int min = int.MaxValue;
In order to get the average, you'll need to have a sum and a count, and keep track of those. Then, at the end, use a double to compute the average, and print. To get 2 decimal places, you can use average.ToString("N") - the "N" format does a nicely formatted number with 2 decimal places by default.
How about you separate out the processes? Do the reading and fill a list of integers with the contents of the file. Then perform the processing for min / max and average later on.
Isolating issues help you focus on them. I like to call this noise reduction. As a contractor I get to work on a lot of messy code and one of the reasons they are hard to understand is that too much is going on at the same time. If you simplify whats going on the code almost writes itself. This is also called Separation of Concerns. This is a very important programming principle.
After you've isolated the issues and got the code working you can then try to put it all together again so the process is more efficient (if you do it inline with the file reading then you will only hold one line in memory at a time).
For starters, you need to get rid of the line "max = int.Parse(myData)". Otherwise, you'll keep overwriting max with the current value.
I take it that this is the general gist of your assignment. You should be leery of copying this code.
double count = 0.0;
double min = double.MaxValue;
double max = double.MinValue;
double total = 0.0;
using(StreamReader sr = new StreamReader(#"c:\data.txt"))
{
while (!sr.EndOfStream)
{
String line = sr.ReadLine();
double value = double.Parse(line.Trim());
if (value < min) min = value;
if (value > max) max = value;
total += value;
count++;
}
}
Console.WriteLine("Min: {0}", min);
Console.WriteLine("Max: {0}", max);
Console.WriteLine("Avg: {0}", (total / count).ToString("0.00"));
Console.ReadLine();
static void Main(string[] args)
{
const string filename = #"data.txt";
bool first = true;
int min=0, max=0, total=0;
var lines = File.ReadAllLines(filename);
foreach (var line in lines)
{
var score = int.Parse(line.Trim());
if (first)
{
min = max = total = score;
first = false;
continue;
}
if (score < min)
min = score;
if (score > max)
max = score;
total += score;
}
if (first)
{
Console.WriteLine("no input");
return;
}
var average = (double)total/lines.Length;
Console.WriteLine(string.Format("Min: {0}, Max: {1}, Average: {2:F2}", min, max, average));
}
Hey thanks to everybody who helped out and offered suggestions, here is my final code implementation (without using arrays)
using System;
using System.IO;
class Program
{
static void Main()
{
string line = "";
int value = 0;
int max = 0;
int min = 100;
int total = 0;
double count = 0.0;
double average = 0;
StreamReader fileReader = new StreamReader(#"data.txt");
do
{
line = fileReader.ReadLine();
if (line != null)
{
value = int.Parse(line);
if (value > max)
max = value;
if (value < min)
min = value;
total += value;
count++;
}
} while (line != null);
average = total / count;
Console.WriteLine("Max: {0}", max);
Console.WriteLine("Min: {0}", min);
Console.WriteLine("Avg: {0:f2}", average);
Console.ReadLine();
}//End Main()
}//End class Program

Categories