388 Posted Topics
Re: Where do you remove elements from the list? I see that you call deleteNode(data), but you haven't shown us what deleteNode does. | |
Re: What the OP described is a variation of the [URL="http://en.wikipedia.org/wiki/Knapsack_problem"]Knapsack Problem[/URL], which is NP-complete (i.e. known to be very difficult from a computational viewpoint). | |
Re: If the array is sorted, the problem is easy to solve in O(n). Start with a pair of pointers (or indices) at opposite ends of the array. Step one of the pointers through the array one element at a time. For each element, step the other pointer [I]backward[/I] through the … | |
Re: 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 … | |
Re: If memory has 16K blocks of 8 bytes each, what does that say about the possible values that memory addresses can have? | |
Re: @tuff: I think that your statement solves the letter of the problem but violates its spirit. That is, I think the point of the original question was to understand which recursive functions could be turned into tail-recursive functions that in turn can be turned into iterative functions that require an … | |
![]() | Re: Instead of [CODE]return (static_cast<Person>(*this) == static_cast<Person>(right));[/CODE] you should write [CODE]return (*static_cast<Person*>(this) == *static_cast<Person*>(&right));[/CODE] because otherwise you are asking the compiler to generate code that copies the objects "right" and "*this". |
Re: I think you want [icode]std::uninitialized_copy[/icode]. | |
Re: Line 26 calls g_c_d recursively and throws the result away. | |
Re: [ICODE]s.substr(0,p)[/ICODE] 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 [icode]s.substr(p+1)[/icode] works. | |
Re: Here's one way. You'll have to translate my prose into code. 1) Start with a sequence of 0 bits. 2) Repeat steps 3-5 until the sequence is all 1-bits. 3) Locate the leftmost 0 bit. (This works because the sequence is known not to be all 1-bits) 4) Set that … | |
Re: [URL="http://www.youtube.com/watch?v=toiM1B6E2ww"]Click here.[/URL] | |
Re: Your loop says [icode]for x in range(50):[/icode], 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? | |
Re: I'm biased, of course; but I think that [I]Accelerated C++[/I] is pretty close to what you're looking for; it definitely has a tutorial narrative, so it's intended to be read sequentially (unlike many that are essentially reference books) and it covers quite a bit more material than most introductory books. | |
Re: 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 … | |
Re: [QUOTE=vegaseat;1319267]Switch/case is rather slow in C or C++ and therefore was not implemented in Python.[/QUOTE] Switch/case is slow in C or C++? Not in my experience. Do you have evidence to support this claim? | |
Re: The "for each" statement isn't part of standard C++, so I'm not familiar with its exact semantics. However, I would expect that it would have to work by making [icode]comp[/icode] a copy of each element in turn of [icode]sysRelMap[/icode]. The reason is simple: If it didn't make a copy, what … | |
Re: [icode]overwriteSlot < 1 && overwriteSlot > 3[/icode] 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 &&. | |
Re: If you write a loop that runs backward through an array, it is a good idea to get into the habit of decrementing the control variable at the [I]beginning[/I] of the loop: [code] int pro[100]; for (int a = 100; a > 0;) { --a; // whatever } [/code] This … | |
Re: Most of the programmers I know--but by no means all of them--are male. I think that reflects interest much more than it reflects talent or aptitude. For that matter, I've seen some spectacularly bad male programmers as well. | |
Re: If you want your program to be reliable, I suggest you look for a different way of solving your problem. You have a pointer to one type (uint8) and you casted it to a pointer to a different type (A). Compilers are under no obligation to make such a cast … | |
Re: What do you want to happen when you do this? [code] SecondChild sc; sc.setResults("foo"); [/code] and how do you want what happens to differ from what happens when you do this? [code] BaseDB b; b.setResults("foo"); [/code] In both cases, you're trying to call a member function that isn't defined. | |
Re: [QUOTE=mike_2000_17;1316517]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 [URL="http://www.cplusplus.com/reference/stl/list/"]list[/URL] provides only [URL="http://www.cplusplus.com/reference/std/iterator/BidirectionalIterator/"]bidirectional iterators[/URL], not [URL="http://www.cplusplus.com/reference/std/iterator/RandomAccessIterator/"]random access iterators[/URL], which is required for the [URL="http://www.cplusplus.com/reference/algorithm/sort/"]sort algorithm[/URL]. [/QUOTE] The standard list class has its own sort member … | |
Re: There is only one way to learn to write programs, and that is by writing programs. Just get out there and do it. If you can, work on stuff that you personally find interesting; but writing programs that bore you will still teach you more than not programming at all. ![]() | |
Re: The current C++ standard does not support threads, so you will either have to pick a nonstandard implementation or wait until the new standard is complete. | |
Re: [QUOTE=embooglement;1316052]The line "car * autocar = new car[count];" makes an array of cars of size count, and autocar is a pointer to that array.[/QUOTE] 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 … ![]() | |
Re: [QUOTE=Ancient Dragon;1315193]Well, you would be doing it wrong. There is no need to hard code the number of digits. [code] int sumdigits(int n) { int sum = 0; while( n > 0) { sum += (n%10); n /= 10; } return sum; } [/code][/QUOTE] This program doesn't quite solve the … | |
Re: What I don't understand is why a book that purports to teach C++ spends so much effort on what is effectively a C program. Isn't it easier to understand this? [code] // read the first string string s1; cout << "Enter string #1: "; getline(cin, s1); // now the second … | |
Re: [QUOTE=yapkm01;1315161]C++ Primer 4th edition says default constructor is used regardless where a class variable is declared - does that means variable of class type is initialized whether it is global or local?[/QUOTE] Yes, that's what it means, as long as the class has one or more constructors. Even if the … | |
Re: [CODE] xTPos = (rand()%15)+1; yTPos = (rand()%15)+1; [/CODE] Why the +1? | |
Re: [QUOTE=pdk123;1304231]As you suggested if i use the prgma_pack for these structures, i donot need to worry at all... [/QUOTE] If you use [I]any[/I] pragma, you [I]always[/I] need to worry. Pragma has two properties that is guaranteed to cause worry: 1) The very nature of pragma is machine-dependent. 2) An implementation … | |
Re: The first statements you execute in your main program are [CODE] p = NULL; add(&p,5); [/CODE] The first statements you execute in add are [CODE] r=*q; if(num<=r->data||r==NULL) // ... [/CODE] Here, q is the first parameter of add, which means that the value of q is &p as computed in … | |
Re: [QUOTE=miturian;1304080]So, what is puzzling me here is how the length can be shorther than the amount of elements in the vector? I thought it was supposed to resize automatically?[/QUOTE] You thought wrong. The size of a vector changes only if you do something that changes it. Assigning a value to … | |
Re: Line 16 compares the values of the two pointers, not the values of the doubles to which they point. Also, if your argument is const double *, that means you're promising not to change the value to which the pointer points. If you now return double *, you're violating that … | |
Re: [QUOTE=hazar;1295177]What is to stop someone simply 'going backwards' in terms of the encryption process, for example if I encrypt something using a public key why can't I simply decrypt it using the same public key? [/QUOTE] The simplest answer is that if it were possible to do that, what you … | |
Re: Just for fun... What happens if you add the semicolon that you forgot after the } in foo.h? | |
Re: Please show us the work you've done so far, and explain where you're stuck. | |
Re: Every derived-class object contains a base-class object as part of it. By implication, constructing a derived-class object involves constructing the base-class object as well. Therefore, when you construct an object of type Bar, part of that construction process is to construct an object of type Foo. The Foo constructor is … | |
Re: Are you saying that you'd like a programmer to be able to write 14 and have it mean, say, 42? That's what changing a literal constant would mean. | |
Re: This is a Java program; I think you will be more likely to get help with it if you post it in a Java forum rather than a C++ forum. | |
Re: The reason is that it is difficult to implement nested functions in a way that maintains compatibility with C and other compilers. For example: [code] extern void f(void (*)()); int main() { int x; void g() { x = 42; } f(g); } [/code] In order for this program to … | |
Re: The expression [icode]pick != 'r' || pick != 'g'[/icode] is always true. The reason is that either [icode]pick != 'r'[/icode] is true, in which case the whole expression is true, or [icode]pick != 'r'[/icode] is false, in which case pick must be equal to 'r'. And if that is the … | |
Re: Also, the form of your example: [CODE] String operator+(const String &s) [/CODE] suggests that [icode]operator+[/icode] is a member function. The name of the class, [icode]String[/icode], suggests that [icode]operator+[/icode] is probably being used for concatenation. The fact that the function has one argument rather than two suggests that it is probably … | |
Re: I beg to differ with both of the previous posters. Most computers these days (including, so far as I know, all currently manufactured Intel processors) use IEEE floating point, which requires that the results of floating-point addition, subtraction, multiplication, division, and square root be deterministic. In particular, the IEEE standard … | |
Re: Assuming that the actual question meant to ask whether each element of one array is equal to the corresponding element of the other, the first observation is that in order for them to be equal, they must have the same number of elements, which I'll call [icode]n[/icode]. Then [code] std::equal(array1, … | |
Re: Note that the value returned by [icode]ip.c_str()[/icode] persists only until the next time you change the value of [icode]ip[/icode]. So if you modify [icode]ip[/icode] in any way between the time you assign the value to argv[1] and the time you use the value, the effect is undefined. | |
Re: You should write[icode]iter!=fg_sprites.end()[/icode] rather than [icode]iter<fg_sprites.end()[/icode] because the multiset type offers a bidirectional iterator, not a random-access iterator, and only random-access iterators support <. (also, it is preferable to write [icode]++iter[/icode] rather than [icode]iter++[/icode] because there's no need to copy the value of [icode]iter[/icode] and then throw the copy away … | |
Re: Your statement [CODE] Write = &WriteLogS; [/CODE] should be [CODE] Write = &Logger::WriteLogS; [/CODE] and similarly for the other similar statement. | |
Re: I'd like to echo Aranarth's comments. In addition: 1) The clear() function appears to delete one element, despite the comment that suggests that it deletes all of them. 2) Therefore, the destructor deletes one element and leaves the rest of them sitting around. 3) You have no copy constructor or … |
The End.