arkoenig 340 Practically a Master Poster

You can't have an array of size 0, in the sense that every array type includes the number of elements as part of the type, and for the type to be well defined, the number of elements must be strictly positive. Part of the reason for this requirement is to be able to guarantee that sizeof(x) is nonzero regardless of the type of x.

So your second example is not well-formed, although I imagine there might be a compiler out there that accepts it.

Your first example, on the other hand, is fine. There is nothing wrong with using "new" to create a pointer to "the first element" of a 0-element dynamically allocated array. Of course, that first element does not exist, so the pointer is an off-the-end pointer for that array. It's a valid pointer; it's guaranteed to be distinct from other pointers; and you should delete it at some point if you wish to avoid memory leaks. What you can't do is dereference it, because it doesn't point to an element. That's true of any off-the-end pointer, not just this one.

arkoenig 340 Practically a Master Poster

This is for my Data Structures class. This is all we were given:

I understand what your assignment is.

What I am asking you to do is to explain the program that you wrote.

You say that it is producing results that you do not expect.

I am asking you what results you expect the program to produce, and why you expect the program to produce those results. An answer to that question would be something like:

"Well, the first statement sets this variable to this value, and then the next statement prints that variable, so I would expect it to print the value of the variable" and so on.

jonsca commented: Yep +5
arkoenig 340 Practically a Master Poster

What do you expect it to do and why?

arkoenig 340 Practically a Master Poster

What do lines 20-22 do? They don't seem to correspond to any part of the problem specification.

In particular, they seem to say that A(0,1) is equal to A(1,1). I see no justification for that claim.

arkoenig 340 Practically a Master Poster

Your vector 'quantity' is uninitialized, which means you are trying to print garbage.

Not true. If you create a vector of integers and do not initislize it, the integers are initialized to 0.

More generally, if you create a vector of objects of type T, the initialization process creates an object initialized with the value T(), then copies that object into each element of the vector.

arkoenig 340 Practically a Master Poster

Make a random integer between 50 and 99999, then divide it by 100.0

arkoenig 340 Practically a Master Poster

Your program starts with a do...while loop that looks at the value of a variable named "choice" as its terminating condition. After that, you have a collection of if statements that also look at the value of this variable.

However, I can find no place in your program where you ever give that variable a value. What value do you expect to have, and how do you expect the value to get there?

arkoenig 340 Practically a Master Poster

New programming language like python and ruby, do they have space to grow?

New? Python? It's about 20 years old! Heck, Python 2.0 is more than 10 years old...

arkoenig 340 Practically a Master Poster

Your problem is probably that your program is trying to remove blanks in place, but on line 50 you're calling it with a string literal as its arguments. Compilers are permitted to write-protect the contents of string literals, and many of them do these days.

As a quick check of what your implementation does, try the following:

int main()
{
    char* p = "foo";
    *p = ' ';
}

and see what it does.

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

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. 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

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

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 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

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

OK, then how do you print n spaces?

arkoenig 340 Practically a Master Poster

Do you know how to write a loop that does something n times?

arkoenig 340 Practically a Master Poster

Sky Diploma is correct here. Also, note that the first line:

cout << 1 << endl;

can be thought of as:

1. print 0 spaces
2. print the number 1 and a newline

and, of course, "print 0 spaces" is a degenerate case of "print n spaces."

So... how would you code just the "print n spaces" part?

arkoenig 340 Practically a Master Poster

Please answer my question. Does the code fragment I posted correctly produce the first line of your desired output, or doesn't it? Ignore any additional constraints from your homework assignment. Once we get code that produces the correct output, we can worry about how to make it fit the other requirements.

arkoenig 340 Practically a Master Poster

Hmm. The first line should just be

for(int row = 1, row <= number, row++)
cout << row;

Not even close.

The first line of output is

cout << 1 << "\n";

Right?

arkoenig 340 Practically a Master Poster

Here are some questions for you to answer?

1) Do you know how to print the first line of the required output?

2) Do you know how to print the second line?

3) If I give you a number n, do you know how to print the nth line?

arkoenig 340 Practically a Master Poster

If at all u want to use the ASCII equivalent of a letter, it should be like this:

