Home > Mobile >  How do I calculate the average
How do I calculate the average

Time:11-30

Im able to get the sum in dataGridView but having trouble on properly getting the average.

 foreach (DataGridViewRow row in dataGridView.Rows)
                row.Cells["Average"].Value = Convert.ToDouble(row.Cells[4].Value)   Convert.ToDouble(row.Cells[5].Value)   Convert.ToDouble(row.Cells[6].Value);

enter image description here

CodePudding user response:

The average is the sum of the elements devided by the amount of elements. For your case with three elements you'll have to devide the sum by three.

CodePudding user response:

You are not doing an average calculation but a sum calculation. Simply divide the total by the number of values provided to find the average:

foreach (DataGridViewRow row in dataGridView.Rows)
                    row.Cells["Average"].Value = (Convert.ToDouble(row.Cells[4].Value)   Convert.ToDouble(row.Cells[5].Value)   Convert.ToDouble(row.Cells[6].Value)) / 3;
  • Related