average

Can I calculate the average of these numbers?

人走茶凉 提交于 2019-12-03 13:28:53
I was wondering if it's possible to calculate the average of some numbers if I have this: int currentCount = 12; float currentScore = 6.1123 (this is a range of 1 <-> 10). Now, if I receive another score (let's say 4.5), can I recalculate the average so it would be something like: int currentCount now equals 13 float currentScore now equals ????? or is this impossible and I still need to remember the list of scores? The following formulas allow you to track averages just from stored average and count, as you requested. currentScore = (currentScore * currentCount + newValue) / (currentCount + 1

Average function without overflow exception

这一生的挚爱 提交于 2019-12-03 10:56:55
.NET Framework 3.5. I'm trying to calculate the average of some pretty large numbers. For instance: using System; using System.Linq; class Program { static void Main(string[] args) { var items = new long[] { long.MaxValue - 100, long.MaxValue - 200, long.MaxValue - 300 }; try { var avg = items.Average(); Console.WriteLine(avg); } catch (OverflowException ex) { Console.WriteLine("can't calculate that!"); } Console.ReadLine(); } } Obviously, the mathematical result is 9223372036854775607 ( long.MaxValue - 200 ), but I get an exception there. This is because the implementation (on my machine) to

Calculate average of column from MYSQL query

天涯浪子 提交于 2019-12-03 09:19:17
Ok experts...I've got a table that I am trying to calculate the average of the values in a column. Here is my lookup: $gameswon = mysql_query("SELECT SUM(P1_Score) AS value_sum FROM tblMatches Where P1_ID LIKE '".$playerid."'"); Any idea how I can determine the average (sum of values / total rows)? Thanks for your help. Obviously it is SELECT AVG(P1_Score) So in your case: $gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum FROM tblMatches WHERE P1_ID LIKE '".$playerid."'"); Try using AVG() aggregate function instead of SUM $gameswon = mysql_query("SELECT AVG(P1_Score) AS value_sum FROM

Calculate average or take in an ArrayList as a parameter to a function

放肆的年华 提交于 2019-12-03 09:07:15
Is there a built-in method to calculate the average of an integer ArrayList? If not, can I make a function that will do that by taking in the name of the ArrayList and returning its average value? Óscar López It's really simple: // Better use a `List`. It is more generic and it also receives an `ArrayList`. public static double average(List<Integer> list) { // 'average' is undefined if there are no elements in the list. if (list == null || list.isEmpty()) return 0.0; // Calculate the summation of the elements in the list long sum = 0; int n = list.size(); // Iterating manually is faster than

Mysql AVG to ignore zero

此生再无相见时 提交于 2019-12-03 06:44:08
问题 I need to perform an avg on a column, but I know that most of the values in that column will be zero. Out of all possible rows, only two will probably have positive values. How can I tell mySQL to ignore the zeros and only average the actual values? 回答1: Assuming that you might want to not totally exclude such rows (perhaps they have values in other columns you want to aggregate) SELECT AVG(NULLIF(field ,0)) from table 回答2: You could probably control that via the WHERE clause: select avg(

Generate a random number with max, min and mean(average) in Java

柔情痞子 提交于 2019-12-03 05:49:06
问题 I need to generate random numbers with following properties. Min should be 200 Max should be 20000 Average(mean) is 500. Optional: 75th percentile to be 5000 Definitely it is not uniform distribution, nor gaussian. I need to give some left skewness. 回答1: Java Random probably won't work because it only gives you normal(gaussian) distributions. What you're probably looking for is an f distribution (see below). You can probably use the distlib library here and choose the f distribution. You can

How to calculate the average color of a UIImage?

霸气de小男生 提交于 2019-12-03 05:11:52
问题 I want to build an app that lets the user select an image and it outputs the "average color". For example, this image: The average color would be a greenish/yellowish color. At the moment, I got this code: // In a UIColor extension public static func fromImage(image: UIImage) -> UIColor { var totalR: CGFloat = 0 var totalG: CGFloat = 0 var totalB: CGFloat = 0 var count: CGFloat = 0 for x in 0..<Int(image.size.width) { for y in 0..<Int(image.size.height) { count += 1 var rF: CGFloat = 0, gF:

Ruby on Rails field average?

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:36:41
Is there an easy way to obtain the average of an attribute in a collection? For instance, each user has a score. Given a collection of user(s) (@users), how can you get the average score for the group? Is there anything like @users.average(:score)? I think I came across something like this for database fields, but I need it to work for a collection... For your question, one could actually do: @users.collect(&:score).sum.to_f/@users.length if @users.length > 0 Earlier I thought, @users.collect(&:score).average would have worked. For database fields, User.average(:score) will work. You can also

how to select, average and sort in mysql table

不羁岁月 提交于 2019-12-03 00:05:09
问题 i have a table in mySql like in this picture and i want to write a query which result will group by LESSON column, and add new row which is average value of LESSON column and sum CNT column values.... for this query i use this one i use this query but it gives result like in picture 3 and i cant sort by PERC in this case select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS union all select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG

How to find the lowest, highest and average values in a listbox [closed]

一个人想着一个人 提交于 2019-12-02 23:31:32
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I'm trying to create a program that calculates and displays the highest, lowest and average value of items in a listbox (items generated from a txt file). I finally figured out how to load a text file to the listbox. I have been searching for clues for about an hour and all my attempts have brought me to a dead