arkoenig 340 Practically a Master Poster

If you use the known representation of double numbers, your program will work only on machines that use that representation. If you use the binary-search technique I suggested, it will run anywhere.

Admittedly the binary search is slower. What is it about this particular operation that you need to be able to do quickly?

arkoenig 340 Practically a Master Poster

Lines 35 and 36 are wrong.

I think you think that they allocate an array with numCourses elements and copy the contents of the array to which courseList points into that array. They don't; what they do is put in this->courseList a pointer to a newly allocated array, then throw that pointer away and make this->courseList point to the same array as the one to which courseList points.

I am guessing you really wanted to do something like this:

this->courseList = new string[numCourses];
std::copy(this->courseList, this->courseList+numCourses, numCourses);

the second line of which, by the way, is an easier means of accomplishing what you did in lines 65-68.

Also, line 56 doesn't do anything, so it's probably not what you intended.

There may well be other errors in the code, but I don't see much point in looking for them until you fix these and try it again.

arkoenig 340 Practically a Master Poster

I am sorry to say that this entire long message is based on a fundamental misconception.

The name of an array is not a pointer. Its type is an array type, not a pointer type. Array types and pointer types are different from each other.

You can see this difference in several contexts. For example:

int x[10];
std::cout << sizeof(x) << std::endl;

If x were a pointer, then sizeof(x) would be the size of a pointer; but if you try running this code, you will see that it isn't.

As another example, consider

int arr[5];
int *const *const p = &arr;

Again, if arr were a pointer, this code would compile, but it doesn't.

What is true is that if you use the name of an array in most contexts (the main exceptions being to bind a reference to it or to use it as an operand of unary & or sizeof), it is automatically converted to a pointer to its initial element.

arkoenig 340 Practically a Master Poster

In order for Map to work correctly, you have to give it a comparison function that acts like <, in the sense that it is nonreflexive, antisymmetric, and transitive.

Your vectComp::operator() does not work that way. I do not need to read much of the code to determine this, because of the statement

if (v1.size() != v2.size()) return true;

You are saying that your comparison function returns true for any two vectors of different sizes, which means that it is symmetric. In other words, if the function returns true for two vectors v1 and v2, it will also return true for v2 and v1. So it is symmetric instead of antisymmetric, and that won't work.

Also, I note in passing that the comparison function passes its arguments by value, which means that it copies the entire vectors every time you compare them. That's a substantial waste of time.

I suggest you look up the built-in function named lexicographical_compare and use that as the basis for your vector comparison.

arkoenig 340 Practically a Master Poster

Given a number x, you want to find epsilon with the following two properties:

1) x + epsilon != x

2) If 0 <= y < epsilon, x + y == x.

It should not be terribly hard to find the value of epsilon by binary search.

arkoenig 340 Practically a Master Poster

The code you posted has at least three problems.

1) I still don't understand the point of the "endl" statement. It doesn't actually do anything.

2) Passing hi+1 in the recursive call cannot be right, because it guarantees that you will attempt to access data beyond the end of your string.

3) Even after you fix the problem in (2), if you call reallyReverse( /*anything*/, 1, 2), the program never terminates.

I don't think it would be fair to tell you what to change to fix these problems, but at least you know where to look.

arkoenig 340 Practically a Master Poster

I'm sorry if I've offended you, but this discussion group doesn't do other people's homework for them.

arkoenig 340 Practically a Master Poster

Yes it is possible to have an array as a class member. However, in all arrays in C++, the number of elements must be a compile-time constant. In the code you cited, you tried to define an array with i elements, where i is a variable that changes at run time. C++ doesn't allow this.

I suggest you use std::vector<Room> instead.

arkoenig 340 Practically a Master Poster

Yes: Look on line 148 of the file c:\program files\microsoft visual studio .net 2003\vc7\atlmfc\include\atlcomcli.h, and there you will find a variable named p, probably a pointer, that is zero when it shouldn't be.

Now figure out how that variable got that way and fix it. :-)

arkoenig 340 Practically a Master Poster

That is because myk45 gave you pseudocode instead of C++. If you do not know how to translate what myk45 wrote into C++, then you are probably in serious trouble, and you should consider dropping the course.

Nick Evan commented: Agreed +16
arkoenig 340 Practically a Master Poster

How about starting by writing down a clear description of what the function is supposed to do? In particular: Does the first character affected have index lo, lo-1, or lo+1? Does the last character have index hi, hi-1, or hi+1?

arkoenig 340 Practically a Master Poster

