<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Probleme beim Zuweisen von Werten an Arrayelemente]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich bin vor kurzem von FORTRAN zu C++ umgestiegen und versuche nun eine Metropolis Monte Carlo Simulation für das Ising Modell zu schreiben.<br />
Dabei hat man ein Gitter von Spins, von denen jeder die Werte +1 und -1 annehmen kann. Mit Hilfe von Übergangsvorschriften und Zufallszahlen simuliert man dann, welche Werte die Spins des Gitters am &quot;liebsten&quot; annehmen.</p>
<p>Mein Spinsystem wird im Array</p>
<pre><code class="language-cpp">int spin[V]
</code></pre>
<p>abgespeichert. Das Problem ist nun, dass ich in <strong>Zeile 43 in main.cpp</strong> bei jedem Simulationsschritt einem Element des Arrays einen neuen Wert zuordnen will. Nur scheint das Array das irgendwie zu ignorieren. Es bleibt lange Zeit unverändert und dann auf einmal ändern sich alle Werte.</p>
<p>Nun debugge ich schon tagelang und es scheint mir alles richtig zu sein. Aber wo liegt dann der Programmierfehler? Vielleicht ist das ja für einen C++ Profi offensichtlich?!</p>
<p>Wenn ich ein kleines Testprogramm schreibe, in dem ein Array an eine Funktion übergeben wird und diese Funktion einzelne Elemente ändert, so funktioniert alles. Warum funktioniert das bei dem großen Porgramm nicht, obwohl ich es da genauso mache.</p>
<p>Falls es wichtig ist, ich benutze Ubuntu 11.10, gcc 4.4 und QtCreator</p>
<p>Bitte helft mir.</p>
<p>Danke schonmal!</p>
<p>Hier die Simulation:<br />
main.h</p>
<pre><code class="language-cpp">#ifndef MAIN_H
#define MAIN_H

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#include &lt;fstream&gt;
#include &lt;iomanip&gt;
#include &lt;time.h&gt;
using namespace std;

// SIMULATION AND SYSTEM PARAMETERS
const int dim = 2;
const int Lx = 10, Ly = 10, Lz = 1;
const int V = Lx*Ly*Lz;
const double betai = 0.05, betaf = 2.05;              // beta -&gt; beta*J
const int Nbeta = 5;
const double H = 0.0;                                 // H    -&gt; H/J
const int Nsweeps = 10, eqsweeps = 0;

// GLOBAL VARIABLES
int NNtab[V][2*dim];
double Rmntab[2*dim+1];
double Esum, Msum, Mabssum, sqEsum, sqMsum;

// FUNCTIONS
void Ising_MC(int spin[]);
void initialization();
void close();
int energy(int spin[], int site);
void NNchart();
void Rmnchart(double beta);
void eval(int spin[]);
void output(int spin[], double beta);

// CLASS DECLARATIONS
ofstream file_out, system_out;
clock_t start, end;

#endif // MAIN_H
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">//******************************************************//
//      METROPOLIS MC SIMULATION OF THE ISING MODEL     //
//******************************************************//

