### list

help me understand how to add, multiply or make any other simple math. operations with lists STL. elements Sum=list[i]+list1[i]...listN[i]

 ``123456789101112131415161718192021222324252627282930`` `````` list L(7); list L1(10); list L2(5); for(list ::iterator i=L.begin(); i != L.end(); i++) *i = rand()%10 ; cout<<"L= "; for(list ::iterator i=L.begin(); i != L.end(); i++) cout<<*i<<' '; for(list ::iterator i=L1.begin(); i != L1.end(); i++) *i=rand()%30; cout<<"\nL1= "; for(list ::iterator i=L1.begin(); i != L1.end(); i++) cout<<*i <<' '; for(list ::iterator i=L2.begin(); i != L2.end(); i++) *i = rand()%10 ; cout<<"\nL2= "; for(list ::iterator i=L.begin(); i != L.end(); i++) cout<<*i<<' '; i=1; cout<<"Enter n="; cin>>n; while(!n) //tried using erase() but it doesn't return a bool { cout<<"\n["<

Last edited on
I'm not really sure what're after - but perhaps:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````include #include #include #include #include int main() { srand(static_cast(time(nullptr))); std::list L(7); std::list L1(10); std::list L2(5); std::cout << " L= "; for (auto& i : L) std::cout << std::setw(3) << (i = rand() % 10) << ' '; std::cout << "\nL1= "; for (auto& i : L1) std::cout << std::setw(3) << (i = rand() % 30) << ' '; std::cout << "\nL2= "; for (auto& i : L2) std::cout << std::setw(3) << (i = rand() % 10) << ' '; size_t n {}; std::cout << "\n\nEnter n="; std::cin >> n; while (n--) { int sum {}; if (!L.empty()) { sum += L.front(); L.pop_front(); } if (!L1.empty()) { sum += L1.front(); L1.pop_front(); } if (!L2.empty()) { sum += L2.front(); L2.pop_front(); } std::cout << sum << '\n'; } }``````

Last edited on
thanks!
Or possibly like this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include #include #include int main() { srand(static_cast(time(nullptr))); std::list L(7); std::list L1(10); std::list L2(5); std::cout << " L= "; for (auto& i : L) std::cout << std::setw(3) << (i = rand() % 10) << ' '; std::cout << "\nL1= "; for (auto& i : L1) std::cout << std::setw(3) << (i = rand() % 30) << ' '; std::cout << "\nL2= "; for (auto& i : L2) std::cout << std::setw(3) << (i = rand() % 10) << ' '; size_t n {}; std::cout << "\n\nEnter n= "; std::cin >> n; std::cout << "Sum "; for (auto it = L.begin(), it1 = L1.begin(), it2 = L2.begin(); n--;) { int sum {}; if (it != L.end()) sum += *it++; if (it1 != L1.end()) sum += *it1++; if (it2 != L2.end()) sum += *it2++; std::cout << std::setw(3) << sum << ' '; } std::cout << '\n'; }``````

 ``` L= 5 5 0 7 9 7 4 L1= 25 7 2 2 0 19 28 27 29 0 L2= 9 4 5 2 7 Enter n= 5 Sum 39 16 7 11 16 ```

Last edited on
Registered users can post here. Sign in or register to post.