If you think that lo+1 is the same as lo++, you're wrong. To see for yourself, try running the following program:

#include <iostream>

int main()
{
    int m = 42, n = 42;

    std::cout << m++ << " " << n+1 << std::endl;
}
arkoenig 340 Practically a Master Poster

But I don't think the reallyReverse(a,lo++, hi--) is meaningless because hi starts of with the last character in the array, and lo starts of with the 2nd character in the array. the content array index "hi" is swaped with the content of array index "lo". I increment the index, which is lo++ then I call the function then swap again, and i do the same for hi--. Maybe this should give you a better understanding of what I'm trying to accomplish.

I understand it. The trouble is that you're wrong. I wish I had a less harsh way of putting it.

Here is why it's wrong. Consider the following very simple function:

void g(int);

void f(int n)
{
    g(n++);
}

The use of n++ in the call to g is a mistake, plain and simple, because calling g(n++) has the same effect of calling g(n) except that as a side effect, it increments n and then throws it away without doing anything with it.

Now let's change the code slightly:

void g(int);

void f(int n)
{
    if (n == 0)
        g(n++);
}

Here, the use of ++ in the call to g(n++) is a mistake for exactly the same reason that it was a mistake in the first example.

Finally, let's change the code again to make it recursive:

void f(int n)
{
    if (n == 0)
        f(n++);
}

Here, we have changed the call to g into a recursive call to f. This …

arkoenig 340 Practically a Master Poster

reallyReverse(a, lo++, hi--) is meaningless for the same reason I already cited.

I'm thinking that maybe it would be a good idea for you to spend some more time understanding the basics of how C++ expressions work before you tackle recursion.

arkoenig 340 Practically a Master Poster

What is the purpose of line 6? It makes no sense.

For that matter, the call to reallyReverse in line 13 also makes no sense, because the value of lo++ (and, similarly, hi++) is the same as the value of lo. Of course, it has the side effect of incrementing lo (and hi), but since you don't use those variables again, the call to reallyReverse has exactly the same effect as if you had written

reallyReverse(a,lo,hi);

so I get suspicious: Why didn't you just do that instead? It must be that you think the uses of ++ make a difference, and they don't in this case.

arkoenig 340 Practically a Master Poster

As I said, I did not look at the contents of the code. You said there was a link error, so I cut and pasted the entire code, compiled and linked it, and found only the problems I cited.

arkoenig 340 Practically a Master Poster

Apparently someone has already done part of your homework for you.

This list doesn't usually do people's homework for them. Why not ask the person who did the first part to do the rest of it?

arkoenig 340 Practically a Master Poster

From a compilation viewpoint, I see nothing that should prevent this code from linking. I do see a missing # in line 1, which I assume you removed inadvertently, and a type error in line 75; but when those errors are corrected, the program compiles and links over here.

I did not check for run-time problems.

arkoenig 340 Practically a Master Poster

Yes. In fact, I'm not sure that this declaration is valid at all, because it doesn't declare any variables. The general form of a typedef declaration is

typedef <type> <declarator-list>;

and in this case, the type is

struct D3DVEFCTOR { float x; float y; float z; }

and the declarator-list is empty.

If you omit the "typedef," the declaration is valid in C, but then to use it you would have to write

struct D3DVECTOR foo;

and you sould not need the "struct" in C++.

arkoenig 340 Practically a Master Poster

Perhaps because the author wants both C and C++ programmers to be able to use the same code, and to be able to write

D3DVECTOR foo;

with the same meaning in both languages.

arkoenig 340 Practically a Master Poster

It's a tag followed by one or more declarators.

A tag, in turn, is the identifier that follows "struct," "class," or "union."

arkoenig 340 Practically a Master Poster

My point is that this statement

cout << "a = " << a << endl;

prints the value of a , which is a variable of type int.

That means that it is impossible for this statement to print anything with a decimal point in it. So if you tell me that this statement will print 28.8, I know you're wrong, and I don't even have to look at the rest of the program.

arkoenig 340 Practically a Master Poster

How can you print an int variable and get a value that is not an integer?

arkoenig 340 Practically a Master Poster

You have to decide what you want the data variables "driver" and "owner" to mean. Right now you are using them as if they are pointers to objects that exist somewhere (because when you write *driver = something, that is what you are doing), but you never create those objects, nor do you make your "driver" variable point to any of them.

So before you do anything else, you have to decide how the class is supposed to work.

arkoenig 340 Practically a Master Poster