#include &lt;main.h&gt;

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PERFORMS THE MC SIMULATION ACROSS A SPECIFIC PARAMETER INTERVAL
int main(){
    int spin[V];
    double beta;

    fill(spin,spin+V,1);
    initialization();

    beta = betai;
    for (int kbeta = 0; kbeta &lt;= Nbeta; kbeta++){
        Rmnchart(beta);
        Ising_MC(spin);

        output(spin,beta);
        cout &lt;&lt; beta &lt;&lt; &quot;\n&quot;;
        if(Nbeta != 0) beta += fabs(betaf-betai)/Nbeta;
    }
    close();
    return 0;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// MC SIMULATION ROUTINE FOR CERTAIN PARAMETERS
void Ising_MC(int spin[]){
    int site;
    int dE;
    double urn,prob;

    for(int isw = 1; isw &lt;= Nsweeps; isw++){
        for(int iup = 0; iup &lt; V; iup++){

            site = rand() % V;
            dE = energy(spin,site);

            urn = (double)rand()/RAND_MAX;
            prob = Rmntab[dim-dE/2];
            if (urn &lt;= prob) spin[site] = -spin[site];

            // spin system output zu testzwecken --------------------------------
            spin[site] = 4;
            cout &lt;&lt; &quot;\n&quot; &lt;&lt;&quot;\n&quot;;
            for(int k = 0; k &lt; Lz; k++){
                for(int l = 0; l &lt; Ly; l++){
                    for(int m = 0; m &lt; Lx; m++){
                        cout &lt;&lt; setw(2) &lt;&lt; spin[k*l*m];
                    }
                    cout &lt;&lt; &quot;\n&quot;;
                }
                cout &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
            }
            //spin system test end-----------------------------------------------

        }
        if (isw &gt; eqsweeps) eval(spin);
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PROGRAM INITIALIZATION
void initialization(){
    start = clock();
    srand(time(NULL));
    NNchart();

    file_out.open(&quot;IsingMC_out.dat&quot;);
    system_out.open(&quot;IsingSystem_out.dat&quot;);
    cout &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;METROPOLIS MONTE CARLO SIMULATION OF THE ISING MODEL&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;Parameters&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; setw(10) &lt;&lt; &quot;Lx = &quot; &lt;&lt; Lx &lt;&lt; setw(10) &lt;&lt; &quot;Ly = &quot; &lt;&lt; Ly
             &lt;&lt; setw(10) &lt;&lt; &quot;Lz = &quot; &lt;&lt; Lz &lt;&lt; setw(10) &lt;&lt; &quot;V = &quot; &lt;&lt; V &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; setw(10) &lt;&lt; &quot;H = &quot; &lt;&lt; H  &lt;&lt; setw(15) &lt;&lt; &quot;Nsweeps = &quot; &lt;&lt; Nsweeps
             &lt;&lt; setw(20) &lt;&lt; &quot;eqsweeps = &quot; &lt;&lt; eqsweeps &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;METROPOLIS MONTE CARLO SIMULATION OF THE ISING MODEL&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::&quot;
             &lt;&lt; &quot;::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;Parameters&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; setw(15) &lt;&lt; &quot;Lx = &quot; &lt;&lt; Lx &lt;&lt; setw(15) &lt;&lt; &quot;Ly = &quot; &lt;&lt; Ly
             &lt;&lt; setw(15) &lt;&lt; &quot;Lz = &quot; &lt;&lt; Lz &lt;&lt; setw(15) &lt;&lt; &quot;V = &quot; &lt;&lt; V &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; setw(15) &lt;&lt; &quot;H = &quot; &lt;&lt; H  &lt;&lt; setw(30) &lt;&lt; &quot;Nsweeps = &quot; &lt;&lt; Nsweeps
             &lt;&lt; setw(15) &lt;&lt; &quot;eqsweeps = &quot; &lt;&lt; eqsweeps &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::&quot;
             &lt;&lt; &quot;::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; left &lt;&lt; setw(15) &lt;&lt; &quot;T&quot; &lt;&lt; setw(15) &lt;&lt; &quot;beta&quot; &lt;&lt; setw(15) &lt;&lt; &quot;E&quot; &lt;&lt; setw(15) &lt;&lt; &quot;M&quot;
                     &lt;&lt; setw(15) &lt;&lt; &quot;|M|&quot; &lt;&lt; setw(15) &lt;&lt; &quot;C&quot; &lt;&lt; setw(15) &lt;&lt; &quot;X&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;----------------------------------------------------------------------------&quot;
             &lt;&lt; &quot;----------------------&quot; &lt;&lt; &quot;\n&quot;;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PROGRAM CLOSING PROCEDURE
void close(){
    file_out.close();
    system_out.close();
    end = clock();
    cout &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;CPU time in sec: &quot; &lt;&lt; (double) end/CLOCKS_PER_SEC &lt;&lt; &quot;\n&quot;;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// CALCULATION OF THE ENERGY DIFFERENCE
int energy(int spin[], int site){
    int dE;
    dE = 0;
    for (int i = 0; i &lt; 2*dim; i++) {
        dE += spin[NNtab[site][i&rsqb;&rsqb;;
    }
    return dE;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// NEAREST NEIGHBOR ARRAY GENERATOR
void NNchart(){
    for (int l = 0; l &lt; V; l++){
        NNtab[l][0] = l-1;
        if(l % Lx == 0)     NNtab[l][0] += Lx;
        NNtab[l][1] = l+1;
        if((l+1) % Lx == 0) NNtab[l][1] -= Lx;

        if (dim &gt; 1) {
            NNtab[l][2] = l-Lx;
            if(l % (Lx*Ly) &lt; Lx)         NNtab[l][2] += Lx*Ly;
            NNtab[l][3] = l+Lx;
            if(l % (Lx*Ly) &gt;= Lx*(Ly-1)) NNtab[l][3] -= Lx*Ly;

            if (dim &gt; 2) {
                NNtab[l][4] = l-Lx*Ly;
                if(l % (Lx*Ly*Lz) &lt; Lx*Ly)         NNtab[l][4] += Lx*Ly*Lz;
                NNtab[l][5] = l+Lx*Ly;
                if(l % (Lx*Ly*Lz) &gt;= Lx*Ly*(Lz-1)) NNtab[l][5] -= Lx*Ly*Lz;
            }
        }
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ARRAY OF ALL POSSIBLE EXP VALUES
void Rmnchart(double beta){
    for (int i = 0; i &lt;= 2*dim; i++){
        Rmntab[i] = min(1.0, exp(-2*beta*(2*dim - 2*i + H)));
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// EVALUATION OF OBSERVABLES
void eval(int spin[]){
    double E, M;
    E = M = 0.0;

    for (int i = 0; i &lt; V; i++){
        for (int jNN =0; jNN &lt; 2*dim; jNN++){
            E += -1/2*spin[i]*spin[NNtab[i][jNN&rsqb;&rsqb;;
        }
        E += -H*spin[i];
        M += spin[i];
    }
    Esum += E;
    Msum += M;
    Mabssum += fabs(M);
    sqEsum += E*E;
    sqMsum += M*M;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// OUTPUT OF AVERAGES AND SPIN SYSTEM
void output(int spin[], double beta){
    int N = Nsweeps - eqsweeps;
    double C, X;

    C = beta*beta*(sqEsum/N - Esum*Esum/(N*N));
    X = beta*(sqMsum/N - Mabssum*Mabssum/(N*N));

    system_out &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;T = &quot; &lt;&lt; 1/beta &lt;&lt; &quot;\n&quot; &lt;&lt;&quot;\n&quot;;
    for(int k = 0; k &lt; Lz; k++){
        for(int l = 0; l &lt; Ly; l++){
            for(int m = 0; m &lt; Lx; m++){
                system_out &lt;&lt; setw(3) &lt;&lt; spin[k*l*m];
            }
            system_out &lt;&lt; &quot;\n&quot;;
        }
        system_out &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
    }

    file_out &lt;&lt; left
             &lt;&lt; setw(15) &lt;&lt; 1/beta &lt;&lt; setw(15) &lt;&lt; beta &lt;&lt; setw(15) &lt;&lt; Esum/(N*V)
             &lt;&lt; setw(15) &lt;&lt; Msum/(N*V) &lt;&lt; setw(15) &lt;&lt; Mabssum/(N*V) &lt;&lt; setw(15) &lt;&lt; C/V
             &lt;&lt; setw(15) &lt;&lt; X/V &lt;&lt;&quot;\n&quot;;

    Esum = Msum = Mabssum = sqEsum = sqMsum = 0.0;
    return;
}
</code></pre>
<p>und hier noch das Testprogramm, wo die Zuweisung zu Arrayelementen funktioniert:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;

using namespace std;

void assignelement(int array[]){

    array[1] = 2;
    array[2] = 3;
}

int main(){
    int array[3] = {1,1,1};

    assignelement(array);

    cout &lt;&lt; array[0] &lt;&lt; array[1] &lt;&lt; array[2]&lt;&lt;endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/300705/probleme-beim-zuweisen-von-werten-an-arrayelemente</link><generator>RSS for Node</generator><lastBuildDate>Wed, 12 Aug 2026 16:11:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/300705.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 08 Mar 2012 17:08:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Thu, 08 Mar 2012 17:08:57 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich bin vor kurzem von FORTRAN zu C++ umgestiegen und versuche nun eine Metropolis Monte Carlo Simulation für das Ising Modell zu schreiben.<br />
Dabei hat man ein Gitter von Spins, von denen jeder die Werte +1 und -1 annehmen kann. Mit Hilfe von Übergangsvorschriften und Zufallszahlen simuliert man dann, welche Werte die Spins des Gitters am &quot;liebsten&quot; annehmen.</p>
<p>Mein Spinsystem wird im Array</p>
<pre><code class="language-cpp">int spin[V]
</code></pre>
<p>abgespeichert. Das Problem ist nun, dass ich in <strong>Zeile 43 in main.cpp</strong> bei jedem Simulationsschritt einem Element des Arrays einen neuen Wert zuordnen will. Nur scheint das Array das irgendwie zu ignorieren. Es bleibt lange Zeit unverändert und dann auf einmal ändern sich alle Werte.</p>
<p>Nun debugge ich schon tagelang und es scheint mir alles richtig zu sein. Aber wo liegt dann der Programmierfehler? Vielleicht ist das ja für einen C++ Profi offensichtlich?!</p>
<p>Wenn ich ein kleines Testprogramm schreibe, in dem ein Array an eine Funktion übergeben wird und diese Funktion einzelne Elemente ändert, so funktioniert alles. Warum funktioniert das bei dem großen Porgramm nicht, obwohl ich es da genauso mache.</p>
<p>Falls es wichtig ist, ich benutze Ubuntu 11.10, gcc 4.4 und QtCreator</p>
<p>Bitte helft mir.</p>
<p>Danke schonmal!</p>
<p>Hier die Simulation:<br />
main.h</p>
<pre><code class="language-cpp">#ifndef MAIN_H
#define MAIN_H

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;
#include &lt;fstream&gt;
#include &lt;iomanip&gt;
#include &lt;time.h&gt;
using namespace std;

// SIMULATION AND SYSTEM PARAMETERS
const int dim = 2;
const int Lx = 10, Ly = 10, Lz = 1;
const int V = Lx*Ly*Lz;
const double betai = 0.05, betaf = 2.05;              // beta -&gt; beta*J
const int Nbeta = 5;
const double H = 0.0;                                 // H    -&gt; H/J
const int Nsweeps = 10, eqsweeps = 0;

// GLOBAL VARIABLES
int NNtab[V][2*dim];
double Rmntab[2*dim+1];
double Esum, Msum, Mabssum, sqEsum, sqMsum;

// FUNCTIONS
void Ising_MC(int spin[]);
void initialization();
void close();
int energy(int spin[], int site);
void NNchart();
void Rmnchart(double beta);
void eval(int spin[]);
void output(int spin[], double beta);

// CLASS DECLARATIONS
ofstream file_out, system_out;
clock_t start, end;

#endif // MAIN_H
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">//******************************************************//
//      METROPOLIS MC SIMULATION OF THE ISING MODEL     //
//******************************************************//

#include &lt;main.h&gt;

//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PERFORMS THE MC SIMULATION ACROSS A SPECIFIC PARAMETER INTERVAL
int main(){
    int spin[V];
    double beta;

    fill(spin,spin+V,1);
    initialization();

    beta = betai;
    for (int kbeta = 0; kbeta &lt;= Nbeta; kbeta++){
        Rmnchart(beta);
        Ising_MC(spin);

        output(spin,beta);
        cout &lt;&lt; beta &lt;&lt; &quot;\n&quot;;
        if(Nbeta != 0) beta += fabs(betaf-betai)/Nbeta;
    }
    close();
    return 0;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// MC SIMULATION ROUTINE FOR CERTAIN PARAMETERS
void Ising_MC(int spin[]){
    int site;
    int dE;
    double urn,prob;

    for(int isw = 1; isw &lt;= Nsweeps; isw++){
        for(int iup = 0; iup &lt; V; iup++){

            site = rand() % V;
            dE = energy(spin,site);

            urn = (double)rand()/RAND_MAX;
            prob = Rmntab[dim-dE/2];
            if (urn &lt;= prob) spin[site] = -spin[site];

            // spin system output zu testzwecken --------------------------------
            spin[site] = 4;
            cout &lt;&lt; &quot;\n&quot; &lt;&lt;&quot;\n&quot;;
            for(int k = 0; k &lt; Lz; k++){
                for(int l = 0; l &lt; Ly; l++){
                    for(int m = 0; m &lt; Lx; m++){
                        cout &lt;&lt; setw(2) &lt;&lt; spin[k*l*m];
                    }
                    cout &lt;&lt; &quot;\n&quot;;
                }
                cout &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
            }
            //spin system test end-----------------------------------------------

        }
        if (isw &gt; eqsweeps) eval(spin);
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PROGRAM INITIALIZATION
void initialization(){
    start = clock();
    srand(time(NULL));
    NNchart();

    file_out.open(&quot;IsingMC_out.dat&quot;);
    system_out.open(&quot;IsingSystem_out.dat&quot;);
    cout &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;METROPOLIS MONTE CARLO SIMULATION OF THE ISING MODEL&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;Parameters&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; setw(10) &lt;&lt; &quot;Lx = &quot; &lt;&lt; Lx &lt;&lt; setw(10) &lt;&lt; &quot;Ly = &quot; &lt;&lt; Ly
             &lt;&lt; setw(10) &lt;&lt; &quot;Lz = &quot; &lt;&lt; Lz &lt;&lt; setw(10) &lt;&lt; &quot;V = &quot; &lt;&lt; V &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; setw(10) &lt;&lt; &quot;H = &quot; &lt;&lt; H  &lt;&lt; setw(15) &lt;&lt; &quot;Nsweeps = &quot; &lt;&lt; Nsweeps
             &lt;&lt; setw(20) &lt;&lt; &quot;eqsweeps = &quot; &lt;&lt; eqsweeps &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;METROPOLIS MONTE CARLO SIMULATION OF THE ISING MODEL&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::&quot;
             &lt;&lt; &quot;::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;Parameters&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; setw(15) &lt;&lt; &quot;Lx = &quot; &lt;&lt; Lx &lt;&lt; setw(15) &lt;&lt; &quot;Ly = &quot; &lt;&lt; Ly
             &lt;&lt; setw(15) &lt;&lt; &quot;Lz = &quot; &lt;&lt; Lz &lt;&lt; setw(15) &lt;&lt; &quot;V = &quot; &lt;&lt; V &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; setw(15) &lt;&lt; &quot;H = &quot; &lt;&lt; H  &lt;&lt; setw(30) &lt;&lt; &quot;Nsweeps = &quot; &lt;&lt; Nsweeps
             &lt;&lt; setw(15) &lt;&lt; &quot;eqsweeps = &quot; &lt;&lt; eqsweeps &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::&quot;
             &lt;&lt; &quot;::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; left &lt;&lt; setw(15) &lt;&lt; &quot;T&quot; &lt;&lt; setw(15) &lt;&lt; &quot;beta&quot; &lt;&lt; setw(15) &lt;&lt; &quot;E&quot; &lt;&lt; setw(15) &lt;&lt; &quot;M&quot;
                     &lt;&lt; setw(15) &lt;&lt; &quot;|M|&quot; &lt;&lt; setw(15) &lt;&lt; &quot;C&quot; &lt;&lt; setw(15) &lt;&lt; &quot;X&quot; &lt;&lt; &quot;\n&quot;;
    file_out &lt;&lt; &quot;----------------------------------------------------------------------------&quot;
             &lt;&lt; &quot;----------------------&quot; &lt;&lt; &quot;\n&quot;;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// PROGRAM CLOSING PROCEDURE
void close(){
    file_out.close();
    system_out.close();
    end = clock();
    cout &lt;&lt; &quot;::::::::::::::::::::::::::::::::::::::::::::::::::::&quot; &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;CPU time in sec: &quot; &lt;&lt; (double) end/CLOCKS_PER_SEC &lt;&lt; &quot;\n&quot;;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// CALCULATION OF THE ENERGY DIFFERENCE
int energy(int spin[], int site){
    int dE;
    dE = 0;
    for (int i = 0; i &lt; 2*dim; i++) {
        dE += spin[NNtab[site][i&rsqb;&rsqb;;
    }
    return dE;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// NEAREST NEIGHBOR ARRAY GENERATOR
void NNchart(){
    for (int l = 0; l &lt; V; l++){
        NNtab[l][0] = l-1;
        if(l % Lx == 0)     NNtab[l][0] += Lx;
        NNtab[l][1] = l+1;
        if((l+1) % Lx == 0) NNtab[l][1] -= Lx;

        if (dim &gt; 1) {
            NNtab[l][2] = l-Lx;
            if(l % (Lx*Ly) &lt; Lx)         NNtab[l][2] += Lx*Ly;
            NNtab[l][3] = l+Lx;
            if(l % (Lx*Ly) &gt;= Lx*(Ly-1)) NNtab[l][3] -= Lx*Ly;

            if (dim &gt; 2) {
                NNtab[l][4] = l-Lx*Ly;
                if(l % (Lx*Ly*Lz) &lt; Lx*Ly)         NNtab[l][4] += Lx*Ly*Lz;
                NNtab[l][5] = l+Lx*Ly;
                if(l % (Lx*Ly*Lz) &gt;= Lx*Ly*(Lz-1)) NNtab[l][5] -= Lx*Ly*Lz;
            }
        }
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// ARRAY OF ALL POSSIBLE EXP VALUES
void Rmnchart(double beta){
    for (int i = 0; i &lt;= 2*dim; i++){
        Rmntab[i] = min(1.0, exp(-2*beta*(2*dim - 2*i + H)));
    }
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// EVALUATION OF OBSERVABLES
void eval(int spin[]){
    double E, M;
    E = M = 0.0;

    for (int i = 0; i &lt; V; i++){
        for (int jNN =0; jNN &lt; 2*dim; jNN++){
            E += -1/2*spin[i]*spin[NNtab[i][jNN&rsqb;&rsqb;;
        }
        E += -H*spin[i];
        M += spin[i];
    }
    Esum += E;
    Msum += M;
    Mabssum += fabs(M);
    sqEsum += E*E;
    sqMsum += M*M;
    return;
}
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// OUTPUT OF AVERAGES AND SPIN SYSTEM
void output(int spin[], double beta){
    int N = Nsweeps - eqsweeps;
    double C, X;

    C = beta*beta*(sqEsum/N - Esum*Esum/(N*N));
    X = beta*(sqMsum/N - Mabssum*Mabssum/(N*N));

    system_out &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;T = &quot; &lt;&lt; 1/beta &lt;&lt; &quot;\n&quot; &lt;&lt;&quot;\n&quot;;
    for(int k = 0; k &lt; Lz; k++){
        for(int l = 0; l &lt; Ly; l++){
            for(int m = 0; m &lt; Lx; m++){
                system_out &lt;&lt; setw(3) &lt;&lt; spin[k*l*m];
            }
            system_out &lt;&lt; &quot;\n&quot;;
        }
        system_out &lt;&lt; &quot;\n&quot; &lt;&lt; &quot;\n&quot;;
    }

    file_out &lt;&lt; left
             &lt;&lt; setw(15) &lt;&lt; 1/beta &lt;&lt; setw(15) &lt;&lt; beta &lt;&lt; setw(15) &lt;&lt; Esum/(N*V)
             &lt;&lt; setw(15) &lt;&lt; Msum/(N*V) &lt;&lt; setw(15) &lt;&lt; Mabssum/(N*V) &lt;&lt; setw(15) &lt;&lt; C/V
             &lt;&lt; setw(15) &lt;&lt; X/V &lt;&lt;&quot;\n&quot;;

    Esum = Msum = Mabssum = sqEsum = sqMsum = 0.0;
    return;
}
</code></pre>
<p>und hier noch das Testprogramm, wo die Zuweisung zu Arrayelementen funktioniert:</p>
<pre><code class="language-cpp">#include&lt;iostream&gt;

using namespace std;

void assignelement(int array[]){

    array[1] = 2;
    array[2] = 3;
}

int main(){
    int array[3] = {1,1,1};

    assignelement(array);

    cout &lt;&lt; array[0] &lt;&lt; array[1] &lt;&lt; array[2]&lt;&lt;endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2189870</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2189870</guid><dc:creator><![CDATA[*gt**gt**gt*APE*lt**lt**l]]></dc:creator><pubDate>Thu, 08 Mar 2012 17:08:57 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Thu, 08 Mar 2012 18:08:59 GMT]]></title><description><![CDATA[<p>Das ist furchtbar viel Code und furchtbar unübersichtlich. Konkret kann ich an Zeile 43 nix sehen und der Rest ist mir erst einmal zu lang. Aber es fällt auf dem ersten Blick auf: Du machst Fortran in C++. Und zwar im negativen Sinne:<br />
<a href="https://www.google.de/search?&amp;q=writing%20fortran%20in%20any%20language" rel="nofollow">Google: writing fortran in any language</a></p>
<p>Daraus resultieren zwei Probleme:<br />
1. Es ist furchtbar unübersichtlich. Variablen kannst du in C++ deklarieren, wo du sie brauchst, nicht am Anfang der Funktion. C++ unterstützt Objektorientierung von Haus aus. C++ kann Variablennamen die länger als 1 Zeichen sind. C++ kommt mit jeder Menge fertiger Algorithmen, die vieles abkürzen. C++ kommt mit jeder Menge fertiger Datenstrukturen die für alle praktischen Zwecke das richtige bieten. Insbesondere ein brauchbarer Ersatz für Arrays. In C++ brauchst du so gut wie nie globale Variablen.<br />
Ein Programm, dass in gutem C++ geschrieben ist, ist schon fast erschreckend robust gegen Programmierfehler, da viele Logikfehler und fast alle technischen Fehler gar nicht erst compilierbar sind.<br />
2. Manche Sprachfeatures funktionieren in C++ schlichtweg anders als in FORTRAN, insbesondere Arrays, die von C her kommen und im Vergleich mit anderen Datentypen ganz ungewohnte Eigenschaften haben. Kann gut sein, dass du da einen Fehler gemacht hast. Aber wie in 1 schon gesagt, hättest du wahrscheinlich gar keine Arrays gebraucht, sondern, je nachdem wie dein Programm genau funktioniert, std::array oder std::vector. Diese zählen wiederum zu den in 1 genannten Datenstrukturen, bei denen Fehler so gut wie ausgeschlossen oder, falls man doch mal einen macht, <strong>sehr</strong> viel leichter debugbar sind.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2189885</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2189885</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 08 Mar 2012 18:08:59 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Thu, 08 Mar 2012 18:25:25 GMT]]></title><description><![CDATA[<p>Wie siehst du das, dass das Array die falschen Werte enthält? Anhand deiner Testausgabe? Der Zugriff spin[k*l*m] ist ja wahrscheinlich nicht das, was du an der Stelle willst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2189889</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2189889</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Thu, 08 Mar 2012 18:25:25 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Thu, 08 Mar 2012 18:47:09 GMT]]></title><description><![CDATA[<blockquote>
<p>Wie siehst du das, dass das Array die falschen Werte enthält? Anhand deiner Testausgabe?</p>
</blockquote>
<p>Ja, die Ausgabe sieht so aus:</p>
<p>1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1</p>
<p>müsste aber eher so aussehen:</p>
<p>1-1 1 1 1<br />
1 1 1 1-1<br />
1-1 1 1 1<br />
-1-1 1-1 1<br />
1 1 1-1 1</p>
<blockquote>
<p>Du machst Fortran in C++. Und zwar im negativen Sinne:<br />
Google: writing fortran in any language</p>
</blockquote>
<p>Kennt denn jemand ein gutes Tutorial explizit um auf objektorientierte Programmierung umzusteigen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2189896</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2189896</guid><dc:creator><![CDATA[*gt**gt**gt*APE*lt**lt**l]]></dc:creator><pubDate>Thu, 08 Mar 2012 18:47:09 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Thu, 08 Mar 2012 19:06:54 GMT]]></title><description><![CDATA[<blockquote>
<blockquote>
<blockquote>
<p>APE&lt;&lt;&amp;l schrieb:<br />
Wie siehst du das, dass das Array die falschen Werte enthält? Anhand deiner Testausgabe?</p>
</blockquote>
</blockquote>
<p>Ja, die Ausgabe sieht so aus:</p>
<p>1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1<br />
1 1 1 1 1</p>
<p>müsste aber eher so aussehen:</p>
<p>1-1 1 1 1<br />
1 1 1 1-1<br />
1-1 1 1 1<br />
-1-1 1-1 1<br />
1 1 1-1 1</p>
</blockquote>
<p>Bashar schrieb:</p>
<blockquote>
<p>Der Zugriff spin[k*l*m] ist ja wahrscheinlich nicht das, was du an der Stelle willst.</p>
</blockquote>
<p>Das wird's sein. Da sollen bestimmt noch die Breiten des Arrays beim Berechnen des Index einfließen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2189898</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2189898</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 08 Mar 2012 19:06:54 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme beim Zuweisen von Werten an Arrayelemente on Fri, 09 Mar 2012 09:05:31 GMT]]></title><description><![CDATA[<p>Ah jetzt hab ichs kapiert. Ich war zu dumm mein Array richtig auszugeben.<br />
Danke für den Tipp!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2190028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2190028</guid><dc:creator><![CDATA[*gt**gt**gt*APE*lt**lt**l]]></dc:creator><pubDate>Fri, 09 Mar 2012 09:05:31 GMT</pubDate></item></channel></rss>