So how do I do the do while NOT thing
while(!thing)
Perhaps a switch -- case statement will do the trick here.
You can't switch on strings. And even if you could that would not help with looping.
So how do I do the do while NOT thing
while(!thing)
Perhaps a switch -- case statement will do the trick here.
You can't switch on strings. And even if you could that would not help with looping.
sizeof(states)
gives you the number of bytes in the array. The number of bytes in an array equals the number of elements times the number of bytes per element. The elements in states
are char pointers and apparently pointers have 4 bytes on your platform (so you're running a 32-bit system or at least a 32-bit compiler), so we get 4 * 4 = 16
bytes total.
I don't know why it gave me that portion of code if it's unusable
fn
is unused, not unusable. You could replace line 19 with fn(rNum, ptr)
and thus actually use fn
.
That's not really true. The compiler will know whether you're using java.awt.Color
regardless of whether or not you use an import. All the import does is to allow you to write Color
instead of spelling out java.awt.Color
. It's just a convenience feature.
What's the relation between a class that has a main method and the String class? The relation is that there's a method that takes an instance of that class as an argument - that's all.
Are you asking why a method defined in Component can take a Color as its argument? Why wouldn't it be able to? It is perfectly common for methods to take objects of different in classes as arguments.
Even the standard main
method takes an array of String
s as its argument even though it is not defined in the String
class.
JApplet extends Component.
The current object is the object that the method is being called on, that is the object referred to by this
.
we are not creating any object of any class
The Java plug-in will create an instance of your applet class and then call the appropriate methods on that instance.
int& length = 0;
does not work because 0 is not an l-value. As I said an l-value is something like a variable or an array access - something that can be assigned to. You can't assign to 0.
Think of it this way: defining length
as a reference means "whenever I change length
, I want the target of the reference to also change". That is when you write int& length = x;
(where x is an int variable) and then length = 42;
, the value of x
will now also be 42. If you write int& length = 0;
and then length = 42;
, that would mean that now the value of 0 is now also 42. Of course that doesn't make any sense - the value of 0 can't be 42; the value of 0 is 0. So you're not allowed to write int& length = 0;
.
I don't use the & in the declaration only when passing it?
You use &
if you want length
to be a reference to something, that is if you want something else to change whenever length
changes. If that's not what you want, you don't use &
.
my question is that when we write applet then we write only methods defination not method calling so i am confuse about this that who call the methods of applets.
Think of it this way: When you write a normal Java application you write a main method, but you never write any code that calls the main method. So who calls the main method? The Java VM does when your application is run.
Likewise when your applet is loaded in a browser, the browser's Java plug-in will load your applet class and call methods on it. It just doesn't call the main method, but rather calls the apropriate listeners whenever an event occurs.
these methods call without anything so how should be calling methods with this syntax.
They're called on the current object. It's exactly the same as writing this.setForeground(Color.cyan);
. When calling a method on the current object, the this.
is simply optional in Java (this is not specific to applets).
As the message says int& length
is a reference (that's what the &
means). You can not define a reference without initializing it. That is you need to do int& length = foo;
where foo
is an l-value (like a variable, an array access, a member access or a function call that returns a reference).
Just writing int& length;
is not allowed because then the compiler does not know what length
is a reference to.
It still has a value
That's not necessarily true:
An uninitialized local variable may contain a trap representation for its type (and thus not a valid value), in which case reading it will invoke undefined behavior (possibly a crash).
Furthermore reading an uninitialized local variable whose address is never taken invokes undefined behavior regardless of whether or not the given type has a trap representation on your implementation.
Your first piece of code is illegal.
In C89 it's illegal because array sizes need to be compile-time constants, so variables are not allowed. A conforming compile should give an error message or at the very least a warning when compiling in C89 mode.
In C99 it's allowed to use variables as array sizes, but it's still illegal because size
is uninitialized and thus using it invokes undefined behavior.
As a(n irrelevant) side note: it is defined, just not initialized. To declare a variable without defining it, you'd use the extern
keyword.
---
Your second piece of code defines a one-dimensional array of size 5 because 2+3
is 5. A multi-dimensional array would be defined by using multiple pairs of brackets (like int siy[2][3]
).
Yes, because secs
is the third argument and hrs
is the first. You can use either MyTime(0, 0, totalsec)
or MyTime(secs = totalsec)
.
If you do print(t1)
, you'll use the __str__
method that you defined. The reason that it's not being called when you do print(t1.increment(300))
is that increment
returns an integer, not a MyTime
, so print
prints it using the normal rules for printing integers. To fix this you should probably make increment
return a MyTime
rather than an integer.
Wait, do you want to create your own command line program that you start from the command line and into which you can then enter the command "display bla.txt" (which is what I though you meant) or do you want to create a display command so that you can enter "display bla.txt" into cmd?
As the error message says, tuples don't have a between
method, so you can't do t.between(...)
where t
is a tuple.
You can call between
on MyTime
objects because you defined a between
method for the MyTime
class. So if you create an object of that class (by using MyTime(...)
), you can call between
on that object.
Assuming that the intended syntax is "'display', followed by one or more spaces, followed by the name of the file", you read a line of input, check whether the line starts with "display" followed by one or more spaces. If it doesn't, you display a "no such command" error. If it does, you iterate up to the first non-space character after "display", which is where the file name starts. Then you open the file with that name, read each line of the file until you hit the end of the file and print each line as you read it.
Then you put the whole thing into a loop, so it asks for commands until the window is closed or the user hits Ctrl-z or Ctrl-c.
pipelining and branch prediction at the CPU level maybe???
Branch prediction seems like a pretty good candidate. Note that the if condition data[c] >= 128
will randomly switch between being true and false when the array is unsorted, but when it is sorted it will be false consistently until the first element > 128 appears and after that it will be true all the time. So it will act much more predictably for a sorted array.
when you put i++ in to a for loop it will add the value of "i" after the compiler has checked the middle comparison statement but will add 1 to "i" before the compiler proceeds to the bracket
That's not true. The steps happen in the following order:
So no matter whether you write i++
or ++i
or i=f(i)
or whatever, the increment always happens before the condition is checked (except the first time when the increment doesn't happen at all).
For example if you were to print i++ and ++i one would print the value of i then add and the other would add then print the value. Makes a difference.
The difference between i++
and ++i
is not when the increment happens. It always happens exactly when the expression is evaluated. The difference is the resulting value. The expression i++
results in the old value of i
while the expression ++i
results in the new value.
So if i
starts as 42 and you do f(i++)
, then f
will be called with the argument 42, but the value of i
will already be 43 (you can test this by making i
an instance variable and letting f
print both its argument and i
).
in upper code i++and ++i are also as a statement
No, they're not. System.out.println(i++)
is a statement and i++
is an expression inside that statement. It's not used as a statement on its own here. Most importantly System.out.println
uses the value of i++
, your loop does not.
To be completely accurate the i++
in the loop is an expression as well (if it were a statement, it would have a semicolon at the end), but it's an expression that is only executed for its side effects and whose value is not used, so it's basically used as a statement.
If there's no file system entry for a file, it does not consume space. Zeroing the bytes of a file will not make it consume less space, but it will make it harder to recover the file after you delete it. However writing the empty string to a file before deleting it will not zero the bytes. It will do basically nothing (it's pretty much the same as deleting the original file, creating a new empty one and then deleting that as well).
If all you want is to free disk space, just delete the file with unlink/remove.
how can I delete the file if it just becomes unlinked?
When you say "delete", what exactly do you mean? Scrambling the file? Because when most people say "delete", they mean "removing the file system entry", which is exactly what remove/unlink does.
Also I haven't tried to yet, but does './file.txt' work as is or do I must provide the full path?
You can use relative as well as absolute file names. So all of 'file.txt'
, './file.txt'
, '../file.txt'
and '/path/to/file.txt'
will work.
Because you're using i++
/++i
as a statement on its own without doing anything with its value. The side-effects of ++i
and i++
are the same, only the value differs. So if you don't use the value, there is no observable difference.
Can you explain a bit what you code does any why you think it should do more than discarding one character after it sees a {
?
You need to shove it onto the stack so you can push and pop the curly brackets.
DFAs don't have stacks, so I assume the comments are supposed to be non-nested (which is pretty standard in programming languages).
When i
is 0, we get arr[num[0]] += 1
. num[0]
is 1, so we get arr[1] += 1
. By the same logic we get arr[4] += 1
when i is 1 and so on.
So no, it doesn't increase each element of arr
by 1. Rather it increases each element in arr once for each time its index appears in num
. So for example arr[1]
is incremented twice because 1 appears twice in num
and arr[0]
is never increment because there is 0 in num
.
In C++ pre-11 you'll need to cast the first argument to a floating point type in order to tell the compiler which overload to use (there are no overloads taking int as the first argument before C++11). Or in this case you could just replace 10 with 10.0 since it's a constant.
In C++11 your code should work as fine.
it gives an error message at line 12
What's the error message?
You do not use parsing algorithms to generate a list of tokens. The list of tokens is the input to the parsing algorithm. The output of the algorithm is generally an AST or some kind of intermediate representation of the code (or in some cases even the end product of the compilation or interpretation).
To generate the list of tokens you write a so-called lexer, which usesa totally different algorithm. This algorithm is usually based on regular expressions, not CFGs, and works by implementing a state machine that keeps track of which of the possible regular expressions can still match the input read so far.
A lexer can either generated by tools like lex (or its many clones like flex) or written by hand. Since the tokens for most languages follow quite simple rules, hand-writing lexers can often be relatively simple. For example a state machine for many common languages could follow rules like this:
Start-state: If the current character is a letter or underscore, switch to ID-state. If it's a digit, switch to Number-state. If it's a quote, switch to String-state. If it's an operator-start character, switch to operator state. If it's white space, discard it.
ID-state: If the current character is in [0-9a-zA-Z_], add it to the token. Otherwise: token is finished; if the characters read form a keyword, it's a keyword token; otherwise it's an identifier token. Return to Start-state.
Number-state: If the current character is a valid character for a number (possibly taken into account suffix …
First of all sp::bool
does not exist, you probably meant sp::b
there.
But even then it won't work because ADT is about where the type of the argument is defined, not where the variable is defined (the argument doesn't even need to be a variable). The type of the argument here is bool
and since bool
is not defined in the sp
namespace, that namespace won't be part of the lookup.
If you define a type in sp
(maybe as a typedef) and then make b
an instance of that type, the lookup will behave as you expect. It will even still behave that way if you define b
outside of the namespace (as long as the type is defined within the namespace).
A node needs to point to another node, not to a DATA.
It does. node<DATA>*
is a pointer to a node.
Why not just use the following (apparently satisfactory) structure
I haven't read the book, but presumably the solution presented hides the implementation of the nodes, whereas your struct exposes everything.
Pointers are iterators (random access iterators to be specific) and they are in fact commonly used as such. For example std::vector<T>
uses T*
in most (possibly all) implementations (except in the case of vector<bool>
, which is a special case).
On the other hand there are many iterators that aren't pointers. For example std::list<T>::iterator
could not possibly be a pointer type.
Yes, cup1
is an object (or rather it's a variable referring to an object - just like cup2
).
The biggest difference between cup1
and cup2
is that, if the Coffee
class defines public methods in addition to those declared in CoffeeInterface
, you will be able to call those on cup2
, but not on cup1
.
Another difference is that, if you have a method m
that has one overload for Coffee
and one for CoffeeInterface
, then m(cup1)
will call the overload for the interface while m(cup2)
will call the overload for the class.
Can you clarify what exactly you don't understand? The arrow means assignment, for i <- x to y
means a for loop that iterates i
from x
to y
and everything else seems to be the same as in C (i.e. /
is division, []
is array access etc.).
One thing I see wrong with your signature is that the pseudo code said that the "output" (i.e. the return value) should be an array, but you declared the function as returning an int.
You're counting all multiples of 15 twice (once as multiples of 3 and once as multiples of 5).
The problem is that it is taking away too much, double the cost
System.out.println("Calc: " + this.curGold + " - " + goldValue + " = " + (this.curGold -= goldValue));
this.curGold -= goldValue;
In the above code you execute this.curGold -= goldValue
twice (once in the print statement and once in the line afterwards), so the gold value will indeed be subtracted twice.
and also will not buy if the current gold is equal to the cost.
public boolean hasEnoughGold(float cost)
{
if(getCurGold() > cost)
return true;
return false;
}
Right because getCurGold() > cost
is not true if getCurGold()
is equal to cost
.
PS: if(foo) return true; else return false;
can just be written as return foo;
- no need for the if
.
Let's say you have 25c and can use the 10c and 5c coins. Since 15 is greater than 10, you may use a 10c coin. If you do, you have 15c left (for which you again may or may not use another 10c coin). If you don't, you still have 25c left, which you pay with 5c coins.
So the total number of ways to give change for 25c using 10c and 5c coins is the number of ways you can give change for 25c using only 5c coins (n[0][25]
) plus the number of ways you can give change for 15c using 10c and 5c coins (n[1][15]
).
When declaring a variable, preceding the variable name with a *
makes it a pointer. Preceding it with a &
is illegal (in C - in C++ it makes the variable a reference).
In an expression &
takes a variable and produces a pointer to that variable, whereas *
takes a pointer and produces the value that that pointer points to.
The third error is telling you that the first two branches of your case return a list (nil or y/z respectively) while the third branch returns a Cons.
And the fourth error tells you that z is a list, but you treat it as a Cons.
The reason that it thinks that z is a list is because you do x=z
, meaning x
and z
need to have the same type and then y else z
, meaning y
and z
also need to have the same type.
To fix this, you should first decide what each of the arguments' types should actually be and what you want the function to return. Once you decided that, you can go through the code to find the places where each value is used with a type other than the one it should have and fix those.
Cons is just the name of your type - you do not have a constructor named Cons. So writing Cons(something)
doesn't work. You probably meant List(x1, x2)
.
The error is because you call sub2
first with hd z
and then with tl z
as the third argument. hd z
and tl z
have different types: If z
is a list of foo
s, then hd z
is a foo
and tl z
is a foo list
. For foo
and foo list
to be the same type, foo
has to be equal to foo list
, which is impossible. That's the circularity the error message is complaining about.
Another error (not mentioned by the error message) is that the types of your patterns don't match: Your first pattern is nil
, implying that z
is a list (which it certainly is since you use hd
and tl
on it later). However your other two patterns are of some other type.
'^' is a special character ONLY when used as the first character within square brackets
^
means "beginning of the line" outside of square brackets, so it does need to be escaped.
You're right about _
though.
<
is the input redirection operator in the shell. When you write java SetTest < Ted.txt
, you're executing the command java SetTest
without passing any arguments to SetTest
and feeding Ted.txt into the program's standard input stream. This is not possible to do in Eclipse.
If you want to read from a file, you should pass a File
object as the argument to Scanner
's constructor rather than System.in
. That's what James meant by "create your Scanner using the file directly".
If by "const" you mean macros (as opposed to a variable that has been declared const
), it's convention in C and C++ for macro names to be in ALL_CAPS to easily distinguish between macros and functions/variables.
That's a matter of style and really up to you (or your team's style guidelines if you're a part of a larger team).
Okay and what about my other questions? What does line 29 in main.cpp look like? If you replace NewLine
with std::endl
, does that make a difference?
From the error message it looks like the problem might be because endl
is an overloaded function, so the compiler does not know which overload (and thus which type) to pick. This would be unrelated to the fact that you defined a NewLine
macro.
To fix this you could define NewLine
as a function that explicitly takes an ostream
as its argument. Or you could just use cout
normally and not bother with any of this, which is really the preferable option if you ask me.
Where and how are you using NewLine
? If you replace your use of NewLine
with std::endl
, does the error disappear? If so, that would kind of surprise me to be honest. If not, your problem has nothing to do with the fact that you defined NewLine as a macro.
Also what you posted isn't an error message. It's a note that gives you additional information about the preceding error message. Please post the whole error message and any notes attached to it.
why it the program won't stop until the 3 variable is already greater than 70??
Because your while condition says the the program should loop while either of the variables is less than 70. Let's say your variables are a=77, b=70, c=67. Is the statement "a is less than 70, b is less than 70, or c is less than 70" true? Yes because c is less than 70 and "false or false or true" is true.
If you want it to stop when either variable is greater than or equal to 70, you need to loop while all of them are less than 70. So you need to replace your ors with ands.
If you read the comments on the accepted answer, you get the whole story: The only native code in .net .exe is a single jump instruction. This native code is ignored on both modern Windows systems and on Mono. It's only relevant for Windows systems that predate .net.
None of this affects the compability between .net and mono. As far as that is concerned, all of this is irrelevant information.
the template have 2 parameters, but you transform 'N' in second parameter
What do you mean by "transform" here? I take N
as the second template parameter.
you don't use comma
There's a comma between typename T
and size_t N
- where else do you expect a comma?
Just to clarify: There are two template parameters (T
and N
) and one function parameter (arr
, which has type (T&)[N]
, i.e. "reference to an N-element array of Ts"). The template parameters are separated by commas. The function parameter is not as there's only one.