Data Structures with C++ Using STL”, William Ford and William Topp, 2002, Prenti
ID: 3758878 • Letter: D
Question
Data Structures with C++ Using STL”, William Ford and William Topp, 2002, Prentice Hall, 0-13-085850-1
CH11
Question 17. page 639
The following is part of a main program that includes the declaration of the integer array intArr:
int intArr[] = {15,7,32,12};
map<int, vector<int> >m;
int i, j, value;
for (i=0; i<4; i++)
{
value = intArr[i];
j=2;
while(value > 1)
if (value % j ==0)
{
m[intArr[i]].push_back(j);
value /=j;
}
else
j++;
}
a. Describe the action of each step in the for loop.
b. List each entry in the map by giving first the key component and then the contents of the corresponding vector component.
Explanation / Answer
Before that we know about Map and vector
Map is working with key and value pair
Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.
Vector is dynamic and growable array
in the for loop
i value reads from 0 to 3 means
i = 0, 1, 2, 3
array declared is {15, 7, 32, 12}
the size is 4
if i=0 value =15 read based on index
i=1 value =7
i=2 value =32
i=3 value=12
the value is greter than the 1 means here all the values are greater than the 1
value % j ==0 is this possible only two that is 32%2 ==0
12%2 = 0
sent to back
fails it is incremented to 1 on j
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.