how to Create a primitive type bool array with all elements initialized to true.

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;

Thanks so much!
Last edited on
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;
}


S.
Perhaps use std::vector<bool> array( n, true) ; instead?


1
2
3
4
5
//...
     int n;
     std::cin>>n;             //Get number of elements, This can be preset.      
     bool p [n];
     //... 


This is not C++.
thanks ssegota and JLBorges !!
But bool p [n]; < the n has an error , said expression must have a constant value

And I'm not quite have I learnt std::vector<bool> array( n, true) ; this before...sorry!

But anyway, thanks!
Topic archived. No new replies allowed.