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]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    list <int> L(7);
    list <int> L1(10);
    list <int> L2(5);

    for(list <int>::iterator i=L.begin(); i != L.end(); i++)  *i = rand()%10 ;

    cout<<"L= "; for(list <int>::iterator i=L.begin(); i != L.end(); i++) cout<<*i<<' ';
    
    
    for(list <int>::iterator i=L1.begin(); i != L1.end(); i++) *i=rand()%30;
    
    cout<<"\nL1= "; for(list <int>::iterator i=L1.begin(); i != L1.end(); i++) cout<<*i <<' ';
    
    
    for(list <int>::iterator i=L2.begin(); i != L2.end(); i++)  *i = rand()%10 ;

    cout<<"\nL2= "; for(list <int>::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["<<i<<"] = "<<L.front()<<"+"<<L1.front()<<"-"<<L2.front()<<"="<<L.front() +L1.front()-L2.front()<<endl;
	
	L.pop_back();
	L1.pop_back();
    L2.pop_back();
    
	i++;
}


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
include <list>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	std::list<int> L(7);
	std::list<int> L1(10);
	std::list<int> 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <list>
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	std::list<int> L(7);
	std::list<int> L1(10);
	std::list<int> 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
Topic archived. No new replies allowed.