Best method for sum two arrays
a simple approach could be
$c = array_map(function () {
return array_sum(func_get_args());
}, $a, $b);
print_r($c);
Or if you could use PHP5.6, you could also use variadic functions like this
$c = array_map(function (...$arrays) {
return array_sum($arrays);
}, $a, $b);
print_r($c);
Output
Array
(
[0] => 11
[1] => 22
[2] => 16
[3] => 18
[4] => 3
)
Sum two arrays in single iteration
I know this is an old question but I was just discussing this with someone and we came up with another solution. You still need a loop but you can accomplish this with the Array.prototype.map().
var array1 = [1,2,3,4];
var array2 = [5,6,7,8];
var sum = array1.map(function (num, idx) {
return num + array2[idx];
}); // [6,8,10,12]
How to sum two arrays in another array in C?
What do you exactly want? Summation of each element from two arrays into a new third array? That's it right?
int main(int argc, char** argv) {
int v1[3],v2[3],v3[3];
for(int i = 0 ; i < 3; i++) {
printf("Type a number for v1 :\t");
scanf("%d", &v1[i]);
printf("Type a number for v2 :\t");
scanf("%d", &v2[i]);
// Add here
v3[i] = v1[i] + v2[i]; // Mind you that this could lead to integer overflow.
}
printf("\nResult Arr :\n");
for(int i = 0 ; i < 3; i++)
printf("%d\n", v3[i]);
}
Find the sum of two arrays
Well, all other answers are awesome for adding 2 numbers (list of digits).
But in case you want to create a program which can deal with any number of 'numbers'
,
Here's what you can do...
def addNums(lst1, lst2, *args):
numsIters = [iter(num[::-1]) for num in [lst1, lst2] + list(args)] # make the iterators for each list
carry, final = 0, [] # Initially carry is 0, 'final' will store the result
while True:
nums = [next(num, None) for num in numsIters] # for every num in numIters, get the next element if exists, else None
if all(nxt is None for nxt in nums): break # If all numIters returned None, it means all numbers have exhausted, hence break from the loop
nums = [(0 if num is None else num) for num in nums] # Convert all 'None' to '0'
digit = sum(nums) + carry # Sum up all digits and carry
final.append(digit % 10) # Insert the 'ones' digit of result into final list
carry = digit // 10 # get the 'tens' digit and update it to carry
if carry: final.append(carry) # If carry is non-zero, insert it
return final[::-1] # return the fully generated final list
print(addNums([6, 9, 8], [5, 9, 2])) # [1, 2, 9, 0]
print(addNums([7, 6, 9, 8, 8], [5, 9, 2], [3, 5, 1, 7, 4])) # [1, 1, 2, 7, 5, 4]
Hope that makes sense!
php, sum two array values
You can use array_keys
to get the unique from both of the array and then loop through keys to some them
$r = [];
$keys = array_keys($a1+$a2);
foreach($keys as $v){
$r[$v] = (empty($a1[$v]) ? 0 : $a1[$v]) + (empty($a2[$v]) ? 0 : $a2[$v]);
}
Working DEMO
How do I sum the respective elements of two arrays into another array in Javascript?
Use Array.map
(see MDN)
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 4, 5];
const sums = arr1.map((v, i) => v + arr2[i]);
document.querySelector('#result').textContent = JSON.stringify(sums);
<pre id="result"></pre>
C++ getting the sum of two array
You need to use loop
for (int i = 0; i < 10; i++) std::cout << A[i] + B[i] << "\n";
or if u want whole array you need to add both array into a variable using again loops if you are using c style arrays. After learning this try looking at c++ ways of doing this. this is c way of doing this.
C function to sum two arrays of numbers?
Is there a function like
memcpy()
, to add the values in arrays? Something likememconcatenate()
maybe?
The standard library doesn't have any such function.
If not, is there some equivalent way I could element-wise add the values of an array to another (other than using a loop) ?
I don't think there is a way to do that without using a loop.
Related Topics
Regex to Strip Comments and Multi-Line Comments and Empty Lines
Long Polling/Http Streaming General Questions
Compile a Referenced Less File into CSS with PHP Automatically
How to Force PHP to Use Strings for Array Keys
How to Remove the Url Protocol and Slash from User Input in PHP
Tcpdf Utf-8 Symbols Not Showing Up
Http_Build_Query with Same Name Parameters
Apache Mod Rewrite for Laravel
Notice: Undefined Offset: 0 In
Php: Get N-Th Item of an Associative Array
PHP Variable Interpolation VS Concatenation
Error 403 in Loading Resources Like CSS and Js in My Index.Php
How to Use PHP Array with SQL in Operator
Multiple Index Variables in PHP Foreach Loop
Counting Occurrence of Specific Value in an Array with PHP