C++ Programming, ONLY NEED ANSWER FOR 5-7. 1. Make the following code work in yo
ID: 3754339 • Letter: C
Question
C++ Programming, ONLY NEED ANSWER FOR 5-7.
1. Make the following code work in your environment.
2. Modify it so that you can time first two “for loops” in the code seperately from timing the third “for loop” in the code. This may require increasing the value of N to get a positive result.
3. Modify the display of the results so that each line only contains 10 numbers
4. Change the array type from int to char and comment on changes to run time.
5. Change the array type to bool and comment on changes to run time.
6. Change the static array in part A to a dynamic array and compare the run times
to the variations you tried in parts 1-5. Be sure to use types char and bool in your testing.
7. Change the static array in part A to a vector and compare the run times
to the variations you tried in parts 1-5. Be sure to use types char and bool in your testing.
#include <iostream.h>
static const int N = 1000;
int main()
{ int i, a[N];
for (i = 2; i < N; i++) a[i] = 1;
for (i = 2; i < N; i++)
if (a[i])
for (int j = i; j*i < N; j++) a[i*j] = 0;
for (i = 2; i < N; i++)
if (a[i]) cout << " " << i;
cout << endl;
}
Explanation / Answer
ANSWER 5
No significant changes were measured as average time comes out be almost same i.e., 47ms.
ANSWER 6
Average running time for int type array increased by 5ms while for bool type and char type array average running time remained the same.
int type dynamic array:
char type dynamic array:
bool type dynamic array:
ANSWER 7
Average running time of int type vector > Average running time of char type vector > Average running time of bool type vector
int vector:
char vector:
bool vector
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.