Board index » delphi » Please help with this problem!!ARRAY problem

Please help with this problem!!ARRAY problem

Hi..my name is afrezal. I've got problem with the ARRAY in PASCAL. In my
program, I've got a constant array of Integer(12 number). Fine. This
array I use to produce an another 12 numbers after doing some
calculations. That's ok I've done that. The problem is I've got an input
file which is also an array of real numbers and the program that I wrote
need to read this input file so it can produced another 12 outputs.

Basically this is what I'm doing;
---------------------------------------------------------------------------------------------------

Program Constarray(input,output);

{Uses
CRT,Dos;}

Const
A:array[1..12] of Integer =
(17,47,75,105,135,162,198,228,258,288,318,344);
degree=(180.0/pi);{change rad into deg}
rad=(pi/180.0);   {change deg into rad}
Gsc=1367; {Solar Constant}
day=(24*3600);{day in second}
{pies=-33.57;}

type
mystring = string[127];
{windata = array [1..12] of Real;}

Var X:Integer;
pies:Real;
i:1..12;
Delta:Real;
Deltadeg:Real;
Sinrad:Real;
Sindeg:Real;
Comegar,Omegar,Omega,thetar,theta,Part1,Part2,Part3,H0:Real;
deltar,piesr:real;
Hbar:array[1..12] of Integer;{monthly average daily radiation of a
particular day's radiation}
Kt
:Real;
house,{weather description climatic data}
results{echo of description then results for each run}
:text;

Function Tand(deltar:Real):Real;
Begin
Tand:=Sin(deltar)/Cos(deltar);
end;

Function Tanp(piesr:Real):Real;
Begin
Tanp:=Sin(piesr)/Cos(piesr);
end;

Function ArcCos (Comegar : DOUBLE) : DOUBLE; (* inverse cosine using TP
arctan     *)
Begin                                        (* answer returned in
radians         *)
  If Comegar = 0.00 THEN
    ArcCos := Pi / 2.00
  Else
    If Comegar < 0.00 THEN
      ArcCos := Pi - Arctan(Sqrt(1.00 - Sqr(Comegar)) / Abs(Comegar))
    Else
      ArcCos := Arctan(Sqrt(1.00 - Sqr(Comegar)) / Abs(Comegar))
end;

Procedure outstring(str:mystring);
{Writes string to line of standard output and results.}
begin
writeln(str);writeln(results,str);
end;{outstring}

Procedure getwritedata;
{Get name of location file from command line, find and read house file,
find and read
weather file, if results file exists read to end else create results
file, write data to results
file}

var
temp,
location
:Real;
locate,
resultsfile,
weatherfile
:mystring;
weather: text;

begin {getwritedata}
{If no house file prompt for it.}
if ParamCount < 1 then
begin
writeln('Usage:');
writeln(' ',ParamStr(0),' ','filename');
writeln('where the data in filename describes a climatic data.');
Halt;
end;
{Check if house file exist.}
assign(house, ParamStr(1));
reset(house);
if IOResult <> 0 then
begin
writeln('House data file, ' , ParamStr(1), ', not found.');
Halt;
end;
if Eof(house) then
begin
{Read then close weather file.}
readln(house,pies);
for i:=1 to 12 do read(house,Hbar[X]);readln(house);
writeln(Hbar[X]:2);
close(house);
end;{getweather}
end;

{Procedure Calculate;
Begin
For X := 1 to 12 do
Begin
Sinrad:=(360*((284+A[X])/365))*rad;
Delta := 23.45*rad*sin(Sinrad);
Deltadeg:=Delta*degree;
{deltar:=delta*rad;
piesr:=pies*rad;
Comegar:=-Tanp(piesr)*Tand(Delta);
Omegar:=ArcCos(Comegar);
Omega:=Omegar*180.0/pi;
Part1:=(day*Gsc)/pi;
Part2:=(1+0.033*cos(360*A[X]*rad/365));
Part3:=((cos(piesr)*cos(Delta)*sin(Omegar))+(Omegar*sin(piesr)*sin(Delta)));

H0:=Part1*Part2*Part3/1e6;
Kt:=Hbar[i]/H0;
end;
end;}

Begin{calculation for Kt}
writeln('This is calculation program to calculate Kt');
for i:=1 to 12 do
begin
getwritedata;
end;
Writeln('Results for Kt at this location');
Writeln('Jan Feb Mac Apr Mei Jun Jul Aug Sept Oct Nov Dis');
{Begin
For X := 1 to 12 do
Begin
Sinrad:=(360*((284+A[X])/365))*rad;
Delta := 23.45*rad*sin(Sinrad);
Deltadeg:=Delta*degree;
deltar:=delta*rad;
piesr:=pies*rad;
Comegar:=-Tanp(piesr)*Tand(Delta);
Omegar:=ArcCos(Comegar);
Omega:=Omegar*180.0/pi;
Part1:=(day*Gsc)/pi;
Part2:=(1+0.033*cos(360*A[X]*rad/365));
Part3:=((cos(piesr)*cos(Delta)*sin(Omegar))+(Omegar*sin(piesr)*sin(Delta)));

H0:=Part1*Part2*Part3/1e6;
for i:=1 to 12 do
begin
Kt:=Hbar[i]/H0;
end;
end;
end;
{Writeln ('Month',' ',X,' = ',Deltadeg:6:2, ' ''Ho = ',H0:6:6,'
','Omega','=',Omega:6:6);}
Writeln ('Month',' ',X,'= ',Kt:6:6);
Readln;
end.
-------------------------------------------------------------------------------------------------------

Above is my code for my program.

and below is the input file for my program :

syd.dat

-33.57{pies}
22.3 21.2 18.5 15.5 12.0 10.0 11.3 13.3 17.1 19.4 22.6 22.9{Hbar[X]}

the way this program works is just at the command line type the name of
the program which is program1 syd.dat(data file)

I hope the output will be the values for each month.

I really approciate if somebody out there can help me with this problem,
because I've been doing this for 2 months and still no good. At least
give me some idea how this input and output procedure works.Please
help...

Thanx
Afrezal

 

Re:Please help with this problem!!ARRAY problem


Quote
> Hi..my name is afrezal. I've got problem with the ARRAY in PASCAL. In my
> program, I've got a constant array of Integer(12 number). Fine. This
> array I use to produce an another 12 numbers after doing some
> calculations. That's ok I've done that. The problem is I've got an input
> file which is also an array of real numbers and the program that I wrote
> need to read this input file so it can produced another 12 outputs.
> Basically this is what I'm doing;

   Lots of apparent work here, but there are numerous (serious) problems
with your code...and you didn't explain exactly what was going wrong or
was occurring.  Since I don't know these things, I can only clean up the
code (to make it compile) and see what it does.
   First, there's an appalling lack of style in your code, making it
extremely difficult to read and understand.  Although the compiler
doesn't care about such things, the lack of consistent (indentation)
style makes it very difficult for humans (you and me) to "see" what's
being done.  I've modified your code to implement some style (via
indentation), and I encourage you do follow it.  I'm sure this was one
reason you couldn't determine what was wrong...
   Second, you had several blocks of code commented out (with {...}), yet
there were instances where you didn't terminate the start { with a }, and
the compiler couldn't work with that.  Worse yet, you embedded comments
within comments, and that's not allowed.
   Third, the data you're reading in isn't integer data, so the program
faults when you attempt to read the decimal values into the integer array
elements.  I changed that array to be Real.
   Fourth, in the "Read" loop, you used "I" to control the loop. but you
referenced the array elements with "X" - I'm sure that's a mistake, and I
corrected that.  Also, you attempt to display (write) the last element of
the array after reading into it, but the loop variable (I or X, depending
on which you use) isn't "available" after the loop terminates: a Pascal
rule states that the value of a For loop variable after the loop
terminates is _undefined_ (so you can't expect it to have any reasonable
value until you assign it one).
   Lastly, the following code compiles and runs, but it doesn't appear to
produce any results.  I don't understand any of the math involved (and
many of the calculations are commented out), so it's now up to you do fix
it.  Please heed my advice about "style", as it's vital that you be able
to understand the program's logic and flow.
Program Constarray;
{$N+}
Uses CRT,Dos,Err_Func;
Const A      : array[1..12] of Integer = (17,47,75,105,135,162,
                                          198,228,258,288,318,344);
      degree = (180.0/pi);                        {change rad into deg}
      rad    = (pi/180.0);                        {change deg into rad}
      Gsc    = 1367;                                   {Solar Constant}
      day    = (24*3600);                               {day in second}
      {pies=-33.57;}

type  mystring = string[127];
      {windata = array [1..12] of Real;}

Var   X        : Integer;
      pies     : Real;
      i        : 1..12;
      Delta    : Real;
      Deltadeg : Real;
      Sinrad   : Real;
      Sindeg   : Real;
      Comegar,
      Omegar,
      Omega,
      thetar,
      theta,
      Part1,
      Part2,
      Part3,
      H0       : Real;
      deltar,
      piesr    : real;
      Hbar     : array[1..12] of Real;{monthly average daily radiation of                                        
                                       a particular day's radiation}
      Kt       : Real;
      Ch       : char;
      house,                          {weather description climatic data}
      results  : Text;    {echo of description then results for each run}

Function Tand (deltar : Real) : Real;
Begin
  Tand:=Sin(deltar)/Cos(deltar)
end;

Function Tanp(piesr : Real) : Real;
Begin
  Tanp:=Sin(piesr)/Cos(piesr)
end;

Function ArcCos (Comegar : DOUBLE) : DOUBLE; (* inverse cosine using TP
arctan *)
Begin                                  (* answer returned in radians *)
  If Comegar = 0.00 THEN ArcCos := Pi / 2.00
  Else
    If Comegar < 0.00 THEN
      ArcCos := Pi-Arctan(Sqrt(1.00-Sqr(Comegar))/Abs(Comegar))
    Else
      ArcCos := Arctan(Sqrt(1.00-Sqr(Comegar))/Abs(Comegar))
end;

Procedure outstring(str:mystring);
{Writes string to line of standard output and results.}
begin
  writeln(str); writeln(results,str)
end;{outstring}

Procedure getwritedata;
{Get name of location file from command line, find and read house file,
find and read weather file. If results file exists, read to end
else create results file, write data to results file}

var temp, location : Real;
    locate, resultsfile,
    weatherfile    : mystring;
    weather        : text;

begin {getwritedata}
{If no house file prompt for it.}
  if ParamCount < 1 then
    begin
      writeln('Usage:'); writeln(' ',ParamStr(0),' ','filename');
      writeln('where the data in filename describes a climatic data.');
      Halt
    end;
{Check if house file exist.}
  assign(house, ParamStr(1)); reset(house);
  if IOResult <> 0 then
    begin
      writeln('House data file, ' , ParamStr(1), ', not found.'); Halt
    end;
  while not EOF (House) do
    begin                        {Read then close weather file.}
      readln(house,pies);
      for I := 1 to 12 do
        read(house,Hbar[I]);
      readln(house);
      writeln(Hbar[12]:2)
    end;  {getweather}
  Close(house)
end;

{Procedure Calculate;
Begin
  For X := 1 to 12 do
    Begin
      Sinrad := (360*((284+A[X])/365))*rad;
      Delta  := 23.45*rad*sin(Sinrad);
      Deltadeg := Delta*degree;
(* deltar:=delta*rad;
piesr:=pies*rad; Comegar:=-Tanp(piesr)*Tand(Delta);
Omegar:=ArcCos(Comegar); Omega:=Omegar*180.0/pi;
Part1:=(day*Gsc)/pi; Part2:=(1+0.033*cos(360*A[X]*rad/365));
Part3:=((cos(piesr)*cos(Delta)*sin(Omegar))+
        (Omegar*sin(piesr)*sin(Delta)));
H0:=Part1*Part2*Part3/1e6;
Kt:=Hbar[i]/H0;
*)
    end;
end;}

Begin                  {calculation for Kt}
  writeln('This is calculation program to calculate Kt');
  for i := 1 to 12 do
    begin
      getwritedata
    end;
  Writeln('Results for Kt at this location');
  Writeln('Jan Feb Mac Apr Mei Jun Jul Aug Sept Oct Nov Dis');
  For X := 1 to 12 do
    Begin
      Sinrad := (360*((284+A[X])/365))*rad;
      Delta := 23.45*rad*sin(Sinrad);
      Deltadeg := Delta*degree; deltar := delta*rad;
      piesr := pies*rad; Comegar := -Tanp(piesr)*Tand(Delta);
      Omegar := ArcCos(Comegar); Omega := Omegar*180.0/pi;
      Part1 := (day*Gsc)/pi; Part2 := (1+0.033*cos(360*A[X]*rad/365));
      Part3 := ((cos(piesr)*cos(Delta)*sin(Omegar))+
                (Omegar*sin(piesr)*sin(Delta)));
      H0 := Part1*Part2*Part3/1e6;
      for i := 1 to 12 do
        begin
          Kt:=Hbar[i]/H0
        end
    end;
  write ('Press any key to continue- ');   { Pause }
  Ch := ReadKey;
  if Ch = #00 then Ch := ReadKey
end.

{Writeln ('Month',' ',X,' = ',Deltadeg:6:2, ' ''Ho = ',H0:6:6,'
','Omega','=',Omega:6:6);}
  Writeln ('Month',' ',X,'= ',Kt:6:6);
Readln;
end.
-------------------------------------------------------------------------
------------------------------

Above is my code for my program.

and below is the input file for my program :

syd.dat

-33.57 {pies}
22.3 21.2 18.5 15.5 12.0 10.0 11.3 13.3 17.1 19.4 22.6 22.9 {Hbar[X]}

Re:Please help with this problem!!ARRAY problem


Quote
z2180553 wrote in message <37F2CCDB.BCCB5...@student.unsw.edu.au>...
>Hi..my name is afrezal. I've got problem with the ARRAY in PASCAL. In my
>program, I've got a constant array of Integer(12 number). Fine. This
>array I use to produce an another 12 numbers after doing some
>calculations. That's ok I've done that. The problem is I've got an input
>file which is also an array of real numbers and the program that I wrote
>need to read this input file so it can produced another 12 outputs.

>Basically this is what I'm doing;
>-----------------------------------snip------------------------------------

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

{Although I only scanned your work, your data is stored in a text file.
Using READ will read the full line and expect some more. Use READLN and
parse the string and use VAL to convert to type real.

However, if you are making your own data entry program instead of a text
editor, you can store the data in the type of file that allows the use of
READ to extract one real number at a time.

Here is a nonsense demo using your same data that should help you use your
text files.  Written in Turbo v6.0   <clifp...@airmail.net>  9/30/99  }

Program FileDemo;
CONST data = 'syd.dat' ;
A:array[1..12] of Integer = (17,47,75,105,135,162,198,228,258,288,318,344);

TYPE
d12 = Array[1..12] of real;

VAR
house:text;  {put your data in SYD.DAT, which is a text file}
sum, adata:d12;
pies:real;
ct, errFlag:Integer;
tmp:String;

Procedure ParseData(t:String; VAR d:d12);
VAR errFlg, j, k:Integer;
    short:String[10];
    b:Real;
Begin
     t := t + ' ';  {assures at least 1 following space to ease parsing}
     Writeln('String to parse:');
     Writeln('"', t, '"');
     j := 0;
     Repeat
      {The following line takes care of surplus leading AND trailing spaces}
           While (t[1] = ' ') AND (Length(t) > 0) Do Delete(t, 1, 1);
           If Length(t) = 0 then EXIT; {procedure}

           k := Pos(' ', t);             {find 1st space}
           short := Copy(t, 1, k - 1);   {copy word}
           VAL(short, b, errFlg);        {evaluate word}
           If errFlg <> 0 then EXIT ;    {procedure}
           Inc(j);
           d[j] := b;          {store real number in array}
           Delete(t, 1, k);    {chop off 1st word and trailing space}
     Until Length(t) = 0;
End;

Begin
     Assign( house, data);
     Reset(house);
     Readln(house, tmp);
     VAL(tmp, pies, errFlag);
     If errFlag <> 0 then exit;

     Readln(house, tmp);   {<<<<< note, this gets the whole line and is
ready to stop }
     Close(house);
     ParseData(tmp, adata);

     Writeln; Write('pies = ', pies:0:4);
     Writeln('   Parsed REAL data below:'); Writeln;

     For ct := 1 to 12 do Write(adata[ct]:1:2, ' ');  Writeln;
     Writeln;
     For ct := 1 to 12 do
     Begin
          sum[ct] := A[ct] + adata[ct];
          if ct < 10 then write(' ');
          Writeln('A[', ct, '] = ', A[ct]:3, '     sum = ', sum[ct]:6:2);
     End;
     Writeln; Writeln('<<< Press Enter >>>':40); Readln;
End.

Other Threads