0

我收到一个错误,说“adjacencymatrix”没有在这个范围内使用”就在 main 的末尾(在最后的函数 makebond 之前)(注释的第 112 行“BROKEN LINE”)。为什么?很抱歉这很简单。我正在用 g++ ($ g++ ac -of) 编译。

继承人的代码:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

#define PI 3.1415926535897932384626433832795
#define sqr(x) ((x)*(x))
#define count 500

double density;
double volume;
int N;
double beta = 0.1;

double R = 5;
double rob = 1;

int dimension = 2;

double eps=0.1; // Increase in density
double mindensity = 0; // Minimum density
double maxdensity = 8; // max.dens (scaled for the sake of ensuring int()

int makebond(double x);

int main(){

    srand(time(0));

    for (int rho=mindensity;rho<=(maxdensity/eps);density++){

        N = floor(density*volume);

        double nodepositions[N][dimension];

        // Place nodes in volume (square side L, circle volume *R and obstacle *rob)
        for (int i=0;i<N;i++){
            int L = 5;

            double distancefromorigin;

            double x = (L*(rand()/RAND_MAX))-(L/2);
            double y = (L*(rand()/RAND_MAX))-(L/2);

            distancefromorigin = sqrt((x*x)+(y*y));

                if(distancefromorigin<R){
                    if(distancefromorigin>rob){
                        nodepositions[i][0] = x;
                        nodepositions[i][1] = y;
                    }
                }
        }

        double adjacencymatrix [N][N];

        double itzhak; //distance of node 1 from the centre
        double isaac; //distance of node 2 from the centre
        double vivaldi; //distance between node 1 and node 2
        double phi; // a function of the above 3 doubles (see later usage)
        double rubicon; // maximum distance nodes within the icecream can be apart before becoming visually indepdendent
        double maxtheta; // "in the icecream" means theta < maxtheta
        double theta; // angular displacement of inner point from the line bisecting the icecream

        // Create adjacency matrix (note alternative implementation using incidence lists)

        for (int i=0;i<N;i++){
            for (int j=0;j<N;j++){

                double x0 = nodepositions[i][0];
                double y0 = nodepositions[i][1];
                double x1 = nodepositions[j][0];
                double y1 = nodepositions[j][1];

                itzhak = sqrt(sqr(x0) + sqr(y0));
                isaac = sqrt(sqr(x1) + sqr(y1));
                vivaldi = sqrt(sqr(x0-x1) + sqr(y0-y1));
                phi = ((sqr(vivaldi)+sqr(itzhak)-sqr(isaac))/(2*vivaldi*itzhak));
                rubicon = ((itzhak*phi) - sqrt((sqr(rob)) - ((sqr(itzhak))*(1-sqr(phi)))));

                maxtheta = asin(rob/itzhak);
                theta = acos(phi);

                if (x0==x1 && y0==y1){
                    adjacencymatrix[i][j] = 0;
                }
                else{       
                    if (isaac<itzhak && theta<maxtheta) {           
                        if (vivaldi>rubicon){   
                            adjacencymatrix[i][j] = 0;}
                        else {
                            adjacencymatrix[i][j] = makebond(vivaldi);}             
                    }

                else{adjacencymatrix[i][j] = makebond(vivaldi);}
                }

            }
        }

    }

    FILE *datafc1;
    datafc1 = fopen("matrix.dat", "w");

    for (int ii = 0; ii<N; ii++){
        for (int jj = 0; jj<N; jj++){       

            int aaa;
            aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
            fprintf(datafc1,"%i", aaa);

        }
    }

    fclose(datafc1);

    return 0;
}

/////////////////////////////
////////////////
///////  --End Main--
////////////////
////////////////////////////

int makebond(double x){

    // This function takes in the euc. dist. between two nodes and draws a link with prob. H(r)

    double randomnumber = (rand()/RAND_MAX); // Random number between 0 and 1
    double hr = exp(-beta*sqr(x));// ***Connection function***

    int a = 1; // Number to be put into adjacency matrix

    if (randomnumber > hr){
        a = 0;
    }

    return a; //Returns 0 or 1 depending on prob. dist.
}
4

4 回答 4

2

adjacencymatrix在您的第一个for循环中声明,因此它在您使用它的最后一个位置之前超出范围,位于底部的打印输出循环中。

此外,您还有一条无用的using namespace std;线路。您的代码不包含任何包含std命名空间符号的标头。

于 2013-09-23T22:55:37.570 回答
1

您在第 57 行的代码:

    double adjacencymatrix [N][N];

在 for 循环内,在该循环外,adjacencymatrix未定义。

于 2013-09-23T22:56:12.310 回答
0

您的矩阵在第 11 行的 for 循环中定义。因此它超出了第 112 行的范围。

于 2013-09-23T22:58:13.287 回答
0
FILE *datafc1;
datafc1 = fopen("matrix.dat", "w");

for (int ii = 0; ii<N; ii++){
    for (int jj = 0; jj<N; jj++){

        int aaa;
        //error adjacencymatrix is declared in your first for loop
        aaa = adjacencymatrix[ii][jj];///////////////*******BROKEN LINE******
        fprintf(datafc1,"%i", aaa);

    }
}
于 2013-09-23T23:00:21.580 回答