I wanted your help in writing a program to convert fortran array (n, m) in a table (p, 3).
I tried with this program:
program Temp
implicit none
real,dimension (23250,27)::table
real::time
integer::i
integer::j
integer::wl
integer::al
i=1,23250
read(*,*) time(i),wl(i),(table(i,j),j=1,27)
j=1,27
alt(j)=j
write(*,*) time(i),alt(j),table(i,j)
continue
continue
endprogram Temp
but error messages are displayed as:
D:\Travaux de thèse\modeling\essay\essay.f90(9) : Error: Syntax error, found ',' when expecting one of: <END-OF-STATEMENT> ;
i=1,23250
-----^
D:\Travaux de thèse\modeling\essay\essay.f90(11) : Error: Syntax error, found ',' when expecting one of: <END-OF-STATEMENT> ;
j=1,27
----^
D:\Travaux de thèse\modeling\essay\essay.f90(10) : Error: Constants and expressions are invalid in read-only I/O lists. [TIME]
read(*,*) time(i),wl(i),(table(i,j),j=1,27)
----------^
D:\Travaux de thèse\modeling\essay\essay.f90(10) : Error: Constants and expressions are invalid in read-only I/O lists. [WL]
read(*,*) time(i),wl(i),(table(i,j),j=1,27)
------------------^
D:\Travaux de thèse\modeling\essay\essay.f90(12) : Error: This name has not been declared as an array. [ALT]
alt(j)=j
^
Error executing df.exe.
essay.exe - 5 error(s), 0 warning(s)
Can anyone can help me?
T
hanks you in advance.
from your example code alone it is a bit difficult to figure out where this code is going. Where is your array 'array' you mention in the question defined? If you want to loop over something, you need to use the 'do' statement[1]. Furthermore I would try to access the dimension of that array programmatically, so you don't have to hard code it. The following code snippet is not complete, but maybe it gets you going.
program Temp
implicit none
real,dimension (23250,27)::table
integer, dimension(2) :: table_shape
real::time
integer::i
integer::j
integer::wl
integer::al
table_shape = shape(table)
do i=1, table_shape(1)
read(*,*) time(i),wl(i),(table(i,j),j=1,27)
do j=1, table_shape(2)
alt(j)=j
write(*,*) time(i),al(j),table(i,j)
!continue
!continue
enddo
enddo
endprogram Temp
Best,
Max
[1] http://en.wikibooks.org/wiki/Fortran/Fortran_control
Related
A program that loads and processes command-line arguments should be created.
Here comes a few examples on how it should look when you run it (bold text is the text that the user will type):
Terminal prompt % **./my_program**
No arguments given.
Terminal prompt % **./my_program 123**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 10 XYZ 999 Greetings!**
Wrong amounts of arguments given.
Terminal prompt % **./my_program 3 HELLO**
Message: HELLOHELLOHELLO
The program "./my program" is ending.
Terminal prompt % **./my_program 0 Bye**
Message:
The program "./my program" is ending.
This is my code so far:
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
type String is array (Positive) of Character;
N : Integer;
Text : String;
begin
N := Argument_Count;
if N = 0 then
Put_Line("No arguments given.");
elsif N /= 2 then
Put_Line("Wrong number of arguments given.");
elsif N = 2 then
Put("Message: ");
for I in 1 .. N loop
Put(Text);
New_Line;
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
end my_program;
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says
Missing argument for parameter "Item" in call to "Put"
I don't know if I declared my string right because I don't want a string of a specific length. Can anyone come up with something that could help me solve case 4 and 5? It would be nice and highly appreciated
This seems to be a homework or exam question, so I would usually not provide a full answer. But Chris already gave that (with some defects), so here is my suggestion. Compared to Chris's solution, I try to avoid using unnecessary variables, and I favour case statements over if-then-else cascades, and I try to reduce the scope of exception handlers. I prefer to put use clauses in the subprogram so that the context-clause section contains only with clauses. I use the string-multiplying "*" operator from Ada.Strings.Fixed, but that is perhaps an unnecessary refinement.
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Text_IO;
procedure My_Program
is
use Ada.Strings.Fixed;
use Ada.Text_IO;
begin
case Ada.Command_Line.Argument_Count is
when 0 =>
Put_Line ("No arguments given.");
when 2 =>
begin
Put_Line (
Natural'Value (Ada.Command_Line.Argument(1))
* Ada.Command_Line.Argument(2));
exception
when Constraint_Error =>
Put_Line ("Invalid input for argument 1.");
end;
when others =>
Put_Line ("Wrong amount of arguments given.");
end case;
Put_Line (
"The program """
& Ada.Command_Line.Command_Name
& """ is ending.");
end My_Program;
Note that my version:
Rejects negative first arguments (like "-3").
Outputs the repeated strings on a single line, as was required by the examples given.
Includes the name of the program in the final message, as was also required.
Given the clarification in comments as to the purpose of the program, to print a message n times where n is the first argument, and the message is the second argument, you need to parse the first argument as an integer. This can be done with Integer'Value.
Now, that raises the prospect of the user not running the program with an integer. So we have to handle the possible Constraint_Error exception.
with Ada.Text_IO; use Ada.text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure my_program is
argc : Integer;
N : Integer;
begin
argc := Argument_Count;
if argc = 0 then
Put_Line("No arguments given.");
elsif argc /= 2 then
Put_Line("Wrong number of arguments given.");
else
n := Integer'Value(Argument(1));
Put("Message: ");
for I in 1 .. N loop
Put_Line(Argument(2));
end loop;
Put("The program """);
Put(""" is ending. ");
end if;
exception
when Constraint_Error =>
Put_Line("Invalid input for argument 1.");
end my_program;
As an aside, when we've checked in our conditional if argc is zero, and that it doesn't equal two, we don't have to use elsif. The only other possibility is that it is 2.
You say
My program handles the first 3 three cases but when I go ahead with the 4th and 5th (last) case I get an error code at the row Put(Text) where it says "Missing argument for parameter "Item" in call to "Put". "
which doesn't make sense, because your program as shown doesn't compile. I guess what you mean is "when I try to add the code to handle cases 4 and 5, it doesn't compile".
The reason why it doesn’t compile is hidden in the actual error messages:
leun.adb:24:10: no candidate interpretations match the actuals:
leun.adb:24:10: missing argument for parameter "Item" in call to "put" declared at a-tiinio.ads:97, instance at a-inteio.ads:18
...
leun.adb:24:14: expected type "Standard.Integer"
leun.adb:24:14: found type "String" defined at line 7
leun.adb:24:14: ==> in call to "Put" at a-tiinio.ads:80, instance at a-inteio.
You have at line 7
type String is array (Positive) of Character;
which is both misleading and not what you meant.
It’s ’not what you meant’ because array (Positive) means an array of fixed length from 1 to Positive’Last, which will not fit into your computer’s memory. What you meant is array (Positive range <>).
Even with this correction, it's 'misleading' because although it would be textually the same as the declaration of the standard String in ARM 3.6.3(4), in Ada two different type declarations declare two different types. So, when you write Put(Text); the Put that you meant to call (the second in ARM A.10.7(16)) doesn’t match because it’s expecting a parameter of type Standard.String but Text is of type my_program.String.
Cure for this problem: don’t declare your own String type.
I am trying to compile the following SystemVerilog, and I get the following error "Error (10686): SystemVerilog error at InstRom.sv(8): InstAddress has an aggregate value."
module InstRom#(parameter A = 16, parameter W = 9)
(
input InstAddress[A-1:0],
output logic InstOut[W-1:0]
);
logic[W-1:0] instr_rom[2**(A)];
always_comb InstOut = instr_rom[InstAddress];
initial begin
$readmemb("cordic.txt", instr_rom);
end
endmodule
What am I doing wrong?
InstAddress is an unpacked array of bit, not a bit vector (i.e. a packed array). The same applies to InstOut. Change the declaration to
input logic [A-1:0] InstAddress,
output logic [W-1:0] InstOut
and your code should work.
While updating Modelsim from 10.3c to 10.6a, I encountered an error on this piece of code that used to not work without warning:
module test(
input bit clk,
input bit signed[31:0] data
);
real rdata_dl[19:0] = '{20{0}};
real rdata = 0;
always #(posedge clk) begin
rdata_dl = {rdata_dl[18:0], rdata};
rdata = data;
end
endmodule
-- Compiling module test
** Note: test.sv(10): (vlog-13177) Promoting concatenation '{rdata_dl[18:0],rdata}' to an assignment pattern: Assigning to a real/wreal array.
** Error (suppressible): (vlog-13215) test.sv(10): Assignment pattern element 'rdata_dl[18:0]': Cannot assign an unpacked type 'real $[18:0]' to a packed type 'real'.
** Error (suppressible): test.sv(10): (vlog-13174) Illegal assignment pattern. The number of elements (2) doesn't match with the type's width (20).
I managed to fix it by using this line instead: rdata_dl = {rdata_dl[18:0], real'(rdata)};.
However, I fail to understand why it failed and why the new version would work. Can anyone explain?
not sure what you are trying to do with this code. real type is 64-bit. When concatenating it with 19-bits, you're getting a 83-bit bus, where the rdata is in the LSBs.
Now, when assigning this 83-bit vector to a 20-bit bus, it will take the 20 LSBs, meaning that it is equivalent to writing the following assignment:
always #(posedge clk) begin
rdata_dl = rdata[19:0];
rdata = data;
end
I am using openmodelica and I am trying to loop through an array in order to find the maximum value. I was able to reduce my code to a very simple test case that still gives the error. Is this something that I am doing wrong, or is this a bug in openmodelica? Here is a very simple case that does give the error:
package TestLoop
model ItemA
Real p;
end ItemA;
model ItemB
ItemA a[n];
parameter Integer n = 5;
Real p;
equation
for i in 1:n loop
a[i].p = time;
end for;
algorithm
for i in 1:n loop
p := a[i].p;
end for;
end ItemB;
end TestLoop;
The problem is in my algorithm section. Here is the error that I am getting:
TestLoop.ItemB.c:155:13: warning: implicit declaration of function '$Pa$lB' is invalid in C99 [-Wimplicit-function-declaration]
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
TestLoop.ItemB.c:155:20: error: unexpected type name 'modelica_integer': expected expression
$Pp = $Pa$lB(modelica_integer)$Pi$rB$Pp;
^
1 warning and 1 error generated.
Any suggestions for why this might be, or how I can work around it? If I replace the assignment with a fixed value, p:=a[1].p;, the code does run (although that is not useful to me). What I ultimately want to do in the algorithm section is find the largest value of a[n].p, where I do have an equation section that does useful calculations into the array of items.
Yes, the code generation is an error of OpenModelica (it does not like unknown array indexes). Your problem is very easy to solve in a single line though (one of the following):
p = max(r for r in a.p);
p = max(a.p);
I wrote this code inside the M Blcok (one of Xilinx blockstes in Simulink):
function z= discorr(x,y)
t=zeros(12288,1);
i=zeros(12288,1);
k=zeros(12288,1);
i(4096:8191,1)=x(1:4096,2); %output of the image filter
t(4096:8191,1)=y(1:4096,2); %output of the tamplate filter
i=i';
z=A(1:4096,1);
for n=1:8191
k=zeros(12288,1);
k(n:n+4095,1)=t(4096:8191,1);
z(n,2)=i*k;
end
end
Its telling me:
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error("discreatcorr.m"): Syntax error: Lexical error at line 15, column 0. Encountered: after : "\';\r\nz=A(1:4096,1);\r\nfor n=1:8191\r\n k=zeros(12288,1);\r\n k(n:n+4095,1)=t(4096:8191,1);\r\n z(n,2)=i*k;\r\n end\r\nend\r\n"
Error occurred during "Block Configuration".
althogh there is nothing in line 15 in the code
it is giving an error at the end of the code
Any Ideas??
The problem is that your system misinterprets the ' symbol as string symbol. Replacing the line i=i'; by i=transp(i); should solve the problem.