arkoenig 340 Practically a Master Poster

@DonutsnCode: Line 1 of your function says it returns int, not int&. It might still appear to work if you change it, but there's nothing wrong with the code you posted.

arkoenig 340 Practically a Master Poster

The paragraph you cite is an example of explaining how to avoid a bad programming technique by using a second bad programming technique. Here's why I say that.

A reference is an alternative name for an object. For example, if I write

int i;
int& j = i;

then I am saying, in effect, that j is an alternative name for i; in other words, that i and j are two different ways of referring to the same object.

When you write a function that returns a reference, that function is returning an alternative name for an object somewhere, because that is what a reference is. For such a function to be meaningful, it must return a reference to an object that will continue to exist after the function has returned.

So, for example, if I write the following:

int& foo()
{
    int n;
    return n;    // Don't do this!!
}

I have written a function that behaves in an undefined way when I call it. The reason is that if I write

int& x = foo();

I am saying that I want x to be an alternative name for whatever foo returns. However, what foo returns is an alternative name to its local variable n, and that variable ceases to exist when foo returns. Therefore, it returns an alternative name for something that does not exist, and x is similarly caused to name a nonexistent object.

The bad programming technique here is …

arkoenig 340 Practically a Master Poster

>>For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX

Thats true. I never thought about that. But theoretically, cant we scale and shift the values to fall into range? But then that might affect the distribution probability.

Exactly. That's why I said it complicates the problem. We can't just scale the values if we want to maintain a uniforum distribution; we have to decide whether the value will fall into one of those tiny buckets and then perhaps use extra calls to rand() to fill in the gaps if needed.

It's a messy problem if you want to get it right.

arkoenig 340 Practically a Master Poster

@FirstPerson: That's a guess. The trouble is that on most C++ implementations, there are lots of floating-point numbers in the 0-1 range that your suggested function will never return. For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX, even though plenty of such values exist on most implementations.

Without know the OP's application, it hard to know whether this problem is a deal-breaker or irrelevant.

arkoenig 340 Practically a Master Poster

That's not really an answer. Do you mean that you want numbers that are exact multiples of 0.1? Or do you want to accept 0.15 as a possible answer? What kind of probability distribution do you want? For example, do you care whether you ever get very tiny numbers such as 1e-20? Because if you do want such numbers to be possible, the problem gets much harder.

arkoenig 340 Practically a Master Poster

What is the difference between decimal random numbers and other kinds? Putting it differently, exactly what are you trying to do?

arkoenig 340 Practically a Master Poster

if(test_command == true) should be if(test_command() == true) .

Yes. But there's another subtle point here that is worth mentioning.

It is generally a bad idea to write expressions of the form function() == true . It is harmless to do so if the function returns a bool value, as test_command does in this case; but it is not unusual for C++ functions to return integers with the intent that zero be taken as false and any other value be taken as true.

If you use such a function in an expression of the form function() == true , the effect will be to take the value 1 as true and anything else as false, which is probably not what you wanted.

In the context of an if or while, the best solution is to drop the comparison; if(function()) does exactly what you want. Alternatively, function() != false is safe, and means the same as function() != 0 .

Of course, in all cases, you need parentheses to call a function.

arkoenig 340 Practically a Master Poster

It's your course.

arkoenig 340 Practically a Master Poster

Can you prove it? Or did you just look for a solution for a while and then give up?

arkoenig 340 Practically a Master Poster

OK, now see whether you can figure out how to use these stacks to achieve each of the permutations in the exercise. I suggest you start with (3) because it's the easiest.

arkoenig 340 Practically a Master Poster

I guess that would be a good place to start, then. Because if you don't know what a stack permutation is, and someone else answers the question for you, you aren't doing your own homework.

arkoenig 340 Practically a Master Poster

OK... What do you know about stack permutations?

arkoenig 340 Practically a Master Poster

The proposed solutions all imply that the derived classes could be sorted/ordered the same way numbers can. However, in my case, the "ordering" does not have to follow such rules. For example, A<B<C, but a new class D can be considered to be "less" than A, but "greater" than C.

Then in what sense is it an ordering? And what rules does it have to follow?

arkoenig 340 Practically a Master Poster

The curly braces are also part of C++0x. That's part of why I suggested that maybe your compiler doesn't support it yet--the C++0x standard isn't even finished yet.

arkoenig 340 Practically a Master Poster

Your declaration of "Item" is lower case here, though the class itself is uppercase.

auto Item * AoE2Wide= new Item(Pos, rf, "", "");

Does this work? Or am I misunderstanding your intention?

It's a C++0x usage. It is a request to declare a variable named "item" of the same type as that returned by the expression "new Item(<arguments>)"

arkoenig 340 Practically a Master Poster

Maybe your compiler doesn't completely support C++0x initializations yet.

What happens if you replace the curly braces with parentheses in the last line of code in your example, and change "auto" to "AoE2Wide::Item*" ?

arkoenig 340 Practically a Master Poster

First off, absolute brute force isn't going to work in this case as the input is in floating point, and you can easily get a function that has maxima at non-integer values.

The original problem statement constrains the arguments to integer values.

StuXYZ commented: Well spotted! +4
arkoenig 340 Practically a Master Poster

The code is indented properly. Perhaps you could get to the root of the problem instead and mention that mixing tabs and spaces can affect indentation when pasted outside of an IDE.

Aha! Frankly, the thought had not occurred to me that the website (or, perhaps, the combination of the website and my browser) would display mixed-tabs-and-spaces code so confusingly.

Looking at the code again, I am guessing that it was written in an IDE that assumes 4-character tabs, and displayed by an engine that assumes 8-character tabs.

I stand by my remark about trivially redundant comments.

arkoenig 340 Practically a Master Poster

In my earlier remarks I made a number of suggestions, including:

1) Most of your comments are so obvious that they add nothing to the code.

2) Learn how to indent C++ code.

As far as I can tell, you have ignored these remarks. Why should I look for new problems if you haven't done anything about the ones I already found?

arkoenig 340 Practically a Master Poster

Since x, y are integers limited to the inteval [ -100, +100 ] there are just 200*200 == 40000 possible tuples (x,y). Just use brute force.

201 * 201 = 40401 possible tuples.

arkoenig 340 Practically a Master Poster

Memory-mapped IO is not part of standard C++, which means that your instructor must be expecting you to use some locally defined facilities for the purpose. Your first job is to find out what those facilities are.

While you're doing that, I have three other suggestions:

1) Most of your comments are so obvious that they add nothing to the code. For example:

// If statement to check if i > j
if (i>j)

// Show that y is true
y=true;

// Return y
return y;

These comments make your program bigger, and therefore increase the amount of time it takes to read it; but they do not make the program any easier to understand.

2) Learn how to indent C++ code. Line 73, which is the beginning of an if statement, is indented less than line 76, which is the rest of that if statement. You should never write code like that.

3) Don't define useless variables. For example, your variables y and palin are each used in only a single place: You assign a value to the variable, use it once, and then never use it again. So, for example, instead of writing

y=true;
return y;

you can simply write

return true;

and instead of writing

palin=isPalindrome(data);
if(palin) // ...

you can write

if (isPalindrome(data)) // ...

In both cases you can not only get rid of …

jonsca commented: Good advice +6
jimJohnson commented: Great helper +0
arkoenig 340 Practically a Master Poster

The variable "last" you use in line 7 isn't defined, nor is it given a value anywhere.

If your list has only a single element, then presumably after you remove it, you need to set head to 0. Nowhere in the code is it possible for that to happen.

arkoenig 340 Practically a Master Poster

But, in my case, the classes A, B and C don't contain values by which they could get sorted and that's the trick.

Of course they do! That's the point of the value function Narue suggested. You override that function in each of your derived classes to return a value that reflects the ordering you desire.

arkoenig 340 Practically a Master Poster

What does thid have to do with C++?

arkoenig 340 Practically a Master Poster

What is the question?

arkoenig 340 Practically a Master Poster

In a perfect world, it would. In practice, it is not uncommon for implementations to fail to catch integer overflow.

arkoenig 340 Practically a Master Poster

Yes.

More seriously, if you're running this program on a 32-bit machine, you're probably asking for much more memory than the system is capable of providing; and it would not be surprising to learn that an overflow is taking place as part of computing how much memory you need.

arkoenig 340 Practically a Master Poster

Can you point out one or two of the smaller mistakes that I am making Although VS2008 shows me 0 errors and 0 warning. I would be more thank ful

For example, the statement

while (in1.eof()!=1)

is wrong because the last time through the loop, your attempt to read a line will fail (because you had not reached eof before you started to read the line); but your code does nothing to detect the failure.

The use of in1.eof()!=1 instead of simply in1.eof() also suggests that you do not understand how C++ handles Boolean values.

As a more general comment, you should not be using char arrays for a program such as this one, because it guarantees problems if your files have lines in them that are longer than the number of elements in your arrays.

You define a string variable named temp, but never use it.

You define a label named skip, but never use it.

You call a function _flushall that is not part of standard C++.

Your comment "As the first element of line is taken as zero for unknown reasons..." strongly suggests that there are important aspects of your program that you do not understand.

I'm sure there are others. I will be happy to look more carefully at the program once I see evidence that you have looked at it carefully.

arkoenig 340 Practically a Master Poster

This code has lots of small errors that I'm going to leave it to you to find. I'll just point out the big error.

Lines 47-66 read all the contents of in2. When you're done, you've reached the end of in2. Now line 67 tests whether you've reached the end of in1.

Suppose you didn't reach the end of in1 yet. So now you go back to line 38, and eventually reach line 47 which checks whether you've reached the end of in2. But of course you did; otherwise you wouldn't have fallen out of the loop that ends on line 66.

So for each line you read from in1 beyond the first, you will never read anything new from in2 because you've read it all already.

As I said at the beginning, there are many other problems with this code. However, you won't even get to the point where you might have a chance at finding them unless you fix this big problem first.

arkoenig 340 Practically a Master Poster

Here are the three cases with examples:

  1. 123 and 456 have the same length, so you use the first mismatch (3 and 6). 123 and 123 have the same length but no mismatch, so 0 would be returned all the way back up the recursion chain. A more interesting example is 123 and 523. 3 and 2 match, which is why you need to do comparisons back up the chain.
  2. 352 is shorter than 4391, so you don't need to do any comparisons. The result is guaranteed to be 1.
  3. 1234 is longer than 543, so you don't need to do any comparisons. The result is guaranteed to be -1.

I think this case analysis fails to take into account the case where the two lists have different lengths, but one or both of them has a leading zero.

For example, 352 is shorter than 0321, but it's bigger anyway.

arkoenig 340 Practically a Master Poster

The recursive call to CheckSmaller (with the comment) will never be executed because either path of the if statement before it will return first. So I don't even need to read the rest of the code to see that it's wrong.

arkoenig 340 Practically a Master Poster

There is no way to determine where in a file each line begins without reading the entire file (unless you're creating the file, in which case you can remember the line locations as you write them--but that's the same kind of operation as reading it).

So I don't see any way around reading at least as much of the file as needed to determine the location of the highest-numbered line you've ever requested. The following strategy seems to make sense:

1) Plan to create a vector with one element for each line. Each vector element is a seek pointer that corresponds to the location of the beginning of the first character of the correspondingly numbered line. As a micro-optimization, you might omit the first line of the file, because that is known to start at the beginning of the file; but I wouldn't recommend it.

2) Suppose you now want to locate line n in the file. If the vector has at least n elements, all you need to do is seek to the point that corresponds to the beginning of that line.

3) Therefore, the only hard part of the problem is what happens if the vector has fewer than n lines. At that point, you need to seek to the position corresponding to the last element of the vector, and keep reading lines until either the vector is big enough or you reach end of file.

This strategy requires one seek pointer per …

arkoenig 340 Practically a Master Poster

A somewhat easier way to write

copy(d1.begin(), d1.end(), d0);
copy(d2.begin(),d2.end(), &d0[d1.size()]);
copy(d3.begin(),d3.end(), &d0[d1.size()+d2.size()]);

is as follows:

double* next = copy(d1.begin(), d1.end(), d0);
next = copy(d2.begin(), d2.end(), next);
next = copy(d3.begin(), d3.end(), next);

After this code, next points to the first available location in d0.

arkoenig 340 Practically a Master Poster

i am sorry i did not understand can you be still more clear about what you are talking or asking me about??

It's really simple. I asked you what would happen if you try to copy an object of your link_list class, and you said that it would call the copy constructor.

As far as I can tell,l you have not defined a copy constructor for link_list, so I asked you where the copy constructor is that you think it will call.

Do you mean that it will call the copy constructor that the compiler defines for you? If so, what do you think the effect of that compiler-defined copy constructor will be?

Or do you think there is a copy constructor that I have failed to see?

Hint: A member function named copy is not a copy constructor.

arkoenig 340 Practically a Master Poster

i think that would call the copy constructor of the link_list... hope i am correct..

What copy constructor?

arkoenig 340 Practically a Master Poster

What do you think happens when you execute the following?

{
link_list x;
x.insert(42);
link_list y = x;
}
arkoenig 340 Practically a Master Poster

You might use a map, with the strings you care about as keys and pointers to the relevant functions as values.

Either that or use an enum type as the value, with one member for each function you care about.

arkoenig 340 Practically a Master Poster

You can't do it directly, and it's hard to understand why you would want to so--because if you could, the behavior of your program would depend on the names you choose for its components.

arkoenig 340 Practically a Master Poster

Two points:

1) Why do you think your code was executed correctly? Is it not possible that it just happened to appear to be correct by coincidence? How would you go about proving that it was executed correctly even without a copy constructor.

2) If you have a copy constructor, you should probably also have an operator= member.

arkoenig 340 Practically a Master Poster

A random example of what?

arkoenig 340 Practically a Master Poster

No, I cannot show you an example. It's your program -- you need to figure out what you want the program to do. Sorry.

arkoenig 340 Practically a Master Poster

In line 13, you are passing the name of a class as an argument. You can't do that.

In the future, please do not make me look through your whole program for the line that generates the error message.

arkoenig 340 Practically a Master Poster

I have no idea what this statement means:

return callGetDataHP(BattleSituation&getHPData, id, pokeslot);

arkoenig 340 Practically a Master Poster

I don't know what to put in replace of getHPData.

It's your program. If you don't know what you want it to do, how can you expect anyone else to know?

arkoenig 340 Practically a Master Poster

I posted it. Line 18.

And I explained what your problem was, and you said "Ignore that, my mistake."

So is your problem solved now or not? If not, please answer my question about line 18 of your code:

return callGetDataHP(getHPData, id, pokeslot);

In this line, you are using a variable named getHPData. I do not see a declaration for that variable. I see from line 9 that class BattleSituation has a member named getHPData, but that doesn't matter here, because you're not using getHPData as a member of a class.

So as far as I can tell, the compiler is complaining that you used getHPData without declaring it. If you don't understand why that is happening, please show us where getHPData is declared.

arkoenig 340 Practically a Master Poster

You said that you're getting the error message

'getHPData' was not declared in this scope

and you don't understand why. Now that I've told you why, is your problem solved? If not, please post the code that's causing trouble now, along with the error message and the line number that the error message specifies, so that we don't have to hunt for it.

arkoenig 340 Practically a Master Poster

In line 18, what does getHPData represent?

arkoenig 340 Practically a Master Poster

I can't fix the problem. :s I don't get why it's happening.

Until you post the actual code that's causing the problem, I doubt anyone will try to help you.

arkoenig 340 Practically a Master Poster

Why don't you start by describing your ideas? Then other people will be more likely to contribute theirs.

arkoenig 340 Practically a Master Poster

i don't understand what you want me to post realy.

I want you to post a complete program that I can compile and run, and that illustrates the problem that you are having.