IN C++ Need all 7 Simple Questions Anwsered. Will Upvote. 1. Make the following
ID: 3751016 • Letter: I
Question
IN C++ Need all 7 Simple Questions Anwsered. Will Upvote.
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
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
int* a = NULL;
int n; // Size needed for array
cin >> n; // Read in the size
a = new int[n];
for (int i=0; i<n; i++) {
a[i] = 0; // Initialize all elements to zero.
}
#include
int main()
{
int *a=NULL; // Pointer to int, initialize to nothing.
int n;
cin >> n;
a = new int[n]; // Allocate n ints and save ptr in a.
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;
}