- Home /
Math.Pow and Array Values
Is there a way where I can get the total of the values in an array combined, but the array value is to the a power that is incremented by one for every increase in the array size. Also, if the array value is 0 the value will be skipped example:
for (int Increment = 0; Increment < ArraySize; Increment++) {
if(TheArray[Increment] != 0) {
Math.Pow(TheArray[Increment], Increment);
}
}
Edit: I have realized I have forgotten to mention this, but the number being multiplied by is suppose to be multiplied by 2. The values in my array are dynamic, but say I have 5 values and they are
0 -- 1 * 2
1 -- 0 * 2 (skipped (2 * 0 = 0)),
2 -- 1 * 2,
3 -- 0 * 2 (skipped (2 * 0 = 0)),
4 -- 1 * 2,
What I want to happen is for the array row to be the power and the array's value to be then powered by the power and then the values added together.
2 ^ 0 = 1,
2 ^ 2 = 4,
4 ^of 2 = 16,
----------------
1 + 4 + 16 = 21
You mean sum of all values in an array like: [0, 1, 5, 4] => 0 (skipped) + $$anonymous$$ath.Pow(1, 1) + $$anonymous$$ath.Pow(5, 2) + $$anonymous$$ath.Pow(4, 3) = 90 ???
Yes, but ins$$anonymous$$d of having to write out $$anonymous$$ath.Pow(1,1) + $$anonymous$$ath.Pow(5,2) + $$anonymous$$ath.Pow(4,3) and have it add itself in a for-loop statement.
Answer by Bunny83 · Apr 14, 2019 at 09:45 PM
You original code is actually almost complete. All you need is a sum variable
double sum = 0;
for (int i= 0; i < TheArray.Length; i++) {
if(TheArray[i] != 0) {
sum += Math.Pow(TheArray[i], i);
}
}
You shouldn't use to complicated names for for-loop variables. Especially if they are used for different things. The variable is not an "increment" but just the index into the array.
May I ask what's the actual point of this calculation? It seems a bit strange. I looks a little bit like a generic implementation of the tayler series. Though it would be wrong the way it's handled.
I forgot to include that the array value gets multiplied by 2 then to the power. It is suppose to convert binary to integer numbers.
You want to convert a binary number into an integer? Than using Pow is the most inefficient way of doing so. Just do this:
int val = 0;
for (int i= 0; i < TheArray.Length; i++)
{
val |= TheArray[i] << i;
}
This assumes that the values in the array are either 0 or 1. Of course the values in the array are in reverse order. So the least significant bit is the one at index 0.
Is there a reason you use an array for the individual bits? Usually directly manipulating the bits in a single integer is just as simple as working with an array. To set bit 3 in an integer value, just do
val |= 1<<3;
To clear a bit just do
val &= ~(1<<3);
Of course the lease significant bit has the index 0 (just like in your array).
Your answer
Follow this Question
Related Questions
"For"-Loop only works for final element in array 1 Answer
How to convert and integer into an Array of his digits 4 Answers
Copy values between two classes in two lists. 1 Answer
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Using for loop to gather all waypoint objects near player 2 Answers