Write a C++ console program with CODEBLOCKS to do the following: 1. Define an ar
ID: 672266 • Letter: W
Question
Write a C++ console program with CODEBLOCKS to do the following:
1. Define an array of ints with the ten elements { 0, 1,2,3, 4, 5, 6, 7, 8, 9 }.
2. Define a vector with those ten elements.
3. Define a list with those ten elements.
4. Define a second array, vector, and list, each initialized as a copy of the first array, vector, and list, respectively.
5. Increase the value of each element in the array by 2; increase the value of each element in the vector by 3; increase the value of each element in the list, by 5.
6.Print out a label and then the values of each of the containers
Explanation / Answer
#include<iostream>
#include<vector>
#include<list>
using namespace std;
int main()
{
int arr[10] = { 0, 1,2,3, 4, 5, 6, 7, 8, 9 };
vector<int> vectorArr(arr, arr + sizeof(arr) / sizeof(int) );
list<int> listArr(arr, arr + sizeof(arr) / sizeof(int) );
int secondArr[10];
memcpy(secondArr,arr,10);
vector<int> vectorCopy(vectorArr);
list<int> listCopy(listArr);
for(int i=0;i<10;i++)
arr[i] = arr[i]+2;
for (vector<int>::iterator it = vectorArr.begin(); it != vectorArr.end(); ++it)
*it = *it + 3;
for (list<int>::iterator it = listArr.begin(); it != listArr.end(); ++it)
*it = *it + 5;
cout << "Printing Array ";
for(int i=0;i<10;i++)
cout << arr[i] << " ";
cout << endl;
cout << "Printing vector ";
for (vector<int>::iterator it = vectorArr.begin(); it != vectorArr.end(); ++it)
cout << *it << " ";
cout << endl;
cout << "Printing list ";
for (list<int>::iterator it = listArr.begin(); it != listArr.end(); ++it)
cout << *it << " ";
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.