sepp2k 378 Practically a Master Poster

So why does the foll code doesnot give any exception although e points to object of same class??

Your original code created a new Animal object every time that an Animal object was created. Your new code only creates a new Animal object if you call the foo method. You call the foo method on the first Animal that you create, but you don't call foo on the second Animal (the one that you create inside foo). So it stops there and no infinite recursion happens. If you added the line e.foo() inside your foo method, you would get infinite recursion again.

sepp2k 378 Practically a Master Poster

Each Animal object has an instance variable named a. Every time a new Animal object is created, its e variable is initialized to new Animal(), which creates a new Animal object. So every time you create a new Animal object, you create another Animal object. So if you create one Animal object, you create an infinite amount of Animal objects. This leads to a stack overflow.

sepp2k 378 Practically a Master Poster

Nevermind.

sepp2k 378 Practically a Master Poster

It's always incorrect to use a pointer that doesn't point to valid memory to do anything at all.
It's perfectly correct to take a pointer that points to valid memory containing a 0-terminated sequence of characters and pass that to a function that expects a string.

sepp2k 378 Practically a Master Poster

Many string functions seem to work without the null byte... is it necessary to add it all the time?

Which functions are you talking about specifically? If the function takes a size as an argument or only reads up to a certain delimiter in the string that's guaranteed to be in the string, you won't need a 0-byte. Otherwise you will. Generally all functions that you'd usually refer to as "string functions" (that is the ones in string.h) do expect the string to be nullterminated.

Note that since the byte that comes after a string in memory could very well be 0, it's perfectly possible that you might still get the correct result if you pass a string without a 0-terminator to a function that expects a 0-terminated string. However that's undefined behavior and will break as soon as the next byte in memory happens to be something else.

and my second question, does scanf %s automatically ads the null byte?

Yes.

sepp2k 378 Practically a Master Poster

cproger obviously posted to help.

That's not obvious at all. If it was just an accident that he mispasted the code in this question, then why did he do the same thing (posting an error message that was not caused by the code in the thread) in every other thread he responded to? He can't be that bad at copy and pasting. Why did he post a link to w3schools in a thread that had nothing to do with the web? Because he's such a newbie that he can't tell the difference between HTML and C++?

No, because he's a troll who gets a kick out of people responding to him and trying to correct him. And maybe as an experiment to see how many people he can get to believe that he's acting in good faith.

cproger commented: Be quiet for once please +0
sepp2k 378 Practically a Master Poster

In Python (and many other languages) there is a difference between changing variables and changing objects. A statement of the form myvar = new_value will change the variable myvar. Most other things that you can do with myvar (like myvar[i] = foo or myvar.append(bar)) will change the object, not the variable.

If you execute a statement of the form myvar = my_other_var, then the two variables will now refer to the same object. If you change that object, the change will be visible through both variables. However if you change one of the variables to refer to a different object, that change will not be visible to the other variable and the variables will no longer be connected in any way.

So names = deque(...) does not affect nmls because it's changing the variable, not the object.

sepp2k 378 Practically a Master Poster

The line dice1, dice2, dice3 = numb(dice) does not appear in the code you've posted, so I think you accidentally posted the wrong code. Assuming that the definition of numb that you gave in the code you've posted is the same one that you use in your real code and assuming that everything up to line 30 is supposed to be indented, the error message that you quoted would be caused if the value of dice is something other than 3.

sepp2k 378 Practically a Master Poster

Look I'm not saying anything bad?

Yes, you are. First of all nothing you've said is helpful and adding to a thread without adding value is bad because it revives the thread without reason - especially when a correct answer has already been given.

Secondly your advice in this thread was actively harmful in that it has nothing to do with the OP's problem and would only cause him to look for his problem in the wrong place if here were to take it seriously.

sepp2k 378 Practically a Master Poster

None of the code snippets posted in this thread even have a line 21. Certainly none of them have any errors like the ones you're talking about. It seems like you're just saying stuff at random.

sepp2k 378 Practically a Master Poster

Cproger, could you please clarify what you're trying to say? You've posted an error message that one might get if one misspelled struct as truct. However no one misspelled struct that way in this thread. So I'm not really sure how your post relates to this thread.

sepp2k 378 Practically a Master Poster

No, these are not equivalent at all. If you try to compile and link the second piece of code, you'll get a linker error telling you that a has not been defined.

It is true that the default linkage is external, but extern does more than just saying that a variable's linkage is external. It also says that this is only a declaration - not a definition - and the corresponding definition can be found elsewhere (generally in a different compilation unit).

sepp2k 378 Practically a Master Poster

I thought I did: It means that if you're declaring a variable with extern, it must have external linkage and thus can't be static.

sepp2k 378 Practically a Master Poster

