Reputation: 9
PART 1: I am to create a program that that reads a file’s contents into an array, and displays the array’s contents in a ListBox control, and calculates and displays the total of the array’s values. - DONE THAT PART
PART 2: Calculate the average, Highest, and Lowest value and display them in a Label control.
I'm new to coding so I couldn't do much, I turn to stack overflow for help
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SalesAnalysis
{
public partial class SalesAnalysisApplication : Form
{
public SalesAnalysisApplication()
{
InitializeComponent();
}
private void salesAnalysisButton_Click(object sender, EventArgs e)
{
//declaring array
const int SIZE = 100;
decimal[] sales = new decimal[SIZE];
//varible to hold amount stored in array
int count = 0;
//declaring streamreader
StreamReader inputFile;
//opening the sales file
inputFile = File.OpenText("Sales.txt");
try
{
//pull contents from file into array while there is still
// items to pull and the array isnt full
while (!inputFile.EndOfStream && count < sales.Length)
{
sales[count] = decimal.Parse(inputFile.ReadLine());
count++;
}
//close the file
inputFile.Close();
//display contents in listbox
for (int index = 0; index < count; index++)
{
salesListBox.Items.Add(sales[index]);
}
//Calculate the sum of all values
for (int index = 0; index < sales.Length; index++)
{
totalSales += sales[index];
}
//display total of all values
salesListBox.Items.Add("Total =" + totalSales);
//Determine the average sales from the array
for (int index = 0; index < sales.Length; index++)
{
//calculate the average
averageSales = totalSales / 7;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Clear_Click(object sender, EventArgs e)
{
//Clear all fields
salesListBox.Items.Clear();
averageResultsLabel.Text = "";
highestResultsLabel.Text = "";
lowestResultsLabel.Text = "";
}
private void exitButton_Click(object sender, EventArgs e)
{
//close form
this.Close();
}
}
}
Upvotes: 0
Views: 1014
Reputation: 6778
The non-linq approach.
You have a sales.Length
and a totalSales
. Therefore, you have an averageSales
.
For max & min, while you are in the for
loop
for (int index = 0; index < sales.Length; index ++
{
if (sales
}
simply assign the value based on an if
statement. Something like:
if (sales[index] > highestSales)
{
highestSales = sales[index];
}
if (sales[index] < lowestSales)
{
lowestSales = sales[index];
}
Upvotes: 1
Reputation: 186708
In case you prefer good old loop (and no Linq), you can try File.ReadLines
and foreach
:
decimal max = 0;
decimal min = 0;
decimal sum = 0;
int count = 0;
foreach(string line in File.ReadLines("Sales.txt")) {
decimal value = decimal.Parse(line);
salesListBox.Items.Add(value);
if (count == 0 || value > max)
max = value;
if (count == 0 || value < min)
min = value;
sum += value;
count += 1;
}
decimal average = sum / count;
averageResultsLabel.Text = average.ToString("f2");
highestResultsLabel.Text = max.ToString();
lowestResultsLabel.Text = min.ToString();
Here we don't have to create any array at all. If you insist on having array a simple Linq can help
decimal[] sales = File
.ReadLines("Sales.txt")
.Select(line => decimal.Parse(line))
.ToArray();
And foreach
will be
foreach(decimal value in sales) {
salesListBox.Items.Add(value);
...
}
Upvotes: 0
Reputation: 589
First convert u r Decimal
array to List.
List<decimal> lst = sales.OfType<decimal>().ToList(); // Convert to List.
Then Follow Linq
Operations.
lst.Average(); lst.Min();
Upvotes: 0
Reputation: 1547
You can use linq to do this for example:
var list=Enumerable.Range(0,1000);
var average = list.Average();
var highest = list.Max()
var smallest= list.Min()
var total= list.Sum()
P.S do not forget to add using System.Linq;
Upvotes: 6