how to calculate proportional ratio - c#

First of all pardon me to raise this question here (not sure). Not good in maths so need help from others to understand how to calculate.
I have to calculate proportional ratio score. For doing that i am taking two input values
ValueA = 3
ValueB = 344.
To find the percentage of the proportional ratio ((ValueB-ValueA)/ValueA )*100)
that formula gives me the score 11366.6.
Now i have to match with proportional percentage against with following table,
no idea how to match with percentage
for example the score comes around 43.12 % then i will pick the value 5 (>40 -50)
% Ratio Score
0 0
≤10 1
>10 – 20 2
>20 – 30 3
>30 – 40 4
>40 – 50 5
>50 – 60 6
>60 – 70 7
>70 – 80 8
>80 – 90 9
>90 – 100 10

your formula is of (as you can see by the 11366.6 percentage) - it should be
100.0*(ValueB-ValueA)/(double)ValueB
this will give you values in between 0 and 100 percent if ValueB is always bigger than ValueA (if not use):
100.0*Math.Abs(ValueB - ValueA)/(double)Math.Max(ValueA, ValueB)
based on the table your score should than be simply:
var score = (int)Math.Ceiling(percentage / 10.0)

You should swap value a and value b of you get percentages bigger than 100. By the way, finding the proportional value is not unique and the formula you have provided is one way to do that. I guess Valuea/valueb is also a possibility for example.

Related

Is there a method for finding what number is needed to achieve an increment in an average

I am working on a personal project, and I need to calculate what increase in numbers will bump the average to the next increment, I am able to do this long winded with if statements but wondered if there is already an algorithm or method for this.
Example
8 numbers averaging 750.7
which numbers need increasing to get to 751
Not really a programming problem, maybe there are simpler Maths formulas, but the following works.
If you want to increase each number by the same amount then:
Multiply the average value you want to have by the number of elements
751 * 8 = 6008
Minus the sum of your existing elements and Divide by the number of elements
6008 - 6005.6 = 2.4
2.4 / 8 = 0.3
Each number needs to be increased by 0.3 to make your average 751.
If you want to just increment 1 number to increase your average then:
Multiply the average value you want to have by the number of elements
751 * 8 = 6008
minus all the existing numbers except the one you want to increase / last value.
This will leave you with the new last value you should use.

Building up a matrix dynamically in the xy plane

I have to read some data sequentially(from a file) and put the data into a matrix. I don't know the rank of the matrix initially. For example consider the data is plotted on an x, y plane with years on the Y axis and increments in the x axis. At first the data came in for 1990 with 3 increments
year increment(1991) increment(1992) increment(1993)
1990 12 25 35
Note that I will only know about the increments after reading the data line. So next is 1989 with 4 increments. So it should be
year increment(1990) increment(1991) increment(1992) increment(1993)
1989 23 33 43 53
1990 0 12 25 35
Note that when the new data came in another increment year came in the y axis(1990).As there is no increment year of 1990 for year 1990 this has to be filled with zero or kept it empty, but the
In the end I have to create a matrix. For example
year increment(1990) increment(1991) increment(1992) increment(1993)
1989 23 33 43 53
1990 0 12 25 35
1991 0 0 23 33
To build up the matrix, the difficult part is I don't know the years/increments initially, I will only know after reading the entire data. I would like to plot the matrix while reading the data so that I can avoid more than one pass through the data.
The placement of the matrix in the xy axis will be only known after the entire data is processed!
Any suggestions?
I quite like the sparse matrix solution, but you could use a version of http://en.wikipedia.org/wiki/Dynamic_array. Dynamic arrays are arrays that you resize when they get too full. Resizing is expensive, but if you increase the size by a constant factor every time you resize the cost of resizing works out so that the total cost is still O(n) if the final size has n elements.
To use dynamic arrays for this, you could create two dynamic arrays for each row, one growing with years larger than those seen so far, and one growing with years smaller than those seen so far (so with years in decreasing order along the array).
Another way to do this would be to create a single area of storage for the matrix, with only the central section used, so there is always space to add entries in any direction. You would then have to check that increasing the size of this storage by a constant factor when you were about to run over the edges would lead to a total cost of at most O(n). I suspect that it would, but the constant factors might not be very good.
You can build it as a sparse matrix with SortedList<int, SortedList<int, int>>

random at min&max values , enlight me

While i'm trying to code basic lottery app for myself ( note that i'm really beginner on programming especially c#), a guy on StackOverflow said to me
rnd.Next(1, 50 * 7) % 50 // Randoming like that will be increase to chance of getting 1 and 49
rnd.Next(1, 50 ) // instead of this
I am really wondering how can we test it ? Can we rely on this tests ? Please enlight me
The last example will get a uniform distribution between 1 and 49 (inclusive). That is, the same chance for any number between (and including) 1 and 49.
The first example is much more tricky. It will first create any number between 1 and 349. The modulo 50 maps the number onto the interval 0-49 (including 0 and 49).
We now introduce the possibility to get 0 - if the random number is 50, 100, 150, 200, 250 or 300.
We can also get number 1-49 through N+0, N+50, N+100, N+150, N+200, N+250, N+300
That is, 6 chances to get 0 and 7 to get any other number.
The conclusion is that the first example will give a random number betwen 0-49 (inclusive) with slightly less chance of 0 than for the other numbers.

Rounding value to .5

There are the obvious quirks of Math.Round but is there a way to make Math.Round fulfill this type of manipulation for a rating system.
If greater than .0 and less than or equal to .5, display half a star
If greater than .5 and less than or equal to .0 display whole star
So obviously a half star would be .5 and a whole start would be the next whole value.
I don't know of a rounding method to go to half whole numbers.
Should I just write if statements to control my rounding?
**Edit/Solution
From the below answer I came up with.
double roundedRating = (Math.Ceiling(2 * currentRating)) / 2;
I'd recommend multiplying by two, performing Math.Ceiling, and dividing by two to get to the nearest half.
You're going to want to make sure that you end up performing your checks against integers, rather than floating point numbers.
Start by multiplying the number by 2. Continue doing this until it's an integer value (no value in the decimal part).
Now, continuously divide by 2 until you end up with a number that's less than or equal to the original number. If the result decimal part is greater than .0 and less than or equal to .5, display half a star. If it's greater than .5 and less than or equal to +.0, display a whole star.
Actually, go with matt's answer. ; )
Can this work?
Multiply the number by 10 e.g. 0.1x10, 0.2x10 to get n
Math.Ceil(n / 5) / 2
where n = 1, 2, 3 instead of - .1, .2, .3
examples:
1,2,3,4,5 = 1/2 = 0.5
6,7,8,9,10 = 2/2 = 1
11,12,13,14,15 = 3/2 = 1.5
If efficiency is no issue, the following approach could be used:
Number *= 10;
Number % 10 = remainder;
if(remainder <=5 && remainder != 0)
//Half star
else
//Whole star
However, that code is kinda ugly, but I think it gives you the general idea.
double roundedRating = (int)(currentRating * 2D + 0.999999999999999) / 2D

Adding a factor to 2 values

A method calculates the percentage of 2 given in points (for example 15/20 and 16/20) and returns the total %.
But the problem is when i try to add weight to those values.
Meaning standard weight factor 1 and weight factor 2 for both values are set on 1 (normal % calculation).
But how exactly do i add weights to them?
Assuming i want to add a weight factor of 2 to the first value and leave the second one to standard(1). (So a certain score is more important then a other, hence the % changes.)
Calculation with integers
input 15 and 16 (score on /20)
With default weight factors (1 & 1) the overal percentage would be 77
If the weight factors are changed to 2 (for 15) and standard 1 (for 16) the total % would be 76.
What would be the best way to implement this, cannot find anything about this.
Regards.
Firstly,
((15/20)+(16/20)) / 2 = 0.775 (77.5%)
instead of 77% (you shouldn't round until you perform a final calculation, e.g. for a grading system, different assignment types would have different weights and systems should never round until they display a final percentage.)
To weight one amount as 2 instead of 1, you would take the amount into account twice:
((15/20)+(15/20)+(16/20)) / 3 = 0.766
(76.6%)
(I know this because I have great knowledge of Pearson's PowerSchool)

Categories

Resources