I meant what you're trying to do in your code: Define the variable at the end of the file, but declare it before then. You can't do that with a static variable.

sepp2k 378 Practically a Master Poster

What exactly were you trying to measure here? If you just wanted to measure the cost of lookups, wouldn't it have made more sense to just measure a function that makes a bunch of lookups in an already-built list or dictionary without measuring the time it takes to build the list or dictionary?

By the way: When you say that the time of the list code increases by factor 100+ when the input size increases by 10, that's not really accurate. It increases by around 100 the first time you increase the input size. The second time, the increase factor is about twice that. And if you increased the input size again, the runtime would increase by an even larger factor. That is to say: the runtime of your list code is quadratic, not linear.

Lists are sequential in memory, meaning that the program has to get the offset for the beginning of the item it wants to use, go to that offset, compare to what it wants to find, and go back and do it all over again for each element in the list.

That's not really accurate. It starts at the memory address where the list begins and then increases the address by the size of the list elements (i.e. sizeof(void*) in Python) until it finds the element it's looking for. There's no going back involved.

One would expect the same increases for a tenfold increase in the size of each individual item being stored …

sepp2k 378 Practically a Master Poster

If you're declaring a variable with extern, it must have external linkage. This means it can't be static. It is not possible to forward-declare a static variable. So your only option are to not make it static or to put the definition at the top of the file and get rid of the forward-declaration.

sepp2k 378 Practically a Master Poster

Why did you ask where the semicolons are missing if the error messages you're getting specifically say "missing ;" with a line number?

Anyway, the code uses a lot of functions (or macros) that aren't defined anywhere, so clearly it's incomplete. I don't know what to tell you beyond that.

sepp2k 378 Practically a Master Poster

As I said already, you should either forward-declare both functions or move their defintions before main. It doesn't really matter which one though forward-declarations are more common. As for the missing semicolons, I was talking about the calls to insert_reserved. But if that's defined as a macro, the missing semicolons might in fact be intentional. Though in that case, I'd have to say it's a rather badly defined macro - macros should generally be defined in such a way that they can be followed by semicolons in the same contexts as functions are (and they should be named in ALL_CAPS).

Anyway insert_reserved doesn't seem to be defined anywhere, so that's another error.

sepp2k 378 Practically a Master Poster

Yeah, there are a couple of mistakes in there. In addition to leaving out includes (which I assume was intentional), leaving out the forward declarations (which might have been intentional as well - after all it's not a C tutorial and forward declarations don't help you understand lexing), they also forgot semicolons in a couple of place. And now I see that you copied that mistake, too, so that's one thing that will be causing errors in your code.

sepp2k 378 Practically a Master Poster

Then you should fix those, too.

It's not uncommon that fixing one error will produce multiple other errors that were previously unreported. That does not mean that you're going in the wrong direction (even if intuitively one might think that few error messages are better than many error messages).

sepp2k 378 Practically a Master Poster

You're defining them on lines 46 and 54 respectively. You're calling them on lines 41 and 42 (according to the line numbers in the code you've posted here - the error message says 43 and 4). 46 comes after 41 and 54 comes after 42. And as I said, "in C++, the declaration of any identifier generally must come before its use".

So either move your definitions to the top or use forward declarations.

sepp2k 378 Practically a Master Poster

As the error indicates those names are not declared at the point where you use them. In C++, the declaration of any identifier generally must come before its use. So if you want to use InitScanner and ScanOneToken on lines 43 and 44, you need to declare them before then.

The usual process is to put declarations for all functions that you define in your file at the top of your file or into a header file that you include.

sepp2k 378 Practically a Master Poster

Why are you trying to boot from CD 5? I would expect that CD 1 is the boot CD and CD 5 is not.

sepp2k 378 Practically a Master Poster

No, you can't do this - . and ' aren't overloadable operators. You can overload & for AND and ! for NOT though (and make OR | while you're at it). That way the operators are also consistent with those of the builtin bool type.

PS: How exactly do you want to make your type simpler than bool? What's complicated about the existing bool type?

sepp2k 378 Practically a Master Poster

Yes, that's fine. The problem about the wrong number of arguments should be fixed now.

There wasn't any error.

It would have been a warning - not an error. And as I said: if you didn't get one, increase your warnings level.

Another problem I just noticed is that your variables are doubles, but you're using %E/%f, which expects a float. That would explain why you're still having trouble. Use %lf instead.

sepp2k 378 Practically a Master Poster

The compiler doesn't recognize Node until after the typedef has been fully parsed. So you can't use the typedef inside the struct. In order to write a recursive struct, you'll need to name your struct without typedef and use that name in the struct.

struct node {
    struct node* next;
};

You can still typedef it if you want, but you'll only be able to use the typedefed name outside of the struct - inside the struct you need to use the struct name.

sepp2k 378 Practically a Master Poster

GeneClaude What do you mean by "I also tried them separated"? What did you separate? Please post the code you tried.

PS: If you didn't get a warning, you should increase your compiler's warnings level. Every decent compiler should give a warning when you pass too many or too few arguments to scanf (assuming the format string is constant, which it was in this case).

sepp2k 378 Practically a Master Poster

Do you want to know how to concatenate a string and an integer using +? If so: by converting the integer to a string first. That might look like this:

s = "foo"
x = 42
foo42 = s + str(x)

That said using format might be better than using + here:

"foo{}".format(x)
sepp2k 378 Practically a Master Poster

Using which library? Or if that's part of the question: for which operating system?

sepp2k 378 Practically a Master Poster

You should be getting a warning on line 19 telling you that you gave two pointers to scanf, but your format string only contains one specifier. If you didn't get that warning, you should increase the warnings level of your compiler.

sepp2k 378 Practically a Master Poster

The + operator associates left to right, so when you write "A+B= "+a+b that's parsed as ("A+B= "+a)+b. "A+B= "+5 is "A+B= 5" and "A+B= 5"+7 is "A+B= 57", so that's the result. You need to add 5 to 7 first and then add the result to the string. To do that put 5+7 in parentheses.

sepp2k 378 Practically a Master Poster

In Python 2 the / operator returns an integer if both operands are integers - rounding the result down if necessary. So 3/2 would be 1 in Python 2. In Python 3 this behavior has been changed, so that / always returns a float even if both operands are integers. So 3/2 would be 1.5 and 4/2 would be 2.0.

The code you've posted was apparently written for Python 2 where image_width/width would result in an integer. Presumably you're using Python 3 where it results in a float and that's why you get that error. To fix this you can replace the / operator with the // operator, which performs integer division (in both Python 2 and Python 3).

sepp2k 378 Practically a Master Poster

That won't actually fix the issue and it will cause another issue: The loop will now quit as soon as one of the files' end has been reached, so if the files contain a different number of entries, it won't read all the numbers.

sepp2k 378 Practically a Master Poster

You seem to think that if the end of file is reached, fscanf will store EOF in the given variable. It won't - it will simply keep the variable unchanged. The only way to tell whether fscanf successfully read a value is to check the return value of fscanf - you can't tell by looking at the variable.

The easiest way to make your code work would be to simply have two loops after each other: One that goes through the first file and one that goes through the second. To avoid code duplication, you should define a function that takes a file as an argument and then sums that file. Then you can simply call that function once with the first file as the argument and once with the second.

SoreComet commented: Never thought of this! thank you +0
sepp2k 378 Practically a Master Poster

Yes, you can use class files from different directories by adding all the directories to your classpath. If you're using an IDE, you can do so in your project's settings. If you're compiling from the command line, you can use the -classpath switch or the CLASSPATH environment variable to set the class path. On Windows multiple directories are separated by a semicolon.

So if you have p1.abc in d:\stuff\p1\abc.class and p2.pqr in e:\otherstuff\p2\pqr.class, you can compile a class that uses both those classes from the command line like this:

javac -classpath d:\stuff;e:\otherstuff MyClass.java
sepp2k 378 Practically a Master Poster

What happens when you add an element to a vector that is already at capacity?

