sepp2k 378 Practically a Master Poster

If num starts at 0, then the assignment should be num = 0 then num should increment during the postfix incrementation of num!?

The increment happens before the assignment. Postfix increment does not mean that the increment happens after the current statement. It only means that the result of the expression will be the value of the variable before incrementing (as you can see in the quote of the specification posted by James).

Basically if you have foo(x++) that's equivalent to oldX = x; x += 1; foo(oldX), not foo(x); x += 1;. So x = x++; is the same as oldX = x; x += 1; x = oldX, not x = x; x += 1.

I still feel like, you may have just copied someone elses work, posed as a professor, and want us to explain their code.

He already knows that replacing num = num++ with just num++ will fix the problem, so how would explaining why num = num++ doesn't work, help him take advantage of someone else's work?

When I write this in C++ I get 1

The C++ code invokes undefined behaviour. You might very well get different results on other implementations. If the behaviour were defined (i.e. if = introduced a sequence point), you'd get 0 just like in Java.

sepp2k 378 Practically a Master Poster

You set ehealth to 15 on line 26, which is inside the loop. Then you subtract a random amount of damage between 5 and 9. So ehealth is now somewehere between 6 and 10. That's still above zero, so the loop repeats, jumping back to line 23. So line 26 will execute again, and ehealth becomes 15 again. So the loop will continue forever without ehealth ever becoming zero (or less) because you always set it back to 15 before subtracting damage.

To avoid this problem, you should set ehealth to 15 once before the loop and then never again. Same thing with thealth.

sepp2k 378 Practically a Master Poster

The only way to make println not call the default toString method is to define your own toString method. If you want the contents of the variables to be printed, you'll need to define a toString method that returns a string containing the contents of those variables.

sepp2k 378 Practically a Master Poster

Let's say you pass in a list with one element. Then Count will be 0 (because the length of T will be 0). Therefore Count > 1 will be false and since there are no alternatives, the whole thing will be false. The same thing will be true if you pass in two elements (because 1 > 1 is still false) or even if you pass in more elements than that (because eventually you'll recurse down to a list that has less than 2 elements).

sepp2k 378 Practically a Master Poster

You need to move Count1 is Count + 1 after the recursive call to countvowels. Otherwise Count doesn't have a value yet at that point.

sepp2k 378 Practically a Master Poster

After making all the changes I mentioned, the code works fine for me. Can you post your current version of the code?

sepp2k 378 Practically a Master Poster

Oh, yes, you also need to move that part after the recursive call. Otherwise Count doesn't have a value at that point either.

Lastly you need to remove the Count1 > 1 bit. Otherwise it would produce False when given a list with only one element and thus, due to the recursion, when given any non-empty list.

sepp2k 378 Practically a Master Poster
Count is Count1 + 1

I think you meant Count1 is Count + 1 here.

PS: As your code is currently written, it counts any item in te given list - not just vowels.

sepp2k 378 Practically a Master Poster

Prolog lists are linked lists - not arrays. However like arrays they are used to store an arbitrary amounts of data, so they're used for many of the situations where you'd use arrays in imperative langauges. Does that answer your question?

sepp2k 378 Practically a Master Poster
germantoenglish([S|H],[G|Ta]):-trans(S,Ta),trans(H,Ta).

Calling trans with S as the first argument is a step in the right direction, but by using Ta as the output, you're making Ta a symbol - it's supposed to be a list though. And you're still calling trans with H as the argument in the second call. As I said, you can't call trans with a list as the first argument (or the second argument for that matter). You need to call germantoenglish recursively to translate the rest of the list. And, as I said, you also need to handle the case where the list is empty.

sepp2k 378 Practically a Master Poster

[S|H] is a pattern that says "this argument must be a list with at least one element. The first element shall be called S and H shall be a list containing all the other elements". You use [e, t, th] for this argument, so S becomes e and H becomes the list [t, th]. Then you call trans(H, Ta). So now prolog searches through the rules of trans to find one that matches the first argument [t, th]. However there is no rule for trans that accepts any kind of list, so no rule is found. Therefore you get false.

So there are basically two things wrong with your rule: 1. You call trans with a list as its first argument when it should be a symbol. 2. You never do anything with S.

trans should be called once for each symbol in the list with that symbol as its first argument. To accomplish that you'd either maplist or recursion. Note that when implementing recursion, you should add a rule for the case where the list you're trying to translate is empty.

ddanbe commented: Very clear answer +14
sepp2k 378 Practically a Master Poster

Predicate or procedure is what Prolog calls its "functions". So fib/2 is a predicate.

You can't write fib(N-1, AF), you have to write something like NM1 is N-1, fib(NM1, AF).

sepp2k 378 Practically a Master Poster

You can't use arithmetic expressions directly as arguments to predicates in Prolog - you need to asssign them to a variable using is and then use that as the argument:

N >= 0, NM1 is N - 1, NM2 is N - 2, fib(NM1, AF), fib(NM2, BF), NF is AF + BF.
sepp2k 378 Practically a Master Poster

If you just write int sum or int sub, you'd be using those variables uninitialized in your loop, which would invoke undefined behavior. So don't do that.

