🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: be.comp.programming
8 messages
8 total messages Started by Matthias Demuzere Mon, 16 Dec 2002 18:07
waarde counter j
#6251
Author: Matthias Demuzere
Date: Mon, 16 Dec 2002 18:07
38 lines
1839 bytes
Hoi,

Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
orde...) Iemand aan idee waar hij die waarde vandaan haalt???
alvast bedankt

Matthias

-------------------------------------------
int j;

FILE *ifp;
  FILE *ofp;

  /*reading file*/

  ifp = fopen("../gjelbreen","r");

  /*reading data from file*/

  for (j=1;j<=MAXX;j++)

  {

   fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
   printf("%f",hb[j]);

  }


exit(0);

--
------------------------------------
Matthias Demuzere
O.L. Vrouwstraat 50
3000 Leuven
Re: waarde counter j
#6253
Author: MaG
Date: Mon, 16 Dec 2002 21:21
48 lines
2224 bytes
"Matthias Demuzere" <Matthias.demuzere@student.kuleuven.ac.be> schreef in
bericht news:1040062049.987345@seven.kulnet.kuleuven.ac.be...
> Hoi,
>
> Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
> geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> alvast bedankt
>
> Matthias
>
> -------------------------------------------
> int j;
>
> FILE *ifp;
>   FILE *ofp;
>
>   /*reading file*/
>
>   ifp = fopen("../gjelbreen","r");
>
>   /*reading data from file*/
>
>   for (j=1;j<=MAXX;j++)
>
>   {
>
>    fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
>    printf("%f",hb[j]);
>
>   }
>
>
> exit(0);
>

1) Bedoel je dat j die waarde heeft *na* het beeindigen van de loop en dat
die waarde abnormaal is
te opzichte van MAXX? Maar voor de exit(0)? Of is het na exit(0) ?
2) Dit stukje code is geen correct programma, geef eens jouw volledige code.
3) Hoe kom je aan die waarde?

Gokje:
Na exit(0) heeft j geen gedefinieerde waarde meer, maar sommige debuggers
zullen wel nog iets tonen bijv: 4067520

MaG
Re: waarde counter j
#6256
Author: Matthias Demuzere
Date: Tue, 17 Dec 2002 06:46
134 lines
4922 bytes

> > Hoi,
> >
> > Ik heb het volgende probleem. Als ik dit stukje programma laat lopen,
dan
> > geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> > orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> > alvast bedankt
> >
> > Matthias
> >
> > -------------------------------------------
> > int j;
> >
> > FILE *ifp;
> >   FILE *ofp;
> >
> >   /*reading file*/
> >
> >   ifp = fopen("../gjelbreen","r");
> >
> >   /*reading data from file*/
> >
> >   for (j=1;j<=MAXX;j++)
> >
> >   {
> >
> >    fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
> >    printf("%f",hb[j]);
> >
> >   }
> >
> >
> > exit(0);
> >
>
> 1) Bedoel je dat j die waarde heeft *na* het beeindigen van de loop en dat
> die waarde abnormaal is
> te opzichte van MAXX? Maar voor de exit(0)? Of is het na exit(0) ?
> 2) Dit stukje code is geen correct programma, geef eens jouw volledige
code.
> 3) Hoe kom je aan die waarde?
>
> Gokje:
> Na exit(0) heeft j geen gedefinieerde waarde meer, maar sommige debuggers
> zullen wel nog iets tonen bijv: 4067520
>
Idd, 't is zoiets! Ik ken er allemaal niet veel van, maar ik wou mn
programma stuk per stuk testen, dit door die exit() toe te voegen. Hieronder
staat de rest van mijn programma, alle, tot aan die exit toch....

/*standard libraries*/

#include <stdio.h>  /*standard input-output*/
#include <stdlib.h> /*standard C library*/
#include <math.h>  /*math library*/


/*definieren van de parameters*/


#define GA   9.81      /*gravity acceleration (m/s�)*/
#define K       (2.1*SEC_PER_YEAR)       /*heat conductivity (W/mK)*/
#define RHO      910.0        /*ice density (kg/m�)*/
#define G       (0.042*SEC_PER_YEAR)     /*geothermal heat flux (*/
#define CP       2009.0       /*specific heat capacity (J/kgK)*/
#define TABS     273.15       /* absolute temperature (K)*/
#define TSEA                  /*temperature at sea level (K)*/
#define BETA  0.000000098     /*constant parameter (K/Pa)*/
#define ADIAT     (-0.012)       /*Factor regarding temperature/height
(K/m)*/
#define A        1.0e-16                     /*flow parameter */