atoi('89');

No, it shouldn't. '89' does not have a well-defined meaning in standard C++, so if your compiler accepts it, it's a local extension.

If you want the character that is equivalent to the decimal value 89 in your machine's character set, you can write char(89) or, more pedantically, static_cast<char>(89) .

arkoenig 340 Practically a Master Poster

I would think that if you replace the type std::pair<std::string, unsigned long> in lines 7 and 23 by std::pair<const std::string, unsigned long>, it ought to work. At least I can't think immediately of any reason it shouldn't.

arkoenig 340 Practically a Master Poster

Why not just do the following?

import a
import b
jopen = a.jopen
ropen = b.ropen
arkoenig 340 Practically a Master Poster

Actually, it's rather remarkable that the code you posted should work at all. You may have thought that you wrote

#define SQUARED(x) (x * x)

but what you actually wrote is

#define SQUARED(x) {x * x}

so that your statement

int i = SQUARED(3 + 2);

should expand to

int i = {3 + 2 * 3 + 2};

As it happens, however, C++ allows initializers to be enclosed in curly braces. This usage is important for initializing arrays and structures. So the expansion of your example is equivalent to

int i = 3 + 2 * 3 + 2;

which, as mentioned earlier, evaluates to 11.

arkoenig 340 Practically a Master Poster

You can't subtract strings.

vasilevich commented: Helped me solve the whole problem with a very simple answer. +0
arkoenig 340 Practically a Master Poster

Here's a hint.

Start by writing a program that reads a number and prints it out again.

arkoenig 340 Practically a Master Poster

Line 10 defines a variable named "a". Line 45 also defines a variable named "a". Which one do you expect your "find" function to use in line 31, and why?

Also, while we're at it, lines 44-45 and 59 are not legal in standard C++: The size of an array must be known at compile time. If you want something that acts like an array but can have its size determined at run time, use a vector.

arkoenig 340 Practically a Master Poster

That's true if the only place DriveCheck is called is from Drive--but we don't know that for sure.

arkoenig 340 Practically a Master Poster

You define badDriveList in one function and expect to be able to use it in another. That won't work, period. As you've written your code, badDriveList is a local variable in TestSystem::Drive, and will be destroyed as soon as TestSystem::Drive exits.

What you probably need to do is to make badDriveList a data member of TestSystem. Until you do that, or something like it, there's little point in talking about other problems.

arkoenig 340 Practically a Master Poster

Here is what I think is a simpler version of the question you are asking.

struct Foo {
    int x, y;
};

Foo bar[2];

void f()
{
    int* p = &bar[0].x;

    // ...
}

I believe you are asking where there is any portable of incrementing p so that it points to bar[1].x instead of pointing to bar[0].x where it currently points.

I believe that the answer to that question is no: There is no portable way of doing this. Probably the closest you can come is something like this:

typedef unsigned char* UCP;

p = reinterpret_cast<int*> ( p +
    (reinterpret_cast<UCP>(&bar[1].x) - reinterpret_cast<UCP>(&bar[0].x)));

The idea is that C++ blesses pointers to unsigned char as if they are pointers to raw memory, and casts to and from that type (which I have abbreviated as UCP) have portability properties that other uses of reinterpret_cast do not share.

So the code above "measures" the distance between corresponding members of adjacent elements of bar in unsigned-char units, then applies that offset to the UCP address of one element to obtain the address of the corresponding other element.

I am going out on a limb here, but my guess is that (a) most members of the C++ committee have not thought about this specific issue, and if asked whether it should be considered portable, there would not be immediate agreement on the answer, but that (b) most compilers would wind up generating code from these statements that would do the …

arkoenig 340 Practically a Master Poster

If you can't solve the problem, try solving a simpler one first.

For example, try writing a function that takes two integers as its arguments and returns their sum as its result. Then try making the function return a long integer.

arkoenig 340 Practically a Master Poster

Once your program has reached end of file, it is no longer possible to read any more input. So if you enter end of file after reading the last student's name, it is not possible to read the grades.

The intent of the exercise was for each student's name to be followed by a list of grades for that individual student. I think that if you reorganize your program to read input in that form, you may have an easier time of it.