I already told you what is broken, when I said:

The Car constructor does not initialize driver or owner, but the set_driver and set_owner functions assume that driver or owner (respectively) have been initialized.

That means that if you execute

Car c;
c.set_driver(a_person);

the effect will be undefined. And I do not see any way to solve this problem without changing the defintion of the Car class.

You responded by saying that you were not allowed to change the definition of the Car class.

Therefore, I do not see how to solve your problem. I doubt a solution exists.

arkoenig 340 Practically a Master Poster

see O(log n) is the algorithm in whiich for every growth of n(the input size) the problem size gets reduced to log n
the base of log is 2 and not 10

If you think the base of the logarithm matters in O(log n), you do not understand big-O notation.

arkoenig 340 Practically a Master Poster

No, because the pointers don't point anywhere legitimate. The Car class as you've posted it is broken. You need to fix it before it will work.

arkoenig 340 Practically a Master Poster

Well, unless there's more to the Car class than you've shown us, I don't see how it can be made to work.

arkoenig 340 Practically a Master Poster

Your definition of class Car is definitely wrong.

The Car constructor does not initialize driver or owner, but the set_driver and set_owner functions assume that driver or owner (respectively) have been initialized.

I am guessing that line 5 of Car should be

driver = p;

and line 8 should be

owner = p;

Also, the & should be removed from lines 11 and 12, and you will need to define an operator<< for class Person. Either that or change lines 11 and 12 to use the print function that you defined.

arkoenig 340 Practically a Master Poster

Sorry; you're right. Log(n) grows more slowly than sqrt(n), so (n^2)log n grows more slowly than n. So I think I should have said that O(n+(n^2)log n)=O(n).

Geez, today isn't my day.

(n^2)log n grows more quickly than n^2, of course. What was I thinking when I implied that n^2 times sqrt(n) is n?

Nonetheless, my original comment stands: If you really want to anaylze the asymptotic behavior of an algorithm, expressing it in big-O terms that can be further simplified suggests that you didn't finish the job.

So let me finish it. Log(n) increases monotonically as n increases, which means that (n^2)log n grows more quickly than n^2. So the "n+" term in O(n+(n^2)log n) is definitely unnecessary.

So is O((n^2)log n) equal to O(n^2)? No, because if you pick a positive constant k,
(n^2)log n > k*n^2 whenever n>exp(k).

So this informal proof shows that O(n+(n^2)log n)=O((n^2)log n), unless I've screwed it up again :-)

arkoenig 340 Practically a Master Poster

Don't throw O(n+(n^2)log n)=O((n^2)log n) in the air, you need to prove it, which is the reason why I left it as it is.

Sorry; you're right. Log(n) grows more slowly than sqrt(n), so (n^2)log n grows more slowly than n. So I think I should have said that O(n+(n^2)log n)=O(n).

arkoenig 340 Practically a Master Poster

O(f(x)+g(x))=O(g(x)) whenever f(x) grows more slowly than g(x) as x increases without bound. Therefore, O(x+x^3)=O(x^3), and O(n+(n^2)log n)=O((n^2)log n).

If you don't understand this, it means you need to learn about big-O notation in more detail.

arkoenig 340 Practically a Master Poster

Putting it differently: If you have to ask about how to use operator new, you shouldn't be using it.

StuXYZ commented: Very true. +3
arkoenig 340 Practically a Master Poster

Your statement

temptr->next = temptr;

should probably be

temptr = temptr->next;

There may be other problems, but this is a good place to start.

That said, I would like to suggest that you learn how to use code tags properly.

arkoenig 340 Practically a Master Poster

