Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reading a .csv file and getting and calculating avg
now i want print value's which are 10% above the avg value.
I am converting my data into Datatable.

What I have tried:

Static void Main(string[] args)
        {
  string csv_file_path = @"C:\Sample files\LP.csv";
            DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);  
 object  med= p.GetMedianFromDataTable(csvData, "Value");
            Console.ReadLine();
}
Posted
Updated 25-Mar-18 6:42am
v5
Comments
Patrice T 25-Mar-18 7:15am    
What is the problem?
Akhil Jain 25-Mar-18 7:27am    
how to print all the values which are 10% less or above than the medians
Patrice T 25-Mar-18 7:41am    
What is exactly your problem (in this code) ?
Use Improve question to update your question.
Akhil Jain 25-Mar-18 7:48am    
i don't know how to get all the values which are 10% above or 10% less than median
RickZeeland 25-Mar-18 8:31am    
You asked this question before and got an answer, please revisit your previous question !

1 solution

Average is average and median is median. Do not mix that!

There's only one case when average is equal to median and vice versa: for 2-element array when both values are the same, for example: {10,10}.

Check this:
C#
void Main()
{
	int[] ages = {100,22,13,43,25,61,17,80, 29}; 
	double median = Median(ages);
	
	double greaterThanMedian = median+median*.15;
	double lessThanMedian =  median-median*.15;
	
	var result = ages.Where(x=> x>lessThanMedian && x<greaterThanMedian)
		.ToList();
	
	foreach(var x in result)
		Console.WriteLine("{0}", x);	
}


public static double Median(int[] arr)
{
	if (arr==null) throw new Exception("An array cannot be null!");
	
	bool isEvenNumberOfValues = arr.Length % 2 == 0;
	int toSkip = isEvenNumberOfValues ? (arr.Length/2)-1 : arr.Length/2;
	int toTake = isEvenNumberOfValues ? 2 : 1;
	
	return arr.OrderBy(x=>x).Skip(toSkip).Take(toTake).Average();
}	


According to your code:
	var result = csvData.AsEnumerable()
		.Where(x=> x.Field<int>("Value")>=lessThanMedian && x.Field<int>("Value")<=greaterThanMedian)
		.ToList();
//or

	var result1 = csvData.Select(string.Format("(Value >= {0} AND Value <= {1})",lessThanMedian, greaterThanMedian))
		.ToList();



Good luck!
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900