arkoenig 340 Practically a Master Poster

Yes you can solve this just with AND OR XOR NOT.

Moreover, because NOT(a) is XOR(A,A), you don't need NOT. Similar reasoning will show you tnat you don't need AND or OR, either, so long as you have XOR.

If, on the other and, you don't have XOR, then you need NOT and either AND or OR.

acer5542 commented: he helped me to solve my self without the answer +1
arkoenig 340 Practically a Master Poster

s.substr(0,p) The first argument is the starting position; the second is the length. If you omit the length, you get the longest possible substring with the given starting position, which is why s.substr(p+1) works.

arkoenig 340 Practically a Master Poster

Your loop says for x in range(50): , so it will execute 50 times. You want to create a 50 by 50 grid, so you want it to have 2500 entries. How can you expect to create a data structure with 2500 entries if your loop executes only 50 times?

arkoenig 340 Practically a Master Poster

This is my original statement:
It does not make any reference to an if/else if construct.

There's no other real alternative to switch/case in C or C++.
In particular, on every C or C++ implementation I've seen, the overhead of calling a function is much greater than that of a switch statement, so fetching an element from an array of function pointers is very unlikely to be a more efficient substitute for a switch.

arkoenig 340 Practically a Master Poster

It is hard to compare a Python dictionary lookup with a C switch/case construct.
Also switch/case is very limited in application.

Of course.

I was responding to the specific claim that Python does not have switch statements because they're slower in C than if/else statements. I haven't commented on whether I think switch statements might be useful in Python for other reasons.

arkoenig 340 Practically a Master Poster

I think it's easy as making a case and a for loop and testing them.

Assuming you're talking about testing the speed of switch statements: I can test only the compilers I have available, and on those I can see by looking at the machine code that switch statements often generated faster code than could readily be obtained by hand coding.

Moreover, in decades of working with C and C++ compilers, I don't think I've ever encountered one that generates slower code for a switch statement than it does for the corresponding if-else statements.

So when someone makes the blanket claim that switch statements are slow, I think it's up to the person making that claim to prove it.

arkoenig 340 Practically a Master Poster

Switch/case is rather slow in C or C++ and therefore was not implemented in Python.

Switch/case is slow in C or C++? Not in my experience. Do you have evidence to support this claim?

arkoenig 340 Practically a Master Poster

My first observation is that you are sorting the entire list (or vector) every time you put a new element there. So if you have 1,000 elements, you're sorting it 1,000 times. It's hard to see how that can be particularly fast.

If you need to put new elements into the data structure while keeping it sorted, why not use a set or multiset?

arkoenig 340 Practically a Master Poster

Using the standard list.sort algorithm should not take several seconds to sort a few hundred elements, so something must be wrong.

Unfortunately, I can't read the file you attached. When I try, I get "invalid or corrupted"

So perhaps you could post the relevant portions of the code, particularly your comparison function.

arkoenig 340 Practically a Master Poster

overwriteSlot < 1 && overwriteSlot > 3 is always false, because no value can be less than 1 and greater than 3 at the same time. So your loop will never terminate.

I am guessing that you meant to write || rather than &&.

arkoenig 340 Practically a Master Poster

What do you want to happen when you do this?

SecondChild sc;
sc.setResults("foo");

and how do you want what happens to differ from what happens when you do this?

BaseDB b;
b.setResults("foo");

In both cases, you're trying to call a member function that isn't defined.

arkoenig 340 Practically a Master Poster

Well, you can use the std::sort algorithm in "#include <algorithm>". The only little problem is that it cannot be used with a list because list provides only bidirectional iterators, not random access iterators, which is required for the sort algorithm.

The standard list class has its own sort member that uses a different algorithm from the stand-alone sort algorithm. So if myList is a list, then instead of writing

sort(myList.begin(), myList.end(), compare);

you can write

myList.sort(compare);
arkoenig 340 Practically a Master Poster

The line "car * autocar = new car[count];" makes an array of cars of size count, and autocar is a pointer to that array.

Not quite: autocar is a pointer to the initial element of the array, not a pointer to the array. This distinction is important because it means that *autocar is an element, not an array.