Код: Выделить всё
void set(bool* a){
for(int i = 0; i< 7; i++){
a[i] = true;
}
}
int main() {
const int size = 7;
bool* a[size];
return 0;
}
< /code>
Теперь это работает, но если я попытаюсь сделать то же самое в своей программе, это не так. Вот основная функция: < /p>
#include
#include
#include
using namespace std;
int main(){
const int size = 7;
bool connection = false;
bool* c_set[size]; //pointer to a 1D array
bool* o_set[size];
bool** graph; //pointer to a 2D array of not set dimention
graph = new bool*[size];
for (int i = 0; i < size; i++){
graph[i] = new bool [size];
}
//filing the graph with edges
for(int i = 0; i < size; i++){
for(int j = i; j < size; j++){
//undirected edges, so the matrix is symmetric
if(i==j){graph[i][j] = false;}else{
graph[i][j] = graph[j][i] = (prob() < 0.19);
}
}
}
print_graph(graph,size);
connection = is_connected(c_set, o_set, graph, size);
cout
double prob(){
const int range_from = 0;
const int range_to = 1;
std::random_device rand_dev;
std::mt19937 generator(rand_dev());
std::uniform_int_distribution distr(range_from, range_to);
return distr(generator);
}
bool is_connected(bool* c_set, bool* o_set, bool** graph, const int size){
int old_size = 0, c_size = 0;
//initialization
for(int i = 0; i < size; i++){
c_set[i] = false;
o_set[i] = false;
};
o_set[0] = true;
while(c_size < size){
for(int i = 0; i < size; i++){
old_size = c_size;
//check which new nodes are reachable
for (int j = 0; j < size; j++){
o_set[j] = o_set[j] || graph[i][j];
}
//adding nodes to the closed set
if (o_set[i]&&(c_set[i]==false)){
c_set[i] = true;
c_size++;
}
if (old_size == c_size){
return false;
}
}
}
if(c_size == size){
return true;
}
return true;
}
void print_graph(bool** graph, const int size){
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
cout
Считается строкой для значений c_set и o_set:
connection = is_connected(c_set, o_set, graph, size);Может ли кто -нибудь помочь понять, почему я вспоминаю 1D -логический массив, рассматривается как 2D -массив?
Подробнее здесь: https://stackoverflow.com/questions/796 ... leans-in-c