how to Create a primitive type bool array with all elements initialized to true. Array elements with prime indices will remain true. All other array elements will eventually be set to false.
I am sorry, I don't quite get it.
Does the primitive type like this? Or use the new bool[n];
And how to initialize to all elements to true? bool p[] = true;
When I have to do something similar to this I just run trough the array and change every element to what I need for it to be. This can be done like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream.h>
int main(){
int n;
std::cin>>n; //Get number of elements, This can be preset.
bool p [n];
for (int i=0; i<n; i++) // Run the code for every element in array.
p[i] = true; // Set the value of element i to true
return 0;
}
And to set all of array elements without prime index to false:
1 2 3 4
for(int i = 0; i<n; i++){
if(i!=prime) // Here you check if index is different then prime. I'll let you think how to do this since I am not sure how to do it at this moment and I don't want to mislead you.
p[i] = false;
}