I am getting the "In line 1, expression expected" error and I am not sure why.
I am using the CPU Emulator from nand2tetris. I tried changing line 1 to 5, but it did not solve the problem. I just do not get what is the problem in the first place.
#j
D=5;
#i;
M=1;
#5
#i
D=M
D=D-A;
#END
D;JGT
#j
#1
M=M-A
#i
#1
M=M+A
#LOOP
0;JMP
What I am trying to recreate is this loop:
J=5
for(i=1; i<5; i++) {
j--
}
There are several issues that pop out at first glance.
First, D=5 is not a valid Hack operation. If you want to load 5 into D, you have to first load it into A and then move to D:
#5
D=A
Second, ; is the jump delimiter, and should be followed by a jump condition (such as JEQ, or JMP for an unconditional jump). You have several lines (including line 1) where you have a ; but no jump condition. These should be removed.
Finally, you should probably review the book pages on the Hack assembly language syntax to make sure you are clear on how it works. In particular, in the above code, you haven't specified your jump targets like END and LOOP. This is done with the (LABEL) construct.
Related
Is there any difference between the terms 'line of code' and 'statement' in programming languages?
Yes, check following:
int a =0;
while(a<100)
{
cout<<
"Is it ok"<<
a <<
"is the current value of 'a'"<<endl;
a++;
}
Above code snippet is having:
Line of code: 9
Simple Statements: 3
Compound Statements: 1
Line of code is basically how many end points you are using. A Statement is the group of code that you produce to create an expected output.
For example in a conditional statement using if else, you can right that in multiple line of core or using only one line via Ternary method.
Sample:
if($var == 0) {
echo "this is zero";
} else {
echo "not a zero";
}
that basically created 5 line of code. In ternary you can go like this
$var == 0 ? echo "this is zero" : echo "not a zero";
As you see the result is the same only it is created in 1 line of code.
Hope this helps you moving forward
Lines of code presumably refers to lines of content in a source file, i.e. the number of lines in a file. On the other hand, a given statement of code can, and often does, exceed a single line. For example, consider the following Java statement from the Javadoc for streams:
int sum = widgets.stream()
.filter(b -> b.getColor() == RED)
.mapToInt(b -> b.getWeight())
.sum();
One statement spans four physical lines in the editor. However, we could inline the whole thing into a single line.
Well, I got this Newton's Method for multiple variables code that was modified by me to function with 4 variables instead of 2, but I keep getting the error
Syntax error, unexpected ;
referencing line 9 which is the Jacobian matrix from the partial derivatives of the equations on function F. Every " " is another column and every ";" is another line.
clear;clc
function z=F(p1,p2,p3,p4)
z(1)=0.3*(500-p1)^0.5-0.2*(p1-p2)^0.5+0.2*(p1-p3)^0.5
z(2)=0.2*(p1-p2)^0.5-0.1*(p2-p4)^0.5+0.2*(p2-p3)^0.5
z(3)=0.1*(p1-p3)^0.5-0.2*(p3-p2)^0.5+0.1*(p3-p4)^0.5
z(4)=0.1*(p2-p4)^0.5+0.1*(p3-p4)^0.5-0.2*(p4)^0.5
endfunction
function z=J(p1,p2,p3,p4) //Jacobiano
z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p3)) 0;0.2/(2*sqrt(p1-p2)) -0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3) -0.2/(2*sqrt(p2-p3)) -0.1/(2*sqrt(p2-p4));0.1/(2*sqrt(p1-p3)) 0.2/(2*sqrt(p3-p2)) -0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)) -0.1/(2*sqrt(p3-p4));0 0.1/(2*sqrt(p2-p4)) 0.1/(2*sqrt(p3-p4)) -0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction
The code doesn't end here, but the error is certainly in the format I used in the 4x4 Jacobian matrix or in the function itself, I just don't get why is wrong.
Appreciate any help.
You forgot to close a parenthesis in column 191 of line 9, and you also forgot an operator on column 319.
If you're creating a matrix in which the elements are results of operations, you'd better use commas instead of blank spaces to separate them. You should also be using ... to break the matrix definition in more than one line to improve readability. Your Jacobian function would be a lot easier to debug if it were written like this:
function z=J(p1,p2,p3,p4) //Jacobiano
z=[-0.3/(2*sqrt(500-p1))-0.2/(2*sqrt(p1-p2))+0.2/(2*sqrt(p1-p3)),...
0.2/(2*sqrt(p1-p2)),...
-0.2/(2*sqrt(p1-p3)),...
0;...
...
0.2/(2*sqrt(p1-p2)),...
-0.2/(2*sqrt(p1-p2))-0.1/(2*sqrt(p2-p4))+0.2/(2*sqrt(p2-p3),...
-0.2/(2*sqrt(p2-p3)),...
-0.1/(2*sqrt(p2-p4));...
...
0.1/(2*sqrt(p1-p3)),...
0.2/(2*sqrt(p3-p2)),...
-0.1/(2*sqrt(p1-p3))-0.2/(2*sqrt(p3-p2))+0.1(2*sqrt(p3-p4)),...
-0.1/(2*sqrt(p3-p4));...
...
0,...
0.1/(2*sqrt(p2-p4)),...
0.1/(2*sqrt(p3-p4)),...
-0.1/(2*sqrt(p2-p4))-0.1/(2*sqrt(p3-p4))-0.2/(2*sqrt(p4))]
endfunction
In this style, your mistakes can be easily spotted: notice the missing ) in line 8, and the missing / in line 14.
I want to take the user's input of a positive integer where 1 < a < 10^6 and run a loop on it and then store it in a matrix which gets printed to the screen. However, my code is throwing a syntax error pointing to the letter "e" in while. Does anyone know why this error is appearing?
A = (while (a!=1)
If(rem(a,2)=0
floor(a^(1/2));
Else
floor(a^(3/2));
endwhile)
disp(A);
You're having several different problems in your code:
a while loop doesn't return anything, so you can't assign it to A
syntax is case sensitive, so it's if and else, not If and Else
you're missing a closing brace after the if clause
you're missing an endif
you're assigning to rem, use == to compare for equality
Ruby noob here, and I am having an infuriating issue.
Something in my script is making Ruby skip a section of it.
#Main_block_begins_here
if __FILE__ == $0
#Open_the_file_with_values_and_weights
File.open(getFileName, 'r') do |f|
#Intial_data...
$totalNumber=f.gets.chomp!.to_i
$totalCapacity=f.gets.chomp!.to_i
$currentItemWeight=0
$currentItemValue=0
$spaceLeft=0
$spaceLeft=$totalCapacity
$takenWeight=0
$takenValue=0
#Make_a_heap_for_the_data
$prioQ = Heap.new do
#reads_values,_computes_them,_and_enqueues_them
until i==$totalNumber
total=0
value=f.gets.chomp!.to_i
weight=f.gets.chomp!.to_i
total=value/weight
puts "#{total}=t, #{value}=v, #{weight}=w. Correct?"
check=gets.chomp!
it=Obj.new(total, value, weight) do
prioQ.enqueue(it)
end
i+=1
end
end
#dequeueIt
puts "total weight taken was #{$takenWeight} and total value taken was #{$takenValue}."
end
end
The commented line #dequeueIt is a method earlier on that when I let run, it gives me an infinite loop with all values that were supposed to be read in from the text file as zeros.
The puts line and the check declaration line inside the until loop are for debugging purposes, and of course they never print out.
Commented out, when I run the program it just prints out the last line as if the until loop never ran. If more code for context is necessary, just let me know and I'll put it up.
My hair's starting to fall out over this one, so any help is appreciated!
EDIT:: Yes, it should have been i==$totalnumber. I fixed it in my code and it still doesn't execute the script inside that loop.
This is one oversight:
until i=$totalNumber
This assigns a number to i; it is always true. Try
until i==$totalNumber
Given a string in the following format:
"(1 AND (2 OR 3) AND 4)"
What is the fastest way to determine if the number of "open" brackets "(" equal the "close" brackets ")".
NOTE: The string could be several hundred characters longs.
Just use a simple counter that starts with 0.
When you encounter "(", increase it by one. When you encounter ")", decrease by one.
If the counter isn't 0 at the end, you've got a mismatch.
Also, as others have mentioned, if the counter ever becomes negative, this means a situation such as )( has occured. Signal an error and stop further parsing.
Initialise counter to zero.
Iterate through the characters of the string.
a. On an opening parenthesis, increase the counter.
b. On a closing parenthesis, decrease the counter.
c. Error out if the counter is negative.
Error out if the counter is not equal to zero after the loop.
This also catches cases like )(, which do have a matching number of opening and closing parens, but should probably be considered erroneous anyway.
If you're trying to count that the number of ( match the number of ), just run through the string once maintaining a counters, incrementing if you see a ( and decrementing if you see a ). This is O(n) and you can't do better; you have to inspect every character.
However, I suspect you meant to ask a different question. Namely, how do you tell if the ( balance with the ). In this case, you maintain a stack and you push whenever you see a ( and you pop when you see a ). If ever you try to pop when the stack is empty, the parentheses are not balanced. If the stack is not empty when you reach the end of the input string, the parentheses are not balanced.
Of course, you can just mimic this with a counter, but it's more natural to think about from the perspective of a stack.
It's O(n). There is no way around it. Here is a rough idea.
For i=0 to string.length
if string[i] == ')'
add to rightBracketCount
else if string[i] == '('
add to leftBracketCount
end for
compare rightBracketCount to leftBracketCount
:)
You can
int left = "your string".split("(").length()
int right = "your string".split(")").length()
boolean ok = (left == right)
Of course this is stupid, but it is just another way