Yes -- the first one is well-formed C++ (if X is a type and x isn't already defined in the same scope) and the second isn't.

arkoenig 340 Practically a Master Poster

If n is a non-negative integer:

The last digit of n is n%10.

If n < 10, the first digit of n is n.

If n >= 10, the first digit of n is the first digit of n/10.

This is all you need to know in order to solve the problem.

arkoenig 340 Practically a Master Poster

That's one problem; there's another. When you're testing a new number, how does the variable 'sum' get back to zero?

arkoenig 340 Practically a Master Poster

Sorry, it's not clearer. In particular, I do not know what a "repeated sequence" is.

For example, how many repeated sequences are there in

1, 1, 1, 1, 1, 1, 1, 1, 1

? How does your answer follow from your definition?

arkoenig 340 Practically a Master Poster

I don't have access to the actual Standard or the Draft for 0x Standard, but according to others here, that is acceptable under the forthcoming 0x Standard.

Under the old Standard(s) though, you are correct, it isn't technically legal.

I can't find it in my copy of the draft for the new standard either. Unless I'm missing something, that draft says that the number of elements in an array must be a constant expression.

arkoenig 340 Practically a Master Poster

Line 23:

double values[quantity];

is not permitted in standard C++ unless quantity is known at compile time. Your compiler may permit it as an extension; but if you want to rely on that extension, you should be aware that what you're writing is not really a C++ program.

arkoenig 340 Practically a Master Poster

I think it would be a good idea to define the problem precisely. Are you looking for every sequence that repeats? Including sequences of a single element? Must the repetitions be consecutive, or can other elements separate them? Can the repetition overlap the original? Are you looking for every sequence that repeats, even if two such repetitions overlap each other?


Until the problem is defined precisely enough that these questions and others like them are easily answered, it is difficult to proceed.

arkoenig 340 Practically a Master Poster

You might start by changing line 38 to say

Pizza* large = new Pizza();
large->setType("pan");
large->setSize("large");
delete large;

arkoenig 340 Practically a Master Poster

But there is no need for him to pass it by reference. I'm not saying you cant but asking why you would want to.

I have no idea :) Perhaps part of the original problem was to learn something about references.

I was just answering the question that was asked.

arkoenig 340 Practically a Master Poster

Here's the first place I'd look.

In line 3, you define a pointer variable named Selected, but you don't initialize it. In lines 9 and 13, you test two different conditions and, depending on the results of those tests, initialize Selected or don't.

So if both of those tests fail, Selected is never initialized.

Yet in line 20 you use *Selected as a function argument. If Selected is never initialized, your program will crash.

The other place I'd look is to verify that Player1Pieces and Player2Pieces, about which you've said nothing, have correct values.

arkoenig 340 Practically a Master Poster

I'm going to disagree with both gerard4143 and NathanOliver here. You can pass an array by reference as follows:

1) Change line 14 to the following:

int readFile(int sum, int &count, int (&grades) [10])

In case you're wondering what I changed, I moved one parenthesis.

2) Change line 107 to the following:

sum = readfile(sum, count, grades);

There may be other errors, but with a little luck, these suggestions will get you started in the right direction.

arkoenig 340 Practically a Master Poster

As in--

http://www.cplusplus.com/reference/stl/vector/vector/

-- nowhere in his code did I see construction of a vector with allocated space to set the size param. Nowhere for that particular vector was a push_back called.

It's called on line 16 of the original code.

Anyway... I agree with stuXYZ's comment that there is a serious problem in the Population structure. If you have a destructor that releases a resource, you must have a copy constructor and copy-assignment operator that correctly allocate and deallocate (in the case of copy-assignment) duplicate resources; otherwise you get double destruction if you ever copy and then destroy an object of that class.

A simple test to try: Delete lint 5 of the original code, thereby getting rid of the destructor. The resulting code will surely leak memory, but at least it won't try to delete the same memory twice. Then try running it again and see how the behavior changes.

Again, deleting the destructor will not fix the problem. Instead, it will convert an acute problem into a chronic one. But it will certainly aid in the diagnosis.

arkoenig 340 Practically a Master Poster

gerard4143 is mistaken -- the semicolon is correct in this context.

However, I can see three potential problems and I can't tell from the code you've posted whether they're relevant:

1) What types do j and k have? If they are unsigned, then j-1 and k-1 will never be < 0.

2) Is it possible that j and k may have values that are too large? You don't seem to be checking for that.

3) Are you sure that every element of tileMap has an appropriate value?

arkoenig 340 Practically a Master Poster

And then there's the issue that maybe the most straightforward thing to do is to unwind the recursion part but not all the way.

For example:

void traverse(Tree* p)
{
    if (p) {
        work_on(p);
        traverse(p->left);
        traverse(p->right);
    }
}

The second call to traverse is a tail recursion, which implies that it is probably a worthwhile optimization to rewrite it as an iteration:

void traverse(Tree* p)
{
    while (p) {
        work_on(p);
        traverse(p->left);
        p = p->right;
    }
}

The remaining call to traverse is not a tail recursion, so it is much harder to rewrite the code to eliminate it. It may well be that it is not worthwhile to do so.

arkoenig 340 Practically a Master Poster

Your poll does not allow me to select more than one alternative. I would like to select #3 and #4 but I can't.

arkoenig 340 Practically a Master Poster

OK, then how do you print n spaces?