+ 1

Storing the sum of 2 arrays in another array

help please

13th Feb 2018, 12:46 PM
Mich
Mich - avatar
3 odpowiedzi
+ 5
Do you want to sum array1[x] + array2[x] into array3[x], or sum all elements of array1 + sum all elements of array2 then store the value in array3?
13th Feb 2018, 3:07 PM
Ipang
+ 5
@Mich, this is what I figured of the case, as you tagged vector, and at the moment I'm learning how to use it, I included a vector version too. I hope I understood what you mean correctly. #include <iostream> #include <vector> using namespace std; int main() { // *** Using array *** int array1[] {15,25,35,45,55}, array2[] {100,200,300,400,500}, array3[5]; for(unsigned int i = 0; i < 5; i++) array3[i] = array1[i] + array2[i]; cout << "Sum 2 arrays:\n"; for(unsigned int i = 0; i < 5; i++) cout << array3[i] << " "; cout << "\n\n"; // *** Using vector *** vector<int> v1 = {10,20,30,40,50}, v2 = {100,200,300,400,500}, v3; for(unsigned int i = 0; i < v1.size(); i++) v3.push_back(v1.at(i) + v2.at(i)); cout << "Sum 2 vectors:\n"; vector<int>::iterator it; for(it = v3.begin(); it != v3.end(); it++) cout << *it << " "; return 0; } Hth, cmiiw
13th Feb 2018, 11:36 PM
Ipang
+ 1
the first one you suggested
13th Feb 2018, 10:17 PM
Mich
Mich - avatar