sepp2k 378 Practically a Master Poster

You're right and that 'variable' is the number of elements.

That variable is whatever you say it is. In the context of computer science it's usually the size of the input, but it could be anything as long as you make that clear. For example it's very common to express the complexity of graph algorithm in terms of two separate variables: the number of nodes and the number of edges.

Ordo notation removes any constant factor from the algorithm.

As well as constant summands, but that's not all. For example 3x^2+2x+1 is in O(x^2), so you don't just drop the constant factors and summands, you also drop any term t such that the limit of t/f(x) is 0.

Of course the function you're trying to classify may not be a simple sum of terms at all, so the dropping-constants-and-smaller-terms approach is just a rule of thumb.

sepp2k 378 Practically a Master Poster

Are you sure that that's the definition of check that's in scope when you call it? Do you perhaps have any other definitions of check in your program, perhaps even one that takes two arguments?

In any case, it would help if you gave us a reproducible code sample of your problem, that is a piece of code that, when we run it, produces the error you get.

sepp2k 378 Practically a Master Poster

You can't, you need a separate field (usually of an enum type) to keep track of which field of the union is set.

sepp2k 378 Practically a Master Poster

javac command not found in linux fedora

Have you installed the JDK?

vi .bash_profile
export JAVA_HOME=/usr/bin/java

JAVA_HOME should be a directory, not an executable, and you shouldn't have to set it yourself.

export PATH=$PATH:/usr/bin/java