The capacity is increased (often doubled, but that's up to the implementation), new storage is allocated with the given capacity and the contents of the vector are copied to the new storage.

Does std::vector have a "capacity"?

Yes. You can query it with the capacity method and manually increase it with the reserve method.

sepp2k 378 Practically a Master Poster

Variables that are declared inside a method will always go out of scope at the end of that method. You don't need to do anything special to make it that way.

Aaron_JY commented: Never knew this. Thank you. +0
sepp2k 378 Practically a Master Poster

Do you mean "how to tell what an algorithm's run time is" or "how to tell which notation to use (i.e. O or Theta or Omega)"? If you mean the latter: You should use the notation that the assignment asks for. when in doubt, use Theta as its the most specific.

If you mean the former: The basic approach is to understand exactly what the algorithm does and then ask yourself how many steps it will take for a given n. Of course that's easier said than done. Plenty of tutorials have been written about this.

sepp2k 378 Practically a Master Poster

The distinction between O, Theta and Omega isn't about best- versus worst-case. You can use any of these notations to analyze any case you want. That is, the statements "QuickSort's worst case is in Theta(n^2)" and "QuickSort's best case is in Theta(n log n)" are both valid usages of Theta.

Instead the distinction is that O specifies an upper bound, Omega a lower bound and Theta both. What this means is that "Foo is in O(n^2)" means that Foo is quadratic or less (that is when something is linear, it's also in O(n^2) because linear is less than quadratic). Similarly Omega(n^2) means quadratic or more (that is something qubic or even exponential will also be in Omega(n^2)). Theta(n^2) means it's exactly quadratic. That is when something is quadratic, it's in Theta(n^2), but something that's linear, cubic or exponential is not. Something is in Theta(f(n)) if it is both in O(f(n)) and Omega(f(n)).

sepp2k 378 Practically a Master Poster

I don't have a problem. I just pointed out that the OP did not ask about any error, so I was a bit a confused about what your replies were about. If I was rude about it, I apologize. I didn't mean to be.

sepp2k 378 Practically a Master Poster

Scheme already has a function named append, which takes multiple lists as arguments and returns the concatenation of all the lists. So you should name your function something else, like append-element perhaps.

You can define your function by simply using the existing append function to concatenate the list you want to append to with another list containing only the item you want to append like this:

(define (append-element x xs)
  (append xs (list x)))
sepp2k 378 Practically a Master Poster

The reason that your numbers look the same when you print them is that by default printf rounds the number to 6 decimal places. If you use something like %.20f, you'll see that your two numbers really look something like this:

float=3.09999990463256835938
double=3.10000000000000008882

So you see that the two numbers are in fact not equal.

The reason for that is that 3.1 can't be exactly represented as an IEEE floating point number and the closest representation using 32 bits is different than the closest representation using 64 bits.

sepp2k 378 Practically a Master Poster

You can not change the superclass of a class after it has been defined. That's just not possible. If you just asked out of curiosity, that's basically all there is to say.

However if you asked because you have a specific problem that you think could be solved by changing the superclass of a class after the fact, you should describe that problem, so that we can think of alternative ways to solve that problem.

sepp2k 378 Practically a Master Poster

Here's one example of a class variable being used at the top level of the class definition:

class MyClass
  @@number_of_instances = 0

  def self.number_of_instances
    @@number_of_instances
  end

  def initialize
    @@number_of_instances += 1
  end
end

As you can see, the class variable @@number_of_instances is used in 3 places here: it is read in the class method number_of_instances, it is incremented in the instance method initialize and it is initialized to 0 at the top level of the class definition.

@LastMitch:

You want to see how the error looks like?

What error? No one mentioned any error except you.

sepp2k 378 Practically a Master Poster

Since the same question has been asked on dreamincode, I'll stop responding here. There's no point in having the same discussion twice.

sepp2k 378 Practically a Master Poster

The first two lines don't load anything. You'd need a load instruction for that. They simply perform pointer arithmetic.

The first line makes $t0 point to the fifth byte of the A array. If A is an array of 32-bit values, that means that $t0 points to the second element of the array - not the fourth or fifth.

The third line takes the value that's in the register $t1 (i.e. the address of A) and stores it in the memory location that $t0 points to, i.e. it stores it as the first element of A.

The fourth lines takes the second element of A (which has been set on the previous line to be the address of A) and stores it in the register $t0 (which previously stored the address of that element).

You seem to understand the fifth line fine.

sepp2k 378 Practically a Master Poster

There are a couple of problems with your code: First you redefined display to basically do nothing. With that definition of display you'll never get anything printed to the screen, no matter how you define your foreach macro. You should get rid of your definition of display, and just use the built-in display function.

Then you hardcoded (display x) in your expansion. Since I assume, you're supposed to be able to use arbitrary expressions with foreach, that doesn't make much sense.

On a related note the do part of the foreach syntax isn't present in your pattern.

Also he way it is now, a user would be able to pass in arbitrary expressions in place of :. Once you add do to the pattern, the same thing is going to apply for that as well. To avoid this, you should add do and : as keywords.

Once you've got your patterns sorted out, you should think about what you want your code to expand to. As I said, you can't just hardcode (display x) in the expansion, so you should think of a general construct that your foreach loop can expand to, that binds the given identifier and works with arbitrary expression.

PS: Don't bother reading the links that rubberman posted. The define-syntax facility of Scheme has absolutely nothing in common with the macro system of Common Lisp, so those tutorials are just a red herring.

As far as tutorials about define-syntax go, this one looks pretty decent. …

sepp2k 378 Practically a Master Poster

It should also be pointed out, that it's not only variables that take up space - temporary values do too. So if you're thinking about removing variable declarations in favor of larger expressions in order to save memory (or registers), don't - it won't work.

sepp2k 378 Practically a Master Poster

No, it doesn't.

sepp2k 378 Practically a Master Poster

Let me correct myself: It would compile in C++, but not in C. Though now that I think about it, a compiler might theoretically refuse to compile a program that it know invokes UB, right? So that's not necessarily true either...