#define TIMETOT  50000.0       /*total calculation time (year)*/
#define MAXX  122       /*maximum number of elements*/
#define MAXXARRAY   (MAXX+1)
#define MAXZ  51
#define MAXZARRAY (MAXZ+1)
#define SEC_PER_YEAR 31556926     /*numbers of seconds in 1 year*/
#define DT   10.0
#define DZETA  0.02      /*interval on the vertical axes*/
#define DX    10.000      /*interval on the horizontal axes (m)*/




/*main program*/

int main(int argc, char **argv)   /*arguments to communicate with the main
system*/

 {
  int i,j,k,tlapse;
  double t0[MAXZARRAY][MAXXARRAY], t1[MAXZARRAY][MAXXARRAY],
w[MAXZARRAY][MAXXARRAY], u[MAXZARRAY][MAXXARRAY];
  double h[MAXXARRAY];      /*ice sheet thickness*/
  double s[MAXXARRAY];      /*Height of surface*/
  double hb[MAXXARRAY];      /*height of basis of glacier*/
  double x[MAXXARRAY];
  double m[MAXXARRAY];
  double zeta[MAXZARRAY];
  double tsurf[MAXXARRAY];
  double tpmp[MAXZARRAY][MAXXARRAY];
  double wsurf[MAXXARRAY];
  double f[MAXZARRAY];
  double g[MAXZARRAY];
  double alfa[MAXZARRAY];
  double beta[MAXZARRAY];
  double gamma[MAXZARRAY];
  double delta[MAXZARRAY];
  double taud[MAXXARRAY];

  FILE *ifp;
  FILE *ofp;

  /*reading file*/


  ifp = fopen("../gjelbreen","r");

  /*reading data from file*/

  for (j=1;j<=MAXX;j++)

  {

   fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
   printf("%f",hb[j]);

  }


exit(0);
Re: waarde counter j
#6258
Author: Bart.Vanhauwaert
Date: Tue, 17 Dec 2002 09:17
49 lines
1992 bytes
Matthias Demuzere <Matthias.demuzere@student.kuleuven.ac.be> wrote:
> Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
> geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> alvast bedankt

> Matthias

> -------------------------------------------
> int j;

> FILE *ifp;
>   FILE *ofp;

>   /*reading file*/

>   ifp = fopen("../gjelbreen","r");

>   /*reading data from file*/

>   for (j=1;j<=MAXX;j++)

>   {

>    fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
>    printf("%f",hb[j]);

>   }

Zonder het volledige programma te zien, vind ik je for loop toch een
beetje twijfelachtig. Normaal telt men in C van 0 tot MAXX-1 en niet van
1 tot MAXX. Dus schrijf je

for (j=0; j<MAXX; ++j)
	...


Als je de variable x bv als 

double x[MAXX];

hebt gedeclareerd, dan ga je dus een waarde schrijven buiten de limieten
van je array. En het kan wel eens heel goed dat op die plaats j staat en
je daarom dus onvoorziene toestanden krijgt.

cu bart

-- 
http://www.irule.be/bvh/
Re: waarde counter j
#6372
Author: Matthias Demuzere
Date: Wed, 18 Dec 2002 16:31
370 lines
9891 bytes
zelfs als ik dat aanpas gaat het nog niet;...ik zal es de rest van mijn
programma meesturen, maar voorlopig is het enkel het inlezen van de data dat
belangrijk is...zolang hij dat niet doet kan ik toch ni verder....

--
------------------------------------
Matthias Demuzere
O.L. Vrouwstraat 50
3000 Leuven

<Bart.Vanhauwaert@nowhere.be> schreef in bericht
news:u3qmta.3e1.ln@ns1.irule.be...
> Matthias Demuzere <Matthias.demuzere@student.kuleuven.ac.be> wrote:
> > Ik heb het volgende probleem. Als ik dit stukje programma laat lopen,
dan
> > geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> > orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> > alvast bedankt
>
> > Matthias
>
> > -------------------------------------------
> > int j;
>
> > FILE *ifp;
> >   FILE *ofp;
>
> >   /*reading file*/
>
> >   ifp = fopen("../gjelbreen","r");
>
> >   /*reading data from file*/
>
> >   for (j=1;j<=MAXX;j++)
>
> >   {
>
> >    fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
> >    printf("%f",hb[j]);
>
> >   }
>
> Zonder het volledige programma te zien, vind ik je for loop toch een
> beetje twijfelachtig. Normaal telt men in C van 0 tot MAXX-1 en niet van
> 1 tot MAXX. Dus schrijf je
>
> for (j=0; j<MAXX; ++j)
> ...
>
>
> Als je de variable x bv als
>
> double x[MAXX];
>
> hebt gedeclareerd, dan ga je dus een waarde schrijven buiten de limieten
> van je array. En het kan wel eens heel goed dat op die plaats j staat en
> je daarom dus onvoorziene toestanden krijgt.
>
> cu bart
>
> --
> http://www.irule.be/bvh/

zelfs als ik dat aanpas gaat het nog niet;...ik zal es de rest van mijn
programma meesturen, maar voorlopig is het enkel het inlezen van de data dat
belangrijk is...zolang hij dat niet doet kan ik toch ni verder....

/*standard libraries*/

#include <stdio.h>  /*standard input-output*/
#include <stdlib.h> /*standard C library*/
#include <math.h>  /*math library*/


/*definieren van de parameters*/


#define GA   9.81      /*gravity acceleration (m/s�)*/
#define K       (2.1*SEC_PER_YEAR)       /*heat conductivity (W/mK)*/
#define RHO      910.0        /*ice density (kg/m�)*/
#define G       (0.042*SEC_PER_YEAR)     /*geothermal heat flux (*/
#define CP       2009.0       /*specific heat capacity (J/kgK)*/
#define TABS     273.15       /* absolute temperature (K)*/
#define TSEA                  /*temperature at sea level (K)*/
#define BETA  0.000000098     /*constant parameter (K/Pa)*/
#define ADIAT     (-0.012)       /*Factor regarding temperature/height
(K/m)*/
#define A        1.0e-16                     /*flow parameter */

#define TIMETOT  50000.0       /*total calculation time (year)*/
#define MAXX  122       /*maximum number of elements*/
#define MAXXARRAY   (MAXX+1)
#define MAXZ  51
#define MAXZARRAY (MAXZ+1)
#define SEC_PER_YEAR 31556926     /*numbers of seconds in 1 year*/
#define DT   10.0
#define DZETA  0.02      /*interval on the vertical axes*/
#define DX    10.000      /*interval on the horizontal axes (m)*/




/*main program*/

int main(int argc, char **argv)   /*arguments to communicate with the main
system*/

 {
  int i,j,k,tlapse;
  double t0[MAXZARRAY][MAXXARRAY], t1[MAXZARRAY][MAXXARRAY],
w[MAXZARRAY][MAXXARRAY], u[MAXZARRAY][MAXXARRAY];
  double h[MAXXARRAY];      /*ice sheet thickness*/
  double s[MAXXARRAY];      /*Height of surface*/
  double hb[MAXXARRAY];      /*height of basis of glacier*/
  double x[MAXXARRAY];
  double m[MAXXARRAY];
  double zeta[MAXZARRAY];
  double tsurf[MAXXARRAY];
  double tpmp[MAXZARRAY][MAXXARRAY];
  double wsurf[MAXXARRAY];
  double f[MAXZARRAY];
  double g[MAXZARRAY];
  double alfa[MAXZARRAY];
  double beta[MAXZARRAY];
  double gamma[MAXZARRAY];
  double delta[MAXZARRAY];
  double taud[MAXXARRAY];

  FILE *ifp;
  FILE *ofp;

  /*reading file*/

  ifp = fopen("../gjelbreen","r");

  /*reading data from file*/

  for (j=0;j<MAXX-1;j++)

  {

   fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
   printf("%f",hb[j]);

  }


exit(1);

----------------------------------------------------------------------------
-------------------------------------

/*constructing matrix,going from top to bottom, evolving downstream*/

  /*
  initializing temperature and velocity profile
  i=1: top of de ice column at height H
  i=MAXZ: bottom of the ice column at height 0
  j=1: most upper part of the ice mass on the horizontal axes
  j=MAXX: lowest part of the ice mass on the horizontal axes
  */


  for(j=2;j<=MAXX-1;j++)

  {

   /*parameters evolving in x-direction*/

   h[j]=s[j]-hb[j];

   /*calculating mass balance and temperature at sea surface */

   if (s[j]<1000)

   {

    m[j]=0.50-(double)(0.00025*hb[j]);  /*mass balance is lineair with
elevation < 1000m*/
    tsurf[j]=257.4-(double)(0.0051*hb[j]); /*TSURF is lineair with elevation
< 1000m*/

   }

   else

   {
    m[j]=(double)(0.678*exp((-0.00106)*hb[j]));   /*exponential decrease of
the mass balance for elevation > 1000m*/
    tsurf[j]=266.4-(double)(0.0143*hb[j]); /*TSURF is lineair with elevation
> 1000m*/

   }

   /*vertical speed at surface*/

   wsurf[j]=(double)(-m[j]);

   /*driving stress used in equation for horizontal speed*/

   taud[j]=(double)(-RHO*GA*h[j]*((s[j+1]-s[j-1])/2*DX));



   for(i=1;i<=MAXZ;i++)

   {

    /*parameters evolving in "z"-direction*/

    zeta[i]=(double)((i-1)*DZETA);

    w[i][j]=wsurf[j]*(1+(double)((i-1)*DZETA));
    u[i][j]=((1.0-pow(zeta[i],4.0))*((2.0*A*h[j])/5.0)*pow(taud[j],3));


   }

   for(i=1;i<=MAXZ;i++)

   {

    /*boundary conditions at ice divide*/

    t1[i][1]=233.15;
    u[i][1]=-u[i][3]; /*at point i,1, u is measured in the opposite
direction*/
    w[i][1]=w[i][3]; /*the vertical speed is in the same direction*/
   }

  }


  /*

  time loop

  */

  tlapse =(int)(TIMETOT/DT+0.5);

  for(k=1;k<=tlapse;k++)

  {

   /*temperature calculation*/

   for(j=2;j<=MAXX-1;j++)

   {

    /*define f,g at top*/

    f[1]=0;
    g[1]=TSEA+(ADIAT*s[j]);

    for(i=2;i<MAXZ-1;i++)

    {
     /*calculate alfa,beta,gamma,delta to solve the implicit equation*/

     alfa[i]=-(K*DT/RHO*CP*pow(h[j],2)*pow(DZETA,2))+(w[i][j]/
       h[j]*DZETA)-(u[i][j]*(s[j+1]-s[j-1])/(2.0*h[j]*DX*DZETA))-
       (u[i][j]*zeta[i]*(h[j+1]-h[j-1])/(2.0*DX*DZETA*h[j]));

     beta[i]=1.0+(K*DT/(RHO*CP*pow(h[j],2)*pow(DZETA,2)))-
       (w[i][j]/h[j]*DZETA)+(u[i][j]*(s[j+1]-s[j-1])/2.0*h[j]*DX*DZETA)
       +(u[i][j]*zeta[i]*(h[j+1]-h[j-1])/2.0*h[j]*DZETA*DX)+(u[i][j]/DX);

     gamma[i]=K*DT/CP*pow(h[j],2)*pow(DZETA,2);

     delta[i]=t0[i][j]+(zeta[i]*GA*(s[j+1]-s[j-1])*(u[i][j]-u[i-1][j])/
        CP*2.0*DX*DZETA)+((u[i][j]/DX)*t0[i][j-1]);


     f[i]=((-gamma[i])/((alfa[i]*f[i-1])+beta[i]));
     g[i]=(delta[i]-(alfa[i]*g[i-1])/(alfa[i]*f[i-1]+beta[i]));

     /*calculating temperature*/

     t0[i][j]=f[i]*t1[i][j]+g[i];


     /*explicit method*/


/*t1[i][j]=t0[i][j]+((K*DT)/(RHO*CP*H[j]*H[j]))((t0[i-1][j-1]-(2*t0[i][j])+t
0[i+1][j+1])

/(DZETA*DZETA))+((t0[i][j]-t0[i-1][j])/DZETA)((w[i][j]/H[j])-(u[i][j]*

((1.0/H[j])*((S[j+1]-S[j-1])/(2.0*DX)-(zeta[i]*((H[j+1]-H[j-1])/(2.0*DX)))))
)
         -u[i][j]*((t0[i][j]-t1[i][j-1])/DX)+(zeta[i]*GA*(S[j+1]-S[j-1])*(u[
i][j]-u[i-1][j]))
         /(CP*2.0*DX*DZETA));*/

     /*T can't be higher than pressure melting point*/

     tpmp[i][j]=TABS-(double)(BETA*RHO*GA*h[j]*zeta[i]);

     if (t1[i][j]>tpmp[i][j])

     t1[i][j]=tpmp[i][j];

    }

    /*calculate boundary conditions for t and velocity at the end of the ice
mass*/

    for (i=MAXZ-1;i>=1;i--)

    {
     t1[i][MAXX]=t1[i][MAXX-1];
     u[i][MAXX]=u[i][MAXX-1];
     w[i][MAXX]=w[i][MAXX-1];

    }


    /*boundary conditions*/

    t1[1][j]=tsurf[j];
    t1[MAXZ][j]=(DZETA*G*h[j]/K)+t0[MAXZ-1][j];

   }

   /*at every time step,basal temperatures must be displayed on the screen*/

   printf("Time=%.0f year --- Basal temp=%fC\n",((float)(k-1)*DT),
      (t1[MAXZ][MAXX]-TABS));

   /*replacing old temperature at t by new at t+1*/

   for (j=1;j<=MAXX;j++)

   {
    for (i=1;i<=MAXZ;i++)

    {

     t0[i][j]=t1[i][j];

    }

   }

  }

  /*writing the output on file outfp*/

  ofp=fopen("tprofout.xls","w");

  for (j=1;j<=MAXX;j++)

  {

   for (i=1;i<=MAXZ;i++)

   {

    fprintf(ofp,"%f\t%f\t%f\n%f\n",x[j],t0[i][j],w[i][j],u[i][j]);
   }

  }

  fclose(ofp);
  return (0);

 }
Re: waarde counter j
#6382
Author: Roel Schroeven
Date: Thu, 19 Dec 2002 10:47
13 lines
1698 bytes
Matthias Demuzere wrote:
> Hoi,
> 
> Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
> geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> orde...) Iemand aan idee waar hij die waarde vandaan haalt???