Again PATH should contain directories, not executables, and /usr/bin is already in the PATH (otherwise you wouldn't be able to run java -version), so there's no need for you to adjust the PATH.

Edit: Sorry, I did not realize that this post was three years old when I posted.

sepp2k 378 Practically a Master Poster

If you do make them variables, dircopy $src $dst will work fine.

sepp2k 378 Practically a Master Poster

Learning Java on Arch Linux is no different than learning Java on any other system. You don't need an emulator - both the Oralce JDK and OpenJDK are available for Linux natively, as are all commonly used Java IDEs (eclipse, netbeans, IntelliJ).

So your steps for learning Java would be: pick the Java learning resource of your choice (be it a book, website, online course, personal tutor, whatever - this is not affected by your using Linux), install the JDK (pacman -S jdk8-openjdk - you might also want to install openjdk8-doc to view the Java docs offline and/or from within your IDE and openjdk8-src if you want to look into the sources for deeper understanding), install the IDE or editor of your choice, start programming.

sepp2k 378 Practically a Master Poster

If static modifier is not applied before does it makes it an instance method ?

Yes.

JamesCherrill commented: Sometimes the simplest answers are the best! +15
sepp2k 378 Practically a Master Poster

You check the file size after you've already opened the file for writing. Since opening a file for writing creates an empty file with that name (overriding the existing file if it exists), this means that a) the file will always exist and b) it's size will always be 0.

You need to check the size before opening the file instead.

sepp2k 378 Practically a Master Poster

It is indeed better to use printf("%s", message) than printf(message), but only because it prevents problems if the string contains characters that have special meaning to printf (like %), which isn't the case here anyway. As long as message does not include special characters, printf(message) will work fine.

Replacing message with (const char*)&message[0] makes no difference at all. Arrays decay to pointers to their first element implicitly and converting from a pointer to a const pointer also happens implicitly. There is no need for an explicit cast there.

sepp2k 378 Practically a Master Poster

By providing the flags mentioned in the error message: -std=c99 for standard C99 mode or -std=gnu99 for C99 with GNU extensions. For C++11 mode just replace 99 with 11.

sepp2k 378 Practically a Master Poster

Your joueur array holds only one element, but your index goes up to 1 (which would be the second element), which is out of bounds.

sepp2k 378 Practically a Master Poster

When I try to compile this program in gcc I get this error

As the error message says, you need to supply the mentioned language options to make the code compile. Alternatively, if you want to make your code compile in C89 mode, you need to replace char *p = message with p = message and then put char *p; at the beginning of the function. I'd personally prefer using C99(or even C11) though.

Also I'm having trouble understanding the for loop because I don't see a condition that says, if we hit '\0' we are done.

The condition is *p. In C an expression counts as false if it evaluates to 0 and true otherwise and \0 is simply another way of writing 0. So the conditions *p, *p != 0 and *p != '\0' are all equivalent.

sepp2k 378 Practically a Master Poster

To tell you where you went wrong, it'd be good to know how you got to ten. For each iteration of the outer while loop what's the value of i going in, what's the value of j after the inner while loop and what's the value of i after incrementing it by j?

sepp2k 378 Practically a Master Poster

i can`t understand this code....

It stores the number 1 in the variable dx if the number stored in the variable a1 is greater than that in the variable c1 and the value stored in b1 is larger than that in a1.

sepp2k 378 Practically a Master Poster

Did you create the text file on Windows? If so, it probably uses '\r\n' as the line ending, so after stripping the '\n', the passwords will end up as 'password\r' and thus not compare equal to 'password'.

That would explain why the problem does not occur in Windows as Python (like most languages) will automatically translate '\r\n' to '\n' when opening files in text mode on Windows (and only there).

sepp2k 378 Practically a Master Poster

The third is almost certainly the fastest (and the second the second fastest) as it only iterates over a third of the array (and the second over half). However this also means that the third and second functions don't do what you intend. If they did, there would unlikely to be too much difference between the performance of the three.

As far as asymptotic complexity is concerned, the previous answers already correctly pointed out that all three are O(n) and there's no point in saying something like O(n/2) (as it's just a more complex way of saying O(n)).

In the second expression, instead of n/2, use n>>1

I'd strongly advise against that. The two are very likely to generate the exact same code, but the former expresses the intent much more clearly. Also in order to be correct the functions shouldn't divide n at all.

sepp2k 378 Practically a Master Poster

As NathanOliver said, "t6" isn't a char, it's a char array. To get 't' and '6', you can simply access them as "t6"[0] and "t6"[1] respectively.

sepp2k 378 Practically a Master Poster

This has nothing to do with const. If you remove the const keyword from your program, you'll get the same error.

The problem is that the initializer for a global variable must be a constant. Confusingly the meaning of the word "constant" here has nothing to do with the const keyword. Here "constant" means either a literal (something like 5, 'c' or "hello") or the application of an operator where all operands are constants. So something like 3 + 5 would be okay, but 3 + x or just x would not be.

Basically the initializer of a global variable must not depend on the value of any other variable or function.

sepp2k 378 Practically a Master Poster

Also asked (and answered) here.

ddanbe commented: OK +15
sepp2k 378 Practically a Master Poster

What you posted does not look like source code, it looks like what you might see when you open a binary file in a text editor. Presumably you downloaded the zcode file (which is a bytecode format), not the source code. I do not think the source code of Zork is publically available at all, which would explain why you were unable to find it.

PS: Neither Zork nor Adventure were written in Python, so even if you did find the source code, it wouldn't be in Python and reading it would probably not help you at all with learning Python.

sepp2k 378 Practically a Master Poster

use 'out' parameter

There's no such thing in C++.

sepp2k 378 Practically a Master Poster

A getUserInput function simply can not be sensibly defined with that signature. It either has to return something other than void or it has to take its parameters by reference (or pointer), so that it can change them.

sepp2k 378 Practically a Master Poster

O (and o, which the OP asked about) describes the growth of a function. That function often describes the running time of an algorithm, but that's not important here.

A statement like "2^n = O(n!)" (or "2^n = o(n!)") does not mean that the running time of an algorithm to compute 2^n is in O(n!)¹, it means that the asymptotic growth of the function f(n)=2^n is less than or equal to that of g(n)=n! ignoring constants (and likewise "2^n = o(n!)" means that the asymptotic growth is strictly less than). For an exact definition you can look at the Wikipedia article on the subject.

Both the statements "2^n = O(n!)" and "2^n = o(n!)" are true because 2^n does indeed grow more slowly than n!.

¹ Note that that wouldn't even make sense as there are infinitely many algorithms to compute 2^n and they don't all have the same running time.

sepp2k 378 Practically a Master Poster

I'm not sure what's left to be explained. In your example the third case calls split with zero as the second argument and in the fourth case you call it with no second argument. As documented this causes trailing empty strings to be discarded.

sepp2k 378 Practically a Master Poster

From the Java docs (emphasis mine):

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

The one-argument version of split is equivalent to the two-argument version with n being zero.

sepp2k 378 Practically a Master Poster

You're only getting one occurence because you're only calling find once. If you use a while-loop instead of an if, you'll call it until it returns false, which will give you all the matches.

sepp2k 378 Practically a Master Poster

Use an if to check whether you're in the last iteration (i.e. whether i == x). If you are, don't print a space after the number, otherwise do.

sepp2k 378 Practically a Master Poster

"separated" means that the space should be between the numbers. Your code also adds one space after the last number, which is probably not allowed.

sepp2k 378 Practically a Master Poster

1 <= X <= 10 isn't a condition that you're supposed to check in your code. It's just telling you that X will be in that range, so you can take that into account when considering performance or picking data types. It's perfectly okay to ignore that information.

The reason that your code is not accepted is this:

The numbers should be separated by one space.

sepp2k 378 Practically a Master Poster

I just understood that big O is an asymptotic notation and used to classify algorithms according to their response time and memory they take. and I have no idea how to apply that def on this equatn.

That's not a definition; it's a description and not a very descriptive one at that. That description doesn't even explain the difference between O and o nor does it make any specific statements about when a function is in the o of another function. It is plainly impossible to show (or even intuit) whether a^n = o(n!) based on that description alone.

So your first step should be to review your lecture notes to find out whether a real definition of o was given in class. If you can't find it in your notes (or you don't have access to any notes), you can try to use the one from Wikipedia and hope that that's the one your professor wants you to use.

PS: To reply to a post, please use the text box at the bottom of the thread with the "Submit your Reply" button. Writing replies in upvote comments makes them easy to miss and the thread hard to follow.

sepp2k 378 Practically a Master Poster

If the implementation of array uses a pointer to dynamically allocated memory (which is pretty much a given unless there's some static maximum size for arrays) and the memory that ptr points to has been allocated using malloc without placement new, then calling operator = will very likely call delete on an uninitialized pointer and/or write to memory it does not own¹. That's why it won't work, not because the wrong thing is being copied.

if the class contents is not actually the array data, because for example it uses a pointer to allocated memory, then what gets stored in the rawdata buffer is not the array contained by bytearray at all.

What will or won't be copied is entirely determined by what operator = does, not by whether or not the implementation of array uses pointers.

¹ I'm assuming here that operator = is defined along the lines of the following with data and size being unitialized if malloc has been used without placement new:

array<T>& operator =(const array<T>& other) {
    if(size != other.size) {
        delete data;
        size = other.size;
        data = new T[size];
    }
    for(int i = 0; i < size; i++) {
        data[i] = other.data[i];
    }
    return *this;
}
sepp2k 378 Practically a Master Poster

If this is what you are trying then what you have written wont necessarily work, it rather depends on the implementation of bytearray, whether it holds the actual data to an internal pointer to the data

Actually what it depends on is whether bytearray's operator = does anything that requires the constructor to have run previously - though technically, I believe, calling operator = on an unitialized object invokes undefined behavior even if that is not the case.

If the OP is indeed using malloc, the memory needs to be initialized using placement new before any members (including the assignment operator) can be used on it. Though preferably malloc shouldn't be used at all.

because in the second case your memory buffer would end up containing the pointer rather than the data.

That completely depends on what operator = does. If the OP's array class acts at all like a standard container class, assigning one array to another should make the assigned-to array have a pointer to newly allocated memory that contains a copy of the other array's contents.

The problem is that you have not explained what you are doing in very much detail so it is hard to work out what the correct answer is.

Indeed.

sepp2k 378 Practically a Master Poster

Assuming that the pointer points to a correctly initialized instance of type bytearray and the bytearray class is correctly assignable (creating an independent copy that will still be usable once the original instance goes out of scope), then yes, the part of your code that you've shown should work.

Given that it does not, at least one of those things is not true - probably the former.

sepp2k 378 Practically a Master Poster

Well, if the class is correctly defined (which one can assume if it's part of a library that you were given), I told you what the most likely problem is. That's really all I can do with the given information.

sepp2k 378 Practically a Master Poster

So you did post in the wrong forum. And array is your own class, right? Because std::array would require two template arguments.

In any case, assuming that your array class is correctly defined and all the constructors, the destructor and the assignment operator work as one would expexpt, the most likely explanation is that ptr does not point to a correctly initialized instance of the array class and thus calling the assignment operator on it invokes undefined behavior.

sepp2k 378 Practically a Master Poster

What's the definition of o that you learned? Do you understand that definition? What are your thoughts on how that definition may apply to a^n = o(n!)?

zeedote commented: I just understood that big O is an asymptotic notation and used to classify algorithms according to their response time and memory they take. and I have no idea how to apply that def on this equatn. +0
sepp2k 378 Practically a Master Poster

What's bytearray and how and where is it defined? Unless you posted this in the wrong forum and this is actually C++ (and bytearray is a class that you defined somewhere), I don't understand how the code even compiles (I suppose temp might be a macro, but that wouldn't make any sense).

sepp2k 378 Practically a Master Poster

First of all you need to include string.h, not string, to get the declaration of strcmp (or even better keep including string, use the string class and don't bother with strcmp at all).

Secondly A is an array of chars, not strings or char pointers, so A[i] is a char and thus not a valid argument to strcmp. It also can't possibly be equal to "new" for that reason.

sepp2k 378 Practically a Master Poster

The syntax is right, but for template functions the definition needs to be in the header file, not the .cpp file.

sepp2k 378 Practically a Master Poster

The compiler will not ever complain, so why should a person?

That's not very sound reasoning. A compiler will also not complain when you name all your variables variable1, variable2 etc., but I can still think of plenty reasons why you shouldn't do that.

sepp2k 378 Practically a Master Poster

why do most people use brackets in this way when it comes to Java.

Because that's what the official style guide recommends.

The second method is obviously easier to read.

That's not obvious at all. I imagine that most people will find the style easier to read that they are used to.

sepp2k 378 Practically a Master Poster

I don't think it's a good idea to define only one class for all arithmetic expressions and I don't think that's what your TA meant either. What you can do is to define one class that corresponds to the a1 opa a2. That class would look like your Add and Sub classes except it would also contain a member for the opa, which you could define as an enum.

sepp2k 378 Practically a Master Poster
Cabin(8) = Cabin(8/2) + 1
         = Cabin(4) + 1
         = Cabin(4/2) + 1 + 1
         = Cabin(2) + 1 + 1
         = Cabin(2/2) + 1 + 1 + 1
         = Cabin(1) + 1 + 1 + 1
         = 0 + 1 + 1 + 1
         = 3

That's why the answer is 3.

sepp2k 378 Practically a Master Poster

Oh, I seem to have misread the last sentence of your question (I thought you wanted us to explain what an AST is and how to implement it in Java).

To answer your actual question: I think by "specify" the assignment simply means "define". That is it wants you to define the classes that make up the AST.

sepp2k 378 Practically a Master Poster

An abstract syntax tree is a tree where each node represents a syntactical element and each child of a node represents a sub-element of that element.

For example for the statement set x := y + 42; the AST would consist of an assignment node whose left child node would be an identifier node with the value "x" and whose right child would be an addition node. The addition node's left child would be an identifier node with the value "y" and its right child would be an integer literal node with the value 42.

To represent an AST in Java you'd define a class for each type of syntactical element that your language contains, using abstract base classes and inheritance to allow for the fact that multiple types of elements can be used as child nodes for some nodes (for example the child nodes of most expressions are themselves arbitrary expressions, so you'd have an abstract base class called Expression from which classes like Addition and IntegerLiteral inherit, so if a node has a member of type Expression, either an Addition or an IntegerLiteral (or any other type of expression that you implement) can be used there).

sepp2k 378 Practically a Master Poster

The power function has three returns. The first is inside the first if, the second (which is wrongly indented) is inside the second if and the third is outside of any if. The third will only execute if none of the previous ifs were executed (just like the second will only execute if the first did not execute (and the if condition is true, of course)), so you can think of it as being inside an implicit else, yes.

sepp2k 378 Practically a Master Poster

If we just take into account precedence (and not associativity), then all we need to know is the priority of the current operator as well as the one whose operand the current expression is. If the outer priority is higher than or equal to the inner one, we need parentheses. Otherwise we do not.

To implement this you can define a recursive helper method that takes the outer priority as an argument. The one-argument printInfix method would then only call the helper method with the priority 0 (0 because we don't want any parentheses around the outer expression).

Note that this does not remove the parentheses in (a + b) + c or a + (b + c) because those are about associativity rather than precedence. Handling that would be a bit more complicated.

sepp2k 378 Practically a Master Poster

but the first sprintf works, the one that writes toDB[0] to param1

The first sprintf causes undefined behavior just like the second does. Some times undefined behavior means that you get a segfault and some times it seems to work fine. That doesn't change the fact that it's undefined behavior.

sepp2k 378 Practically a Master Poster

"instead"? Don't you get all four?

Anyway what happens is that first the super class constructor is called (causing "B::B(3)"), then the member constructors are called (causing "B::B()" because of your member b) and then the body of the constructor is executed (causing the other two outputs).

sepp2k 378 Practically a Master Poster

The return type of your * operator is vector. So when you return a double from that operator, it gets automatically converted to vector (via your constructor - if you don't want implicit conversion from double to vector, mark your constructor as explicit). However there's no implicit conversion from vector to double, so trying to assign the vector v2*v1 to the double z is an error.

Since there's no reason for the result of multiplication operator to be a vector, you should probably just change the return type to double.