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

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

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

You call Time.sleep once before the loop. Inside the loop you keep making attacks without calling Time.sleep. For that reason your program will pause once before the loop. Inside the loop it will keep making attacks without pausing.

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

This is probably not the solution your instructors are looking for. I imagine your instructors are looking for a solution where you caclulate n!/k! without calculating n!.

Note that if you expand n!/k!, it will look like this:

1 * 2 * ... * n
--------------------------
1 * 2 * ... * n *  ... * k

See how the 1 * 2 * ... * n part repeats in both the numerator and the denominator? If you were to calculate this by hand, you wouldn't calculate that part, right? You'd just cancel it out and only calculate the part that doesn't repeat. I imagine your program is suppsed to do the same.

That will not only lead to a better running time, but also to less rounding errors for large n and k.

sepp2k 378 Practically a Master Poster

You're never actually compiling the code. You need to call make to create perl and the other binaries you're trying to copy.

sepp2k 378 Practically a Master Poster

On the end of that line, right before the &&, you have a closing parenthesis. There is no opening parenthesis corresponding to this closing parenthesis anywhere in your script. So you get a syntax error.

sepp2k 378 Practically a Master Poster

The executable created by lex does not take any command line arguments and if you give it any, it will silently ignore them. It reads its input from stdin and writes its output to stdout. So as L7Sqr said, it doesn't do anything because it's waiting for you to enter some input.

If you want to use your lexer on your myname.txt, you should pipe the contents of the file into your lexer using input redirection, i.e. ./a.out < myname.txt.

sepp2k 378 Practically a Master Poster

What does using str on the result of raw_input accomplish? It's already a string.

sepp2k 378 Practically a Master Poster

Floats in C are usually stored as 32-bit (i.e. single precision) IEEE floating point numbers.

sepp2k 378 Practically a Master Poster

You set j to 0 once before the outer loop starts. So when the inner loop is run for the first time, j goes from 0 to 15. On the second iteration of the outer loop, j will still be 15 because you never reset it to 0 after the first iteration. If you add j=0 to the inner loop, j will be reset to 0 at each iteration of the outer loop.

PS: The fact that you used macros here instead of typing the loop out, makes no difference.

I_m_rude commented: yoo! great! +0
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

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

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

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

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

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

Can the two features of C++ Function overloading and function templates be applied together ??

Yes. You can define multiple functions with the same name even if one or all of the overloads are template functions. When you do this, the non-templated overloads will be considered first and if none of them fit, the most specific of the templated overloads is chosen. As an example take this:

template<class T>
void f(T x); // 1

template<class T>
void f(T* x); // 2

void f(int x); // 3
void f(int* x); // 4

Now if you call f with an int as an argument, overload 3 is chosen. With an int pointer overload 4 is chosen. With any other kind of pointer overload 2 is chosen. And with any argument that is not an int and not a pointer, overload 1 is chosen.

Look into template specialization. I think that may be what you're referring too.

No, don't. It's perfectly possible to overload templated function definitions using regular overloading as described above. In the majority of cases this is preferable to using template specialization.

For more information see Why Not Specialize Function Templates.

mike_2000_17 commented: Could not have said it better! +13
sepp2k 378 Practically a Master Poster

It contains a list with a single record in it. That's because you added a condition to the generator. That's not what I did in my example.

In my example I had a simple generator without a condtion attached to it. I then used if-then-else in the body of the list comprehension to replace matching items and keep all other items unchanged. If you do the same thing, you'll get the result you want.

sepp2k 378 Practically a Master Poster

I'm not sure what you mean by that. Both using a list comprehension or fixing the code in your originial post will give you a whole list as the result, not a single record.

sepp2k 378 Practically a Master Poster

I can't reproduce the behavior you're describing.

Your code doesn't produce the output you say it does - your code doesn't even compile. If I correct the code's syntax error, the result will always be the empty list because you have overlapping cases with the less specific case coming first. If I rearrenge the order of your cases, I get the correct output and no entries are duplicated.

Regarding list comprehensions: In its simplest form a list comprehension has the form [expressionInvolving item | item <- items]. This will evaluate the expression expressionInvolving item for every item in the list items. For example you can use a list comprehension to replace each occurence of the string "foo" with "bar" in a list of strings like this:

strings = ["foo", "lala", "lulu", "foo", "baz"]
newstrings = [if string == "foo" then "bar" else "string" | string <- strings]

Now newstrings will be ["bar", "lala", "lulu", "bar", "baz"]. The same logic can be applied to solve your problem.

Note that in simple cases like this a single call to map can do the same thing as the list comprehension. Also in real code I would move the if-then bit in its own named function for readability.

sepp2k 378 Practically a Master Poster

a) In Java we usually speak of "object references", not "memory addresses". Memory addresses are a low-level concept that isn't really visible to Java programmers. But yes, when you pass m2 to the Mixer constructor, you're passing a reference to the object that m2 refers to. So the m1 variable of the newly creater Mixer object will refer to the same object as the variable m2.

b) m1 is an instance variable of the Mixer class. So each Mixer object has its own variable called m1. Writing someMixerObject.m1 will access the variable m1 that belongs to that Mixer object.

c) If a Mixer object is created using the default constructor, its m1 variable will be null (because the default constructor does not set m1). If it is created using the one-argument constructor its m1 variable will be the argument passed to that constructor.

So when you create m2 in your main method, its m1 variable is null because you used the default constructor. m3's m1 variables is equal to m2 because you used the 1-argument constructor with m2 as the argument. So when you do m4 = m3.m1, m4 will be equal to m2. And when you do m5 = m2.m1, m5 will be equal to null because, as already said, m2's m1 variable is null.

Since you're not allowed to call methods on null, you get an exception when you invoke m5.do().

JamesCherrill commented: Very clear and comprehensive reply +14
sepp2k 378 Practically a Master Poster

You're getting the parse error because you forgot a semicolon on line 2. Whenever you get "Unexpected blabla" at the beginning of a line, there's a good chance that you forgot a semicolon on the line before.

sepp2k 378 Practically a Master Poster

Your copy constructor calls your setFoo methods. Your setFoo methods use delete on the old value of the variable that they're setting. When the copy constructor is invoked, the variables won't have old values because the object is brand new. That means that your setFoo methods will call delete on uninitialized pointers. This makes your program crash.

The quick fix would be to set your pointers to NULL in the copy constructor before you invoke your setter methods (deleting a null pointer is safe). A better solution would be to get rid of your char pointers altogether and use std::string instead.

Lucaci Andrew commented: std::string is the way +5
sepp2k 378 Practically a Master Poster

when break() occurs, i assumed that it would be a clean break()

It is.

but clearly it isnt turning out so, and acc[i] is catching a junk value.

When break occurs it leaves the loop just as it should. But acc[i] simply isn't initialized. Let's play it through for ip_num = "12" and op_base = 10:

In the first iteration i is 0, ip_num is 12 and acc[0] is set to 2.
In the second iteration i is 1, ip_num is 2 and acc[1] is set to 1.
In the third iteration i is 2 and the loop end because ip_num is 0. Nothing is written to acc[2].

So after the loop i is 2 and acc[i] is a junk value because nothing has been written to acc[2].

sepp2k 378 Practically a Master Poster

You can use the str_replace function to replace each occurence of "." in $description with ".\n", i.e. to insert a newline (\n) after each period. If you want to display your string as HTML, you should use "<br>" instead of (or in addition to) "\n".

Synestic commented: Thanks +0
sepp2k 378 Practically a Master Poster

You're missing a semicolon at the end of line 40, so the parser is confused when it sees the variable at the beginning of line 41.

sepp2k 378 Practically a Master Poster

The problem is unrelated to the fact that test takes a variable number of arguments. The problem is that you're calling test as if it was a method of the VariableArguments1Test class, which it's not.

If you want to call a static method of another class, you'll need to specify the class when calling it (or use a static import). So you need to write VariableArguments1.test instead of just test.

sepp2k 378 Practically a Master Poster

Your code doesn't seem to match your description at all. You said the function should return a Rectangle, but the function in the code actually takes a rectangle (that it never uses) and returns an int.

Your function also calculates the area of the rectangle that the user entered which your description didn't say anything about. The calculation of the area should really happen in its own function.

PS: In C++ you don't need the struct keyword when declaring variables or parameters of a struct type.

TrustyTony commented: Observant, good suggestion to separate calculation and printing +12