Dynamisches Array: Am Stück füllen...



  • Hey,

    Hab folgendes Programm:

    #include <stdio.h>
    #include <stdlib.h>
    
    unsigned int iStencilSpace=7;
    unsigned int iNrStencil = 3;
    
    double *CentralizeLengths(double *Delta);
    
    int main()
    {
        unsigned int ctr;
    
        double *Delta, *Lengths;
        Delta=(double *)calloc(iNrStencil*iStencilSpace,sizeof(double));
        Lengths=(double *)calloc(iNrStencil*(iStencilSpace+1)/2,sizeof(double));
    
        for(ctr=0;ctr<iNrStencil*iStencilSpace;ctr++) Delta[ctr]=0.5;
    
        for(ctr=0;ctr<iNrStencil;ctr++)
            {
                Lengths+=ctr*(iStencilSpace+1)/2;
                Lengths=CentralizeLengths(Delta+ctr*iStencilSpace);
            }
    
        Lengths-=iNrStencil*(iStencilSpace+1)/2;
        for(ctr=0;ctr<(iNrStencil+1)*(iStencilSpace+1)/2;ctr++) printf("%2d Länge %+f (%p)\n",ctr, Lengths[ctr], &Lengths[ctr]);
    
        return 0;
    }
    
    double *CentralizeLengths(double *Delta)
        {
            unsigned int ctr;
            double Add;
            double *Tmp, *tLengths;
            Tmp=(double *)calloc(iStencilSpace,sizeof(double));
            tLengths=(double *)calloc((iStencilSpace+1)/2,sizeof(double));
    
            Tmp[0]=0;Add=0;
            for(ctr=1;ctr<iStencilSpace;ctr++)
                {
                    Add+=Delta[ctr-1];
                    Tmp[ctr]=Add;
                }
    
            Add=Tmp[((iStencilSpace+1)/2)-1];
            for(ctr=0;ctr<iStencilSpace;ctr++)
                {
                    Tmp[ctr]-=Add;
                }
    
            tLengths[0]=Tmp[0];
            for(ctr=2;ctr<iStencilSpace;ctr+=2)
                {
                    tLengths[ctr/2]=Tmp[ctr];
                }
    
            return tLengths;
        }
    

    Und als Output bekomme ich:

    0 Länge -1.500000 (0x00217150)
     1 Länge -0.500000 (0x00217158)
     2 Länge +0.500000 (0x00217160)
     3 Länge +1.500000 (0x00217168)
     4 Länge +0.000000 (0x00217170)
     5 Länge +0.000000 (0x00217178)
     6 Länge -1.500000 (0x00217180)
     7 Länge -0.500000 (0x00217188)
     8 Länge +0.500000 (0x00217190)
     9 Länge +1.500000 (0x00217198)
    10 Länge +0.000000 (0x002171a0)
    11 Länge +0.000000 (0x002171a8)
    12 Länge -1.500000 (0x002171b0)
    13 Länge -0.500000 (0x002171b8)
    14 Länge +0.500000 (0x002171c0)
    15 Länge +1.500000 (0x002171c8)
    

    Die Nullen dürfen in dieser Liste aber überhaupt nicht sein ? Wie mach ich das ? 😕

    Winn



  • Meinst Du die Nullen hinten an den Nachkommastellen, also die hier:
    +1.500000?

    Wenn Du die weg haben willst solltest Du die Ausgabe mit printf() formatieren, z. B. %+.2f (das wären dann nur 2 Nachkommastellen).



  • Nein, die mein ich ich nicht 😉 Ich meine die Nullen bei Position 4/5 und 10/11. Das Problem liegt wohl irgendwie an der Alloziierung von Speicher, weiß aber so gar nicht, wie ich das eindimensionale Array komplett fülle, ohne die Nullöcher. Könnte das ganze zwar auch mit Structs machen, dort funktioniert es auch, aber die möchte ich halt nicht :p

    Hätte also lieber dieses Aussehen:

    0 Länge    -1.500000 (0x00217150)
    1 Länge    -0.500000 (0x00217158)
    2 Länge    +0.500000 (0x00217160)
    3 Länge    +1.500000 (0x00217168)
    4 Länge    -1.500000 (0x00217180)
    5 Länge    -0.500000 (0x00217188)
    6 Länge   +0.500000 (0x00217190)
    7 Länge   +1.500000 (0x00217198)
    8 Länge   -1.500000 (0x002171b0)
    9 Länge   -0.500000 (0x002171b8)
    10 Länge +0.500000 (0x002171c0)
    11 Länge +1.500000 (0x002171c8)
    

    Winn



  • Hab den Fehler 😃

    Die Schleife muß folgendermaßen aussehen:

    for(ctr=0;ctr<iNrStencil;ctr++)
            {
                printf("Pos %d\n", ctr*((iStencilSpace+1)/2));
                Tmp=CentralizeLengths(Delta+ctr*iStencilSpace);
                for(unsigned int ctr=0;ctr<(iStencilSpace+1)/2;ctr++) *(Lengths+ctr)=*(Tmp+ctr);
                Lengths+=((iStencilSpace+1)/2);
            }
    

    Tmp ist dabei double Pointer.

    Greetz Winn


Anmelden zum Antworten