i need help with programing C# it only show the highest sale, but the lowest and
ID: 3731815 • Letter: I
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 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 < myArray.Length)
{
//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 < myArray.Length; 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 < myArray.Length; count++)
{
if (myArray[count] < low)
{
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 < myArray.Length; 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 ExitExplanation / Answer
Hi please find the modified functions average and lowest:
private double Getlowest(double[] myArray)
{
double low = 0;
for (int count = 0; count < myArray.Length; count++)
{
if (myArray[count] < low)
{
low = myArray[count];
lowestlabel.Text = low.ToString("c");
}
}
//return the lowest method
return low;
}
Modify the above function as follows:
private double Getlowest(double[] myArray)
{
// double low = 0;//if we declare low with zero then 0 is always lessthan any positive numbers thats why it is not working
double low=myArray[0];
for (int count = 1; count < myArray.Length; count++)//and here the change is I have started count with 1 because we have already stored 0 th element in low
{
if (myArray[count] < low)//now we are checking element in low with other elements if it is les than then
{
low = myArray[count];//assigning that to low
lowestlabel.Text = low.ToString("c");
}
}
//return the lowest method
return low;
}
Modify the below function:
private double GetAverage(double[] myArray)
{
double total = 0; double myAverage = 0;
for (int count = 0; count < myArray.Length; count++)
{
myAverage = total / myArray.Length;
averlabel.Text = myAverage .ToString("c");
}
return myAverage;
}
As follows:
private double GetAverage(double[] myArray)
{
double total = 0; double myAverage = 0;
for (int count = 0; count < myArray.Length; count++)
{
//myAverage = total / myArray.Length;//this statement is wrong because total is 0 and if you divide 0 by anything it will be zero only thats why you are getting 0 in that box so comment this line
//averlabel.Text = myAverage .ToString("c");
total=total+myArray[i];//this will add all elements in the array to total
}
myAverage = total / myArray.Length;//here we are calculating the average and storing in my average
averlabel.Text = myAverage .ToString("c");
return myAverage;//returning the average
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.