sepp2k 378 Practically a Master Poster

The easiest solution would simply be to sort the strings by length (using Java's built-in sort method) and then simply take the first three (or last three depending on the order in which you sorted) strings from the sorted array.

That solution would be much shorter, simpler and easier to understand than yours. However it's O(n log n) whereas your solution is O(n) - also if this is Homework, using sort might count as cheating.

To simplify your solution, the first step would be to do it in one single loop, instead of having three separate methods that all look the same except for some adjustments to the looping logic. To do this you should have three variables, keeping track of the longest, second longest and third longest string respectively. Then for each string you simply check whether it's longer than the longest, second longest or third longest string and, if so, adjust the variables accordingly.

JamesCherrill commented: Excellent reply +14
sepp2k 378 Practically a Master Poster

thought can't test it right? cause the names.dat doesnt exist....

You can just create one. Open your favorite text editor, enter a bunch of names, save as names.dat in the directory where you run your python script from.

sepp2k 378 Practically a Master Poster

The System.Console.GetOpt module (which is part of base and thus doesn't add any dependencies to your project) is the proper way to handle command line arguments.

Yes, it's easy enough to write your own code for handling command line options, but there's no need to. There's no point in reinventing the wheel when it's not necessary.

sepp2k 378 Practically a Master Poster

Scheme does have an if expression. It's used as (if condition then-expression else-expression) and works in the obvious way (i.e. it evaluates then-expression if the condition is true and else-expression if its not - the result of evaluating an if expression is the result of whichever of then-expression or else-expression was evaluated).

The difference between cond and if is that cond can take an arbitrary number of conditions and associated expressions, while if takes only one condition. cond also allows you to specify more than one expression for a given condition (which will be evaluated in order) - if you wanted to evaluate more than one expression with if, you'd need to use begin inside the if. So cond is basically a more versatile form of if.

The ways to do loops in Scheme are:

  1. Recursive functions¹
  2. The do form, which lets you specify a list of variables, that each have a starting value and an expression to calculate its new value, and a body. At the beginning of the loop each variable will be set to its starting value. While the condition evaluates to true, the body will be evaluated and after each iteration each variable will be set to the result of evaluating its respective expression. So basically it's a while-loop that allows you to specify how variables are updated declaritively.
  3. The named let construct, which basically works like defining a local recursive function and immediately calling it.

For more information see the Scheme standard's …

sepp2k 378 Practically a Master Poster

(x:y:a:s:d) (and the other expressions like it) will cause a type error because the right operand to : needs to be a list and here it's not. The proper way to achieve what you're trying do would be to use pattern matching like this:

compact (D:C:C:C:C:xs) = [C,M] ++ compact xs
sepp2k 378 Practically a Master Poster

What's your question?

sepp2k 378 Practically a Master Poster

Hint: Open the file and read every string stored on it. Each time you read a string, increment a counter variable. When you've read all the strings from the file, the counter variable will contain the number of names stored in the file.

That sounds about right. So why don't you go ahead and do that then?

sepp2k 378 Practically a Master Poster

Because most C implementations don't perform bounds checking when accessing an array. So when you give an index to an array, that index (multiplied by the size of the element type according to the rules of pointer arithmetic) will simply be added to the address where the array begins and the item at the resulting address will be accessed. If that address is one that you're allowed to access, it will give you a value. If it isn't, you'll get a segmentation fault.

That said accessing an array out of bounds is undefined behavior and anything could happen.

Also note that if arr is actually a pointer inside another array (e.g. int base_arr[] = {1,2,3,4,5}; int *arr = base_arr+2;), -2 might very well be a valid index. In that case arr[-2] will have a fully defined value (in the example I gave it would be 1).

sepp2k 378 Practically a Master Poster

Can your mac/linux do this?

Yes, of course, it can.

It should also be noted, that in that video the user manually resizes the VLC window on which he wants to focus. In the Mac video to which this was a response, the windows was focused and restored to its actual size by clicking on it, not by manually resizing it (also all other windows where restored to their old positions). So usability-wise the Mac version is clearly superior here (and I'm saying that as someone who has no particular love for Mac).

sepp2k 378 Practically a Master Poster

None of the variables in your loop ever change, so you get the same output on each iteration of your loop. For example you set rembalance to balance - principle. Since balance and principle are never changed, balance - principle will evaluate to the same value every time, so rembalance will have the same value every time.

If you want to output different values at each iteration of the loop, at least one of the variables used in the loop needs to be changed inside the loop. Presumably that variable should be balance.

sepp2k 378 Practically a Master Poster

they have given only upto 3*3 matrix

They also have a section on n*n matrices right after the one about 3*3.

sepp2k 378 Practically a Master Poster

Does the explanation on Wikipedia help you?

If not, could you maybe rephrase your question to be more specific?

sepp2k 378 Practically a Master Poster

If you want a given portion of a file to only execute when the file is run directly and not when the file is imported, you can put the code in an if __name__ == "__main__": block. That condition is true if the file is run directly, and false if it's imported from somewhere else.

Of course moving the code into another file works too.

sepp2k 378 Practically a Master Poster

"In file included from ..." is not the error. It tells you where the error occurs. The actual error message comes after that.

Anyway an std::map is a sorted map and thus needs its keys to be comparable to each other. So if you want to use a given type as the key of a std::map, the type either needs to have an operator< defined or you need to supply a custom comparison function (or functor) when creating the map.

sepp2k 378 Practically a Master Poster

You could check whether the string is empty and in that case just return 0 (or whatever number you want the empty string to represent).

sepp2k 378 Practically a Master Poster

The error message is telling you that you're trying to convert an empty string into a number. So are you sure that your 'money.$' file isn't empty?

sepp2k 378 Practically a Master Poster

If your path doesn't start with a drive letter or a (back- or forward-)slash, it's interpreted relative to the current directory. So Chophouse is right, you need to make your path absolute unless Users is a subdirectory of your current directory.

You also need to escape your backslashes or use forward-slashes instead. Otherwise \U is interpreted as the beginning of a unicode escape sequence, which is why you got a unicode error.

sepp2k 378 Practically a Master Poster

According to my compiler the following things are wrong with your code:

bla.c: In function 'main':
bla.c:5:5: error: array size missing in 'SqareNumArray'
bla.c:9:1: error: 'SquareNumArray' undeclared (first use in this function)
bla.c:9:1: note: each undeclared identifier is reported only once for each function it appears in
bla.c:9:1: warning: implicit declaration of function 'sqrt' [-Wimplicit-function-declaration]
bla.c:9:19: warning: incompatible implicit declaration of built-in function 'sqrt' [enabled by default]
bla.c:5:5: warning: unused variable 'SqareNumArray' [-Wunused-variable]
bla.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]

Though I have to say: you could have easily found that out by trying to compile the code yourself.

As far as semantics are concerned another thing that might be wrong is that you're casting the result of sqrt to int, so the result won't be the actual square root. That may or may not be intentional though. You'll know that better than I do.

Style-wise it's wrong that you hardcoded the number 10 as the upper bound for the for-loop. Magic numbers are bad.

sepp2k 378 Practically a Master Poster

There is no string literal (T_STRING) on line 11 in the file you posted. Are you sure the file you posted is the one the error messsage is complaining about?

sepp2k 378 Practically a Master Poster

That really depends entirely on the context.

sepp2k 378 Practically a Master Poster

First of all a isn't a pointer, it's an array. It decays to a pointer in certain contexts, but it decays to a pointer to an int-array, not a pointer to an int, so you need to cast it if you want to use it as an int-pointer.

And yes, a+1 would give you a pointer to the second row of a. However you're not doing a+1, you're doing (int*)a + 1. So you cast a to a pointer to an int, not an array. So incrementing it by 1 will cause it to point to the next int, not to the next row. Basically a is used as if it was a flat array instead of an array of arrays on line 2. So the pointers in p point to 1, 2 and 3 respectively.

sepp2k 378 Practically a Master Poster

I think you might get more help if you described your recursive algorithm instead of requiring us to discern it from your uncommented code.

Anyway I think the standard dynamic programming solution for the coin change problem can be adjusted to work for this variation of the problem.

sepp2k 378 Practically a Master Poster

I only want it to have three elements in it.

Then put only three elements in it. That doesn't seem very hard to do. If you change your loop to

for(var i=1; i < b.length; i++) {
     b[i]= i;
}

It won't add any additional elements to it. Using b.length as the boundary instead of a magic number is much better code anyway because it's easier to understand and prevents many kinds of mistakes.

sepp2k 378 Practically a Master Poster

You left out the part of other.py where you import main, but if you didn't use the from main import ... form of import, you'll have to refer to main's variables, functions and classes as main.app etc. instead of just app.

sepp2k 378 Practically a Master Poster

I'm just showing that allocation and initialization can be two seperate steps.

Yes, if you use placement new, but the OP already knows that. He already mentioned it in the original post (and I emphasized in once or twice in my responses as well).

sepp2k 378 Practically a Master Poster

Which of course is talking about the functions defined in that library and makes no claim about that function being available as part of Javascript itself.

sepp2k 378 Practically a Master Poster

Your code works because it doesn't contain any virtual functions, so no vtable is required. It's still undefined behavior though (the version without placement new, I mean).

sepp2k 378 Practically a Master Poster

When you create the array it does only have 3 elements in it. That doesn't mean you can't add more to it.

sepp2k 378 Practically a Master Poster

it is a function according to the javascript documentation.

According to which documentation?

sepp2k 378 Practically a Master Poster

The only times that it is valid to not do an initialization (which really means that you do the initialization sometimes later) is when the type is a POD (Plain-Old-Data type), in every other case, it wouldn't be "correct" code not to initialize it almost immediately.

It's always valid to allocate memory at one point and then initialize it using placement new much later in the program as long as you don't use the memory in the meantime. And there are cases where this is indeed necessary. For example there's no way to correctly implement std::vector without doing this (at least I can't think of one).

sepp2k 378 Practically a Master Poster

One mistake I immediately saw (though it's probably not the only one and might not be what's causing your crash) is that you're returning x[i][j] on line 91, but i and j are equal to Max and MAX (horrible names by the way) after the loop, so that's an out of bounds access.

Beyond that try running your program through a debugger and/or valgrind to find out where a crashes, what values the variables have when it does and whether or not those values are what you expected, and where and when you might be accessing memory incorrectly.

sepp2k 378 Practically a Master Poster

I don't see a valid reason why memory allocation and object construction should be tied together like that.

In the vast majority of cases it's more convenient (and safer) to do both at once. For the cases where it's not, there's placement new.

if they continue down that path we'll have another java in a few years.

You're talking like new is somehow a new addition to C++. new's been around as long as C++'s been around. It's not a step in C++'s journey to become Java. It predates Java.

sepp2k 378 Practically a Master Poster

The error on line 21 is that you forgot the () to call the function. So you're trying to assign a function to a double variable.

The error on line 105 is that the argument to scanf needs to be a pointer, not a double. Also you're using openfilewrite uninitialized, so it's UB.

PS: The last three of your functions don't contain a return statement even though they're declared to have a non-void return value.

sepp2k 378 Practically a Master Poster

I think the way new works is horrible

How so?

sepp2k 378 Practically a Master Poster

An unchecked exception is one that inherits (directly or indirectly) from RuntimeException. A checked exception is one that doesn't.

sepp2k 378 Practically a Master Poster

If your method calls a method that can throw an IOException (like ImageIO.read does), you either need to catch the exception or declare that your method may throw an IOException too (which you do by adding throws IOException to your method's signature).

You have to do this because IOException is a checked exception and the Java compiler ensures that each method may only throw those checked exceptions that it's been declared to throw using the throws declaration.

The idea behind this is that you're always aware if there's a possible error condition that you do not handle.

And yes, there are other methods in other classes that throw checked exceptions, too. Pretty much all IO-related methods can throw IOExceptions. And there of course are other checked exceptions as well that need to be handled the same way.

sepp2k 378 Practically a Master Poster

Is there any way to make A work without using new (that includes placement new)?

No, non-POD classes can not be allocated dynamically without using new. As far as the standard is concerned, it's undefined behavior to not use new.

In practice the problem is, as you suspected, the vtable. malloc returns uninitialized memory, so the object's vtable pointer will point somewhere random instead of to shazbot's vtable.

May I ask why you want/need to avoid using new?

PS: You shouldn't use explicit casts to perform an upcast. That makes it harder to see when you perform a cast that's actually unsafe. Also you should prefer C++ style casts over C-style casts in the cases where you do need an explicit cast.

sepp2k 378 Practically a Master Poster

None of those really seem necessary (or helpful) and some seem like incredibly bad ideas.

Your out and in macros, will make your code look very strange. If you want to print an item it will look like out foo, which really doesn't make sense syntactically. out(foo) or out << foo would make sense, but out foo doesn't look like C++ at all. And if you want to print two items, it would look like out foo << bar, which just looks totally incosistent.

Your first loop macro are pretty pointless with x not being a macro parameter. Having to insert the value you want by hand kind of defeats the purpose of having a macro (and even if x were a parameter, that's still not really something you need a macro for).

Your second loop macro is just completely pointless. If x is zero or positive, the loop won't run at all. And if x is negative, the loop will run forever (or more correctly it will invoke undefined behavior (signed integer overflow) that might result in an infinite loop - or not). Either way it's not useful.

sepp2k 378 Practically a Master Poster

Do we need to pay fee to Oracle if we sell our game on the market or host it on our own web site with advertisement on the game page?

No.

Also can we sell software written in Java?

Yes.

sepp2k 378 Practically a Master Poster

Actually for me the decryption seemed to work fine-ish (the third.txt file contained the original file's contents repeated multiple times). However after finishing decrypting, it goes back to main and causes a segmentation fault on line 90.

This happens because you mix getline and operator>>. After decrypts uses cin >> to read n and c, there will still be a newline character in the stream. Then when main calls getline, getline gives you an empty line because the first thing it sees is that newline character. So then you put that empty line in a string stream and populate vstring from that. So vstring will be empty. And when you try to access vstring[0] on line 90, you get a segmentation fault.

sepp2k 378 Practically a Master Poster

Have you tried to run your code in the debugger to find out which line the segmentation fault happens on? Have you tried to run your code through valgrind to find out when and where you write to or read from invalid memory?

Also can you give us some sample input we can enter into your program to reproduce your error?

sepp2k 378 Practically a Master Poster

When you call a method, you don't spell out the type of the arguments - you only do that when declaring the method. I.e. if you want to call drawHexagon with width as its argument, you write drawHexagon(width) not drawHexagon(int width).

That said, unless the SimpleTurtle class has a static variable called width, the width variable isn't defined anywhere in your program. So you need to define it and set it to a sensible value before you can use it.

Also you're missing a semicolon on line 105 (that part of the error message was pretty clear, I think).

sepp2k 378 Practically a Master Poster

There's nothing in your code that would reset the value of plus. However with your constructors the way they are, there is no way to create a tuna object that has the numbers as well plus set to a meaningful value.

So I'm guessing when you use your code, you're actually creating two tuna objects: one with the correct value for plus and one with the correct values for num1 and num2. Since each object has its own instance variables, one object's value for plus won't affect the behavior of the other object.

sepp2k 378 Practically a Master Poster

That's because your code in printScore doesn't check whether the user entered 'q'. Only your code in main did. If you handle the user input in printScore, you need to move the code that handles the user entering 'q' in printScore as well.

sepp2k 378 Practically a Master Poster

There is nothing you can do to make newtype f = 1 work. Java doesn't allow implicit conversions for non-primitive types. There's also no way to make arithmetic operators like + etc. work with your newtype class.

Using a user-defined number class will always look different than using primitive numbers in Java - you can't avoid that. That's why code using the BigInteger class is so ugly.

sepp2k 378 Practically a Master Poster

No, it just means that humanChoice will return even if the user enters invalid input. Qutting with 'q' shoudl work fine (at least I can't see why it wouldn't at the moment).

sepp2k 378 Practically a Master Poster

You're asking the player for his choice (by calling humanChoice) twice: Once in main and once in printScore.

PS: On line 24 you will always break because the condition choice == 'Q' || 'q' is always true (because 'q' is true).

sepp2k 378 Practically a Master Poster

Bowls made by the Mayans and by the Sumerians still work today (if found intact). [..] are they better than the bowls actually made today?

They're certainly a lot more expensive, so I can only assume they must be better.

DeanMSands3 commented: That's the best retort I've seen on any DaniWeb forum. +4
sepp2k 378 Practically a Master Poster

The body of a function definition needs to be indented.

The "then" keyword does not exist in Python and the "else" keyword must be indented at the same level as the corresponding "if". The body of the else clause must also be indented, just like the body of the if.

You're also missing colons everywhere.

sepp2k 378 Practically a Master Poster

Which part of that assignment are you having trouble with?