Skip to main content

How to find the median (data from excel file) in c#

Stumbled upon this problem to find the median ...Here is the solution

ArrayList ar = new ArrayList();
string con = @"Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=D:\temp\median.xlsx;Extended Properties='Excel 12.0;HDR=Yes;'";
using (OleDbConnection connection = new OleDbConnection(con))
{

connection.Open();
OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
using (OleDbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
ar.Add(int.Parse(dr[0].ToString()));
}
}
}

ar.Sort();

double ar_median = 0;
double ar_low = 0;
double ar_high = 0;

int lo = 0;
int hi = 0;

int arlo = 0;
int arhi = 0;

if ((ar.Count % 2) == 0)
{
ar_low = Math.Floor((double)(ar.Count + 1) / 2);
ar_high = Math.Ceiling((double)(ar.Count + 1) / 2);
}
else
{
ar_low = Math.Floor((double)(ar.Count) / 2);
ar_high = Math.Ceiling((double)(ar.Count) / 2);
}

lo = int.Parse(ar_low.ToString())-1;
hi = int.Parse(ar_high.ToString())-1;

arlo = int.Parse(ar[lo].ToString());
arhi = int.Parse(ar[hi].ToString());

double arav = Convert.ToDouble(arlo + arhi);

ar_median = Math.Round(arav / 2);

MessageBox.Show(ar_median.ToString());

Comments

Popular posts from this blog

Chess blogging - Chessbase is back with a bang !

In view of my keen interest in UI for applications and websites, I feel it necessary to promote an investigation of the new play-chess interface on www.chessbase.com . * It is minimalistic * Allows you to play as Guest * Allows you to choose from different time-controls * Allows you to test your skills vis-a-vis opponents from across the globe Have a look at this interface, that is impressive from a UI developers point-of-view, and very conducive for a chess player to test his wits !  Whoa !  😇😇😇

Tomas ♟️Bot pgn

I beat this bot with an ELO of 1200 in Fischer 960 Chess ♟️  [Event "Vs. Computer"] [Site "Chess.com"] [Date "2025-01-28"] [White "petrushka_googol"] [Black "Tomas"] [Result "1-0"] [WhiteElo "400"] [BlackElo "1200"] [TimeControl "-"] [SetUp "1"] [FEN "qrkbbnnr/pppppppp/8/8/8/8/PPPPPPPP/QRKBBNNR w HBhb - 0 1"] [Variant "Chess960"] [Termination "petrushka_googol won by checkmate"] 1. e3 d6 2. Bf3 Bd7 3. Ng3 c5 4. Nh3 Bxh3 5. gxh3 Ba5 6. c3 h5 7. b4 Bc7 8. bxc5 dxc5 9. Nxh5 Nf6 10. Nxg7 Rh7 11. Nf5 e6 12. Ne7+ Kd8 13. O-O-O a6 14. c4 Kxe7 15. Rg1 Rd8 16. d3 Rh6 17. Bg4 Nxg4 18. hxg4 Qb8 19. g5 Rh7 20. Qf6+ Ke8 21. h4 Rd6 22. f4 Rb6 23. Rh1 Ng6 24. h5 Bd8 25. Qc3 Nf8 26. g6 Rb1+ 27. Kd2 Rxd1+ 28. Kxd1 fxg6 29. Rg1 g5 30. Bg3 Be7 31. fxg5 Qd8 32. Be5 Rxh5 33. Bf6 Bxf6 34. gxf6 Rh1 35. Rxh1 Qb6 36. Qc1 Kd8 37. Rh8 e5 38. Rxf8+ Kc7 39. Rf7+ Kc8 40. Re7 Qxf6 41. Re8...

B Trees🌲🌳🌴 databases and CHESS☺

Binary trees are a specific version of decision trees that have two nodes,one parent and two children, and this concept is used in database programming with relational databases like SQL server for creating in clustered and non clustered indexes. Alpha beta pruning together with B tree searches makeup the basis of chess programming. I have played a program that reached its thought horizon and then had a bizarre response inspite of  having an advantage of one pawn♟️