Your while loop will run until value is either equal to 'q' or 'Q'. This will only happen if the user enters 113 or 81 as the input because value is an int, not a char. If the user entered q or Q, that would simply be discarded as invalid input and value would not change.

sepp2k 378 Practically a Master Poster

== is an operator that takes two operands and produces the value true if the operands are equal and false if they're not. The boolean values produced by == aren't any different than the boolean values stored in your variables. There is no context in which using == would be okay, but using a boolean variable would not be (assuming there isn't some stupid operator== overload in play that returns something other than a boolean). A boolean expression is a boolean expression.

Same goes for !: you can use it anywhere you can use any other boolean expression.

sepp2k 378 Practically a Master Poster

No, it's perfectly fine to write if(variable1 && variable2). What makes you think that doesn't work?

sepp2k 378 Practically a Master Poster

You set ret to minsol even if minsol was never assigned in the loop. This means that when you reach a point where all items in the list cost more money than you have (which will happen eventually if no perfect solution is found), ret will be set to an empty vector.

You should only set ret to minsol if minsol has been assigned to at least once (or you could initialize minsol to ret, which would have the same effect and would be somewhat analogous to initializing min to desired).

PS: In the future it would be nice if you explained in what way your program failed (i.e. "the part of the output on the left side of the equals sign is empty, but the right side is correct") instead of just saying that it failed. That way I wouldn't have to copy your code onto my machine, compile and run it just to see what the problem is.

sepp2k 378 Practically a Master Poster

When defining template functions or member functions of template classes, you can't put the definitions in a separate file from the declaration. Both the declaration and the definition need to be visible to the code that uses your functions. So you need to put the contents of Dynqueue.cpp into Dynqueue.h instead.

And yes, that's the complete opposite of what you're usually supposed to do, but that's how it is.

sepp2k 378 Practically a Master Poster
if (desired-options[i]>0.0)

You never check whether desired - options[i] == 0. You should push the item on the vector and return 0 in that case.

PS: Is there a specific reason that you're using 0.0 and not just 0? I realize you want the code to work with all types of numbers, but, unless I'm missing something, that would still be the case when using 0 and using 0 would avoid unnecessary conversions to double.

No NP-complete problems are not unsolveable... its just that they are unsolveable in POLYNOMIAL time

Unless P=NP.

sepp2k 378 Practically a Master Poster

It will not import both. When you import a module it searches through the available modules and imports the first one it finds with the given name. The order in which it goes through the modules is explained in this section of the docs:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

After initialization, Python programs can modify sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended. See section Standard Modules for more information.

So since sys is a built-in module, it will always be found before your sys.py.

sepp2k 378 Practically a Master Poster

so the if statement isn't required?

You only need the if statement if you want your code to act differently depending on whether it's imported or run as a program.

what is the __name__ by default?

If the Python file is run as a program (i.e. using python bla.py, a double click in the file manager (assuming you have set it up, so that that actually runs the program rather than opening it in an editor), using ./my_file.py in combination with chmod +x and a shebang line -- whichever), __name__ will be "__main__". If it is imported from another Python file or from the Python shell by writing import my_module, __name__ will be "my_module" where my_module is the name of your file without the .py extension.

I don't understand what you mean when you say "if the Python file is invoked directly" does that mean double clicking on a .py file

See above.

or can I run a .py file using python?

That too.

Basically running it directly refers to any means of running your Python code other than using it from other Python code.

I think import can be used to access functions from another .py file

Yes.

sepp2k 378 Practically a Master Poster

You can name your function anything you want, but the name has to be "__main__". That's not about the function being called. "__main__" is simply the value that __name__ gets if the Python file is invoked directly instead of being imported.

PS: Calling your function both inside and outside of the if means that your function will be called once if the file is imported and twice if the file is run directly. That doesn't seem like what you'd want.

sepp2k 378 Practically a Master Poster

A Python file can either be run as a program or it can be imported by another Python file as a library. if __name__ == "__main__": means "only run the following code, if this file was run as a program - not imported as a library".

This is useful if you want other program's to be able to import this program's functions without actually running the program.

sepp2k 378 Practically a Master Poster

in input specification it is mentioned both of array size will be equal to 'N' and nothing else.

That's enough, isn't it? So, just use i < N as your loop condition.

sepp2k 378 Practically a Master Poster

Maybe the question says something about the array ending with a specific number? Like the last element of the array might be 0 or -1? In that case you can find out the size of the array by going through the array until you encounter that number. Or possibly it says that the arrays will have a specific size. If the question doesn't mention anything like that, it's simply not solvable.

sepp2k 378 Practically a Master Poster

You get that error because you call with get_average with item as its argument, but item is a float and get_average expects to get a list, not a single float.

sepp2k 378 Practically a Master Poster

You came across what error? If you're getting an error message, please post it. If your code acts differently than you want it to, please explain what you expect and in what way it acts differently.

PS: Using return inside a for loop like you're doing, will likely not do what you want (though that does of course depend on what you want). return leaves the function immediately, so when you do it in a loop (without an if around it), that means you will exit the loop after the first element and never even look at the other elements.

PPS: On line 26 you misspelled return. You can tell because it's not highlighted as a keyword.

sepp2k 378 Practically a Master Poster

%c reads any character, including whitespace. So since you still have a space or newline in the buffer from the last scanf, it will just read that. If you use the format string " %c" with a space in front of the %c, it will discard all whitespace before it reads a character.

Oh and your initial error was because you had an else after an opening brace, but I suppose you've figured that out already.

sepp2k 378 Practically a Master Poster

It means "Text means a character array of size 80", yes. There's no such thing as an array of typedefs because typedefs are a compile-time concept only - they're not something that can be stored in arrays.

The syntax of a typedef is basically the same as that of a variable definition (except for the keyword typedef in front obviously). The name of the typedef goes where the variable name would usually go and the type that that variable would have is the type that's being aliased. So since char Text[80]; would define a variable named Text that stores a character array of size 80, typedef char Text[80]; defines a typedef named Text that describes character arrays of size 80.

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

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

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

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

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

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

The pearsonr function returns a tuple containing two values. The [0] selects the first of those two values.

sepp2k 378 Practically a Master Poster

I'm not sure I understand your question. The values for j and k will not affect the size of the resulting arrays. If a is an array of size n*m then a[ ..., j ] is an array of size n, no matter what the value of j is.

Or more generally: a[ ..., j ].shape == a.shape[ : -1 ] for all multi-dimensional arrays a and all valid indices j.

sepp2k 378 Practically a Master Poster

If a is an n-dimensional numpy array a[..., j] will return an (n-1)-dimensional numpy array where each innermost subarray is replaced by its jth element. So for example if a is 2-dimensional, a[..., j] will be a 1-dimensional array containing the jth column of each row.

Nothing in Python, looks like pseudo code.

Actually, ... is perfectly valid Python syntax - it's just not used anywhere except by numpy and scipy.

sepp2k 378 Practically a Master Poster

So in order to display data onto the screen, data must go into the RAM first?

Generally yes. But as I said that doesn't mean that the whole file must be in memory at once. That wouldn't even be possible in this case.

And if the RAM is out of space, then it stores temporary data onto the hard drive as swap files?

Yes, but again that's not what's happening in this case. Trying to read the whole hard drive into memory at once would crash the program - swap file or not.

Since it is storing data onto the hard drive, is there a chance that it will overwrite old data(such as deleted files)?

Yes, writing new things to a hard drive always has the chance of overwriting deleted files (but again note that you can't read the whole hard drive into memory at once and reading only parts of it at a time will likely not involve any swapping).

And when data is being stored from RAM as a swap file, is the data always stored in the same sector(s) on the hard drive, or is the address location dynamic as in it might not always be in the same location?

I'd assume it would be placed just like any other file. But I really don't know anything about how Windows handles swap files.

I'm trying to create a data recovery program and i'm worried that there will be …

krayzeeben commented: very informative +0
sepp2k 378 Practically a Master Poster

Depending on your operating system data will be moved from your RAM into a swap file or partition once your RAM is full.

That said there is no way that your hex editor reads your whole hard drive into memory before displaying it. Not only would reading such large amount of data take an awful lot of time, there's no way you could fit into all into memory - even with swap files (there is usually a maximum size for swap files and even if there weren't, it would be limited by available disk space).

As you said, only a small portion of the data is displayed at any given time, so it'd be easy for the editor to only read a small portion of the data and then load further portions (and possibly unload old portions) as you scroll down.

sepp2k 378 Practically a Master Poster

Okay, I've looked on your code more closely. On line 7 you've assigned a string to a double. This will convert the string to a double using your current locale. So on your system this causes the value of DS to become fifty thousand, not 50.

If you want to disregard locale settings, you should not be doing any implicit conversions from string to double or vice-versa. Always use explicit conversions, in which you explicitly specify how you want to format the numbers. If that's too annoying, you might want to consider changing the locale for your program (which I might have suggested before in this thread).

If you remove the quotes from line 7 and remove lines 23 and 24, your code should work (at least with regards to how the result of dbldkstring is formatted - InvoerStart and InvoerEind will still be converted using your current locale).

sepp2k 378 Practically a Master Poster

According to MSDN the last argument to FormatNumber tells it whether or not to use a thousands separator. If you leave out the argument (which you did), it uses the current locale's default. So if you don't want to change the current locale, you should pass TriState.False as the last argument.

Edit: Shouldn't the second argument to FormatNumber be an integer?

sepp2k 378 Practically a Master Poster

Again, use a locale that formats the numbers in the way you want them to be formatted.

Also: It shows the result as "3.000.000", not "3.000,000"? How did you get that to happen? You didn't just multiply the result by 1000, did you?

sepp2k 378 Practically a Master Poster

250.000 - 50.000 = 249950.

Does your system by any chance use a locale where . is used as a thousands separator and , as the decimal point? Because if so, the users should either input the numbers in a format compatible with the current locale (i.e. use , instead of .) or you should explicitly use a different locale when converting the string to a number.