The following is part of a main program that includes the declaration of the int
ID: 663254 • Letter: T
Question
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 ioop.
(b) List each entry in the map by giving first the key component and then the contents of
the corresponding vector component.
Explanation / Answer
int intArr[ ] = (15, 7, 32, 12};
map<int, vector<int> > m;
int i, j, value;
for(i = 0; i < 4; i++)
{
value = intArr[i]; // assign element at postion i to value
j = 2;
while(value > 1) // while value is greater than 1
if (value % j == 0) // check if value is divided by j
{
m[intArr[i]].push_back(j); // enter into the vector value j
value /= j; // value is equal to the quotient of value / j
}
else // if value is not divided by j
j++; // increment j
}
a) finding all the prime factors
b) 15-> 3, 5
7 -> 7
32-> 2
12-> 2, 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.