Hoe kom je aan die waarde van j? Via een debugger, of door een extra 
printf in de code, of wat?

--
"Codito ergo sum"
Roel Schroeven
Re: waarde counter j
#6384
Author: Jack D
Date: Thu, 19 Dec 2002 11:48
46 lines
2139 bytes
Matthias Demuzere wrote on 16-12-2002 19:07:

> Hoi,
> 
> Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
> geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> alvast bedankt
> 
> Matthias
> 
> -------------------------------------------
> int j;
> 
> FILE *ifp;
> FILE *ofp;
> 
> /*reading file*/
> 
> ifp = fopen("../gjelbreen","r");
> 
> /*reading data from file*/
> 
> for (j=1;j<=MAXX;j++)
> 
> {
> 
>  fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);
>  printf("%f",hb[j]);
> 
> }
> 
> 
> exit(0);

Hoe ziet die gjelbreen file eruit? Misschien zit hier het probleem. Fscanf
verwacht ascii waarden en misschien zitten ze opgeslagen als floating point
dumps. Ik vermoed dat het ASCII zal zijn, maar je weet nooit.

Checken of (ifp != null) is ook altijd aan te raden (eventueel asserteren).

Een laatste optie waaraan ik dacht is dat je probeert de debuggen in een
geoptimaliseerde build, dan is het niet abnormaal dat de compiler waardes
van variabelen gaat upfucken.

Jacko
Re: waarde counter j
#6385
Author: Luc Kumps
Date: Thu, 19 Dec 2002 14:03
21 lines
1729 bytes
"Matthias Demuzere" <Matthias.demuzere@student.kuleuven.ac.be> wrote in
message news:1040062049.987345@seven.kulnet.kuleuven.ac.be...
> Hoi,
>
> Ik heb het volgende probleem. Als ik dit stukje programma laat lopen, dan
> geeft hij een waarde aan mn j van 4067520 (of toch zoiets in die grootte
> orde...) Iemand aan idee waar hij die waarde vandaan haalt???
> alvast bedankt


WANNEER wordt de waarde van j zo groot?
HOE weet je dat ze zo groot wordt, want je code bevat geen enkele printf van
j?

>    fscanf(ifp,"%lf%lf%lf",&x[j],&hb[j],&s[j]);

Zou je niet beter testen of de return value van fscanf gelijk is aan 3, d.i.
het aantal waarden dat je wilt lezen?

Luc K
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads