sepp2k 378 Practically a Master Poster

A pointer is the address of a variable (or the address of an array slot, the address of a struct member etc.). For as long as the pointed-to variable is alive, you can access its value by dereferencing the pointer.

sepp2k 378 Practically a Master Poster

There is no limit to how big the constant can be.

sepp2k 378 Practically a Master Poster

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:

  1. Condition is checked. If false abort.
  2. Loop body is executed.
  3. Increment is executed.
  4. Goto 1

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).

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

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).

sepp2k 378 Practically a Master Poster

You are making the wrong typecast

One doesn't need to (and arguably shouldn't) typecast the result of malloc at all. And much more importantly, ptr is still not a pointer, so that line won't work no matter what he casts the result of malloc to.

sepp2k 378 Practically a Master Poster

First of all ptr is an array (of pointers), not a pointer. So you can't assign to it.

Second of all, even if it were a pointer, it wouldn't be an int pointer, so casting the result of malloc to int* is wrong because that's the wrong type (also: don't cast the result of malloc). Similarly it does not make sense to use sizeof(int) as the basis for your size calculation as, again, it's not an int pointer.

sepp2k 378 Practically a Master Poster

zip accepts more than one argument.

And for cases where zip does not work, you can always iterate using indices (depending on the situation, using each_index, a range or even a while loop with a manually incremented index).

sepp2k 378 Practically a Master Poster

To iterate two arrays in parallel, you can use:

array1.zip(array2) do |element1, element2|
  # ...
end
sepp2k 378 Practically a Master Poster

What do you mean it does not work. The output I get when running your code (after removing the "..." in the hash of course) is:

2|0|1|1
This is key x1|This is key x2|This is key x3|This is key X350

In what way is that not the output you expected?

sepp2k 378 Practically a Master Poster

In Visual Studio you can press Ctrl + F5 or Shift + F5 (I don't remember which one) instead of just F5 to run your application in a console window that does not close.

In most other IDEs the output will either show up in a tab or a window that does not close automatically without you having to do anything special.

I do not recommend changing your actual code to accommodate a quirk of your IDE.

This is c++, not c.

The OP did ask about stdio, not iostream, though.

sepp2k 378 Practically a Master Poster

The problem is c "and the new skill value is": there's only a space between c and the string - no comma.

sepp2k 378 Practically a Master Poster

You're calling your parameter hash, but trying to access it as myHash. I'm also not sure that giving it a default value here is a good idea - it's not clear to me why calling it with an empty hash would ever be desirable - it just wouldn't do anything.

Other than that your code is correct.

Should I to pass as argument the array too?

Assuming you always want to start with an empty array, I'd just create it inside the method (as a non-constant) and then return it as the result.

If you want to be able to start with a non-empty array, passing it as the argument would be a good idea.

sepp2k 378 Practically a Master Poster
def my_method(hash)
  # ...
end

hash = {
  "foo" => "bar",
  # ...
}
my_method(hash)

Or even:

def my_method(hash)
  # ...
end

my_method(
  "foo" => "bar",
  # ...
)
sepp2k 378 Practically a Master Poster

Put your code in a method that takes the hash as an argument, then define the hash after the method and call the method at the end.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

Now the body of the loop consists solely of nr = newrow - everything else is outside of the loop.

sepp2k 378 Practically a Master Poster

Are you sure, you're getting the same error? Because from looking at the new code, I'd say you should be getting an indentation error now because you did not indent the body of the loop.

sepp2k 378 Practically a Master Poster

As the error message correctly points out, it is indeed not a inside a loop. In fact there is no loop anywhere in your function.

sepp2k 378 Practically a Master Poster

Also asked (and answered) here.

sepp2k 378 Practically a Master Poster

Even if I change the number within random()

The argument to random is not the seed.

sepp2k 378 Practically a Master Poster

I'm not sure that the seed is really 2 - you don't seem to set a seed anywhere in your code (the docs you showed, don't mention how to set the seed for random, so I assume you'd set it using srand just like you would when using the rand function).

But if it really is 2, that's your problem right there: If the seed is a constant, the numbers you get will be the same each time. You need a changing seed (like the current system time) to get different numbers.

sepp2k 378 Practically a Master Poster

What is random? It's not a standard function. Did you define it yourself or does it come with Turbo C++?

Anyway this random function probably needs to be seeded just like the standard rand function does. So if you don't provide a seed, you'll get the same sequence of random numbers each time when you run your program.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

it gives an error message at line 12

What's the error message?

sepp2k 378 Practically a Master Poster

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 …

Ancient Dragon commented: helpful +14
sepp2k 378 Practically a Master Poster

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).

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

I think there's been a misunderstanding. I didn't say that the method m should be defined in the interface. The method can be defined in any class you want as long as it has two overloads: one for Coffe and one for CoffeeInterface.

sepp2k 378 Practically a Master Poster

I think the problem is that it does not recognize uint8_t as a type name because the stdint.h header was not included. That would explain why the error happens "before 'r'", rather than on line 1 (also I believe that (AVR-)GCC is the only compiler that you can use to compile for AVR, so that would probably not be the issue).

sepp2k 378 Practically a Master Poster

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.

JamesCherrill commented: Good answer +15
sepp2k 378 Practically a Master Poster

still the same error...

What's your current code?

sepp2k 378 Practically a Master Poster

Is elements a collection of string pointers or of strings? Judging from the error messages I'd say it's the latter, so your lambda needs to take a string or string reference as its argument.

Since destroy takes a pointer, you'll have to pass a pointer to your string to it. Note that if you make your lambda take a string, it will have its own unique address that's distinct from that of the string in the collection, so you probably want to use a reference.

iamthwee commented: rashakil fol in hiding +14
Vasthor commented: always had this funny syntax going wrong, thnx though +2
sepp2k 378 Practically a Master Poster

So by writing the line "objNumber* num1obj;", you have created a pointer variable in heap

Like all local variables num1obj lives on the stack, not the heap.

sepp2k 378 Practically a Master Poster

num1obj has not been initialized and is thus neither pointing on the stack nor on the heap - it is pointing nowhere at all. Therefore dereferencing it invokes undefined behavior.

Note that this is in no way specific to objects. If you dereferenced an int pointer that you did not initialize, it would cause the same problem.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

An immutable object is an object whose state can't be changed in any way through it's public interface. So it has no public variables and none of its public methods change any of its variables. One example of such objects are strings.

You'd use those object like you'd use any other objects.

sepp2k 378 Practically a Master Poster

If you declare a variable as being of type Object, you're not going to be able to call any methods on that object other than the ones defined in the Object class (or pass it as an argument to any method that takes something other than Object as their argument) unless you use a cast.

If you use var the type of the variable is going to be the (static) type of the assigned expression. So you'll be able to call any methods on it that are defined on that type and give it as an argument to any method that takes arguments of that type.

If you use dynamic, you'll be able to call any method on it and pass it as an argument to any method without any type checking happening at compile time. If the types are not compatible, you'll get an exception at runtime.

sepp2k 378 Practically a Master Poster

You're counting all multiples of 15 twice (once as multiples of 3 and once as multiples of 5).

sepp2k 378 Practically a Master Poster

You need the def keyword to define methods in Ruby. The reason that you need a keyword for this is that the creator of Ruby decided that was the most convenient syntax for method definitions.

sepp2k 378 Practically a Master Poster

Instead of creating an array and a pointer just create a pointer and allocate memory.

Why do you find that preferable in this case?

str = (char *)malloc(sizeof(char)*20); // for 20 characters 

There's no need to cast the return type of malloc. Void pointers are implicitly castable to any other point type.

sepp2k 378 Practically a Master Poster

Yes, VLAs and implicit casts from void are not allowed in C++. You'll have to compile this as C code.

Ancient Dragon commented: yes :) +14
sepp2k 378 Practically a Master Poster

VLAs are perfectly valid in C99 and "GNU89" - the only reason it wouldn't work with gcc/MinGW is if you explicitly set the standard to C89. And it doesn't work in VS because Visual Studio's C support is completely outdated.

Oh and I should mention that this question has also been asked and answered here, so there's no need to repeat that here.

sepp2k 378 Practically a Master Poster

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.

sepp2k 378 Practically a Master Poster

How exactly did you install GHC? Did you install anything Haskell-related other than the main GHC package? And what's the exact error message you're getting?

sepp2k 378 Practically a Master Poster

I believe that H(t1) stands in for a list of all the synonymes of t1

According to the paper it stands for "a set of tuples (h, f), where h is a hypernym and f is the number of times the algorithm has found evidence for it".

So n stands for the number of times the algorithm has found evidence of h being a hypernym of t1 and m stands for the number of times the algorithm has found evidence of h being a hypernym of t2.

In general whenever you're unsure what exactly something means, it's a good idea to go back to where it was defined and read the definition carefully. In this case knowing that H produces a set of tuples - not just a set of hypernyms - was crucial to understanding what n and m are, so it's important to pay attention to those kinds of details.

ddanbe commented: Nice! +15
iamthwee commented: rashakil is this person +14