Adding a factor to 2 values - c#

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)

Related

How to divide two entered numbers in C#? [duplicate]

This question already has answers here:
How can I divide two integers to get a double?
(9 answers)
Closed 28 days ago.
`This is my second week of my first Computer Science class so I apologize for what is a very basic question:
"Design and implement a program that asks the user to enter the number of programming majors and the total number of students in a class. The program should display that percentage of the class that is majoring in programming. "
I've spent about four hours on this now and I thought I was getting there using past problems I've solved and changing up the coding but this is what I have and it just gives me zero for an answer no matter what.
Any help would be greatly appreciated!
https://dotnetfiddle.net/P6onoF
Is the code I am using
I just get zero for an answer no matter what I enter`
Right.
You shouldn't be asking these questions here because the whole point of going to school is to actually learn it instead of asking the solution, BUT, been there, done that... so here we go :D
There's a couple of things you're doing wrong.
integers "don't divide" (they do, but they sort of don't), you won't get decimal values from them so 1 divided by 2 is 0 although mathematically is 0.5.
If you're gonna use int you need to cast the final to a double, you're using int total = programmingMajors/studentsTotal; so this is an integer. If you have a total of 10 students, with 5 majors. 5/10 = 0.5 with decimals, 0 in integer. Change it to (double)majors/total and you'll have the 0.5 you need.
You're not really getting the percentage of the students just dividing the value. To get the percentage you need to use rule of three.
If 1000 students are 100% of the students, X majors are Y% of them.
e.g. 1000 students. 50 majors.
1000 students = 100 percent.
50 students = X
1000x = 100 * 50
1000x = 5000
x = 5000 / 1000
x = 5
In one single line would be var result = (100 * majors) / total
When using types such as int, doubles and decimals always check their range. Use this microsoft ref. After changing to double and use rule of three.
I ended up not showing how to do it in your code.
// just change the last line to this.
Console.WriteLine("The percent is: " + (100 * programmingMajors) / studentsTotal);
I would suggest write the formula out on a piece of paper first so one has an idea of what one is trying to code.
Also, as mentioned integer values do not have a decimal place, so consider using a different data type such as 'double'.
Also, in terms of display, there is an output formatter 'P' which can be used to format numbers as a percentage.

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.

how to calculate proportional ratio

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.

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

Finding 2 or more numbers having the given number as GCF

I don't want to find the GCF of given numbers. I use Euclidean for that. I want to generate a series of numbers having a given GCF. For example if I choose 4, I should get something like 100, 72 or 4, 8 etc.,
Any pointers would be appreciated.
A series of pairs of numbers having N as a GCF is {N,N}, {N,2N}, {N,3N}, ....
In fact, any set consisting of N and 1 or more multiples of N has N as its GCF.
1.Maybe this question can be better answered at http://math.stackexchange.com
2.Just construct the numbers you are interested in by multiplying the numbers that are not factors of the GCD. for your example of given GCD=4 that means
$k_1=4$ the GCD itself
$k_2=4 * 2$ since 4 does not divide 2
$k_3=4 * 3$ since 4 does not divide 3
$not k_4=4 * 4$ since 4 divides 4 but
$ k_4=4 * 5$ since 4 does not divide 5 etc.
If 4 is the input, you want a list of numbers whose greatest common factor is 4. You can ensure this by making 4 the only factor in the entire series. Therefore, you multiply the number (4) by all primes to ensure that.
prime-list = 3, 5, 7, 11, 13, 17
gcf-list for 4 -> (3*4)12, (4*5)20, (4*7)28, (4*11)44, (4*13)52, (4*17)68, ...
This will give you a list such that the GCF of any two numbers is 4
Choose a set of numbers that are pairwise-independent (that is gcd(x,y) = 1 for every x<>y in the set). Multiply each number by your target GCD.
I realize that this is an old question but I am going to provide my own answer along with an explanation of how I got there. First, let's call the GCF n.
Initially I would have suggested doing something like picking random integers and multiplying them each by n to get the set of numbers, this would of course give you numbers evenly divisible by n but not necessarily numbers with a GCF of n. If the integers happened to all have a GCF other than '1' then the GCF of the resulting set would actually have a GCF of n times that number, not n. That being said multiplying n by a set of integers seems the best way of ensuring that each number in the set is at least divisible by n
One option would be to make one of those numbers 1 but that would reduce the randomness of the set as n would always be in the resulting set.
Next you could use some prime numbers and multiply them by n but that would also reduce randomness as there would be less possible numbers, and the numbers don't actually need to be prime, just co-prime (GCF = 1 for the entire set)
You could also pick a set of numbers where each pair of numbers were co-prime but again, the entire set needs to be co-prime not co-prime pair-wise (which would be pretty processor intensive with larger sets).
So if you are going for fairly random numbers I would start by determining how many numbers you want in the set (whether that is randomly determined or predetermined) and then generating one less than that number completely 'randomly'. I would then compute the common prime factors for those numbers and then pick a random number that does not have any of those prime factors. Merely ensuring it does not have the same GCF is not sufficient as the GCF could have common factors to the final number. It only requires one number in a set that does not have any of the same prime factors as the other numbers in the set to make the GCF of that set '1'. I would then take that set of numbers and multiply each by n to get the set of numbers you want.

Categories

Resources