Question
i need help with programing C# it only show the highest sale, but the lowest and averge is not working here are the codes i
i need help with programing C#
it only show the highest sale, but the lowest and averge is not working
here are the codes i wrote:
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 user_Lab
{
public partial class DisplaySales : Form
{
public DisplaySales()
{
InitializeComponent();
}
//open and read the text file
private void ProcessSalesFile()
{
//part1
try
{
///decalre variables
const int SIZE = 7;
double sum = 0;
double[] myArray = new double[SIZE];
int index = 0;
StreamReader inputFile;
//2.open the file to read
inputFile = File.OpenText("sales.txt");
//loop through the file to get each value a put in an array
while (!inputFile.EndOfStream && index
{
//read value into array
myArray[index] = double.Parse(inputFile.ReadLine());
//sum it all up
sum += myArray[index];
SaleslistBox.Items.Add(myArray[index].ToString("c"));
index++;
}
//3.close the file
inputFile.Close();
//call the methods.part2
highestlabel.Text = GetHighest(myArray).ToString("c");
lowestlabel.Text = Getlowest(myArray).ToString("c");
averlabel.Text = GetAverage(myArray).ToString("c");
//loop through the array to calculate the total in this loop also display each array value to the list box
totalSaleslabel.Text = sum.ToString("c");
}
catch(Exception ex)
{
// error message showing to the user
MessageBox.Show(ex.Message);
}
}
// GetHighest Method
private double GetHighest(double[] myArray)
{
double hVal = 0;
for (int count = 0; count
{
if (myArray[count] > hVal)
{
hVal = myArray[count];
highestlabel.Text = hVal.ToString("c");
}
}
//return highest
return hVal;
}
//Get lowest method
private double Getlowest(double[] myArray)
{
double low = 0;
for (int count = 0; count
{
if (myArray[count]
{
low = myArray[count];
lowestlabel.Text = low.ToString("c");
}
}
//return the lowest method
return low;
}
//write the methods GetAverage
private double GetAverage(double[] myArray)
{
double total = 0; double myAverage = 0;
for (int count = 0; count
{
myAverage = total / myArray.Length;
averlabel.Text = myAverage .ToString("c");
}
return myAverage;
}
private void GetSalesbutton_Click(object sender, EventArgs e)
{
ProcessSalesFile();
}
private void Clearbutton_Click(object sender, EventArgs e)
{
highestlabel.Text="";
averlabel.Text = "";
lowestlabel.Text = "";
totalSaleslabel.Text = "";
SaleslistBox.Items.Clear();
}
private void Exitbutton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Sales Analysis $1,245.67 S1.189.55 $1,098.72 $1,456.88 $2,109.34 $1.987.55 $1,872.36 Highest Sale $2,109.34 Lowest Sale S0.00 Average Sale 0.00 Total Sale is $10,960.07 Get Sales Exit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started