Narue 5,707 Bad Cop Team Colleague

>I'm sorry if a 1300% increase in speed isn't considered better in your opinion
Certainly not in this case. You failed to describe exactly what you meant by saying "best" (assuming that "best" was execution speed), you failed to compare two equivalent solutions to prove your assertion, and you failed to compare the execution speeds correctly. Therefore your premise was subjective, your test was flawed, and your conclusion was biased.

Show me a legitimate 1300% increase in speed and I'll take you more seriously. :icon_rolleyes:

>This is the latest code for the efficiency test, and you will find the results to be quite identical.
Not when taken out of the timing loop. The timing loop should be only for timing, but your "non div + mod calculations" require it for correct behavior. These two snippets are equivalent without the timing loop:

int iX = i % ROW;
int iY = i / ROW;
int iX = 0;
int iY = 0;

for ( int j = 0; j < i; j++ ) {
  if ( ++iX >= ROW ) {
    ++iY;
    iX = 0;
  }
}

That's what I mean by identical results. If you remove the timing loop and run the code with any value of i, and the result is the same. In your code the result is only the same when run with consecutive values of i, which means your method is dependent on the timing loop while the method you …

Narue 5,707 Bad Cop Team Colleague

>i was wondering how much i should use classes in C++.
As often as you need them, obviously. In my experience, this question is born of ignorance. You don't understand OOP and classes well enough to know when to use them, so you try to turn it into some kind of nonsensical numbers game. It's not that simple, unfortunately.

Just try to figure out the concepts behind a feature and eventually you'll learn when and where it's best used.

>Should i use it as much as possible?
:icon_rolleyes:

Alex Edwards commented: Although someone harsh, I do agree! =) +4
Narue 5,707 Bad Cop Team Colleague

>I'm trying to prove to my teacher that Div and Mod isn't always the best way to solve things.
I find it humorous that you seem to think "fastest" and "best" are synonymous.

>You're all welcome to show your instructors this =)
Oh, I will indeed. It's a very amusing test, seeing as how an accurate comparison of a feature and it's alternative should produce the same results for both when hoisted out of the timing loop. I'm curious how you think that this:

iX = i % ROW;
iY = i / ROW;

Has identical results to this:

iX++;
if (iX >= ROW)
{
  iY++;
  iX = 0;
}
Narue 5,707 Bad Cop Team Colleague

>#include <iomanip> // what is this library used for
The <iomanip> header essentially declares stream manipulators that take an argument. This is as opposed to those stream manipulators that don't take a special argument and are declared in the <ios> header.

The code you posted is actually incorrect in that it includes <iomanip>, but uses manipulators that are declared in <ios> (std::oct, std::dec, and std::hex). However, due to the common inclusion of <ios> by <iostream>, it works as intended. I'd recommend that you drop <iomanip> and include <ios> directly for maximum portability. I'd also recommend explicitly qualifying your standard names with std:: and not abusing the using directive:

#include <iostream>
#include <ios>
#include <string>
#include <bitset>

int main()
{
  std::cout << "Enter a binary number: ";

  std::string bin;

  if (getline(std::cin, bin)) {
    std::bitset<32> bits(bin);
    std::cout <<"Octal  ";
    std::cout << std::oct << bits.to_ulong() << '\n'; 
    std::cout <<"Hex   ";
    std::cout << std::hex << bits.to_ulong() << '\n';
    std::cout <<"dec   ";
    std::cout << std::dec << bits.to_ulong() << '\n';
  }

  return 0;
}

>//what does the bits.to_ulong() part do or what is it tied to .
to_ulong takes the bit pattern stored in bits and copies it in an unsigned long value. The result is that the bit pattern "11011" will produce 27 when you call to_ulong() and print the value as a decimal number.

Narue 5,707 Bad Cop Team Colleague

Post your code.

Narue 5,707 Bad Cop Team Colleague

I'm not sure what you mean, but I'd guess that you don't know how to signal end-of-file from the command line, so the loop never ends. Hold down the ctrl key and press z (ctrl+z) if you're on Windows and d (ctrl+d) if you're on Unix or Linux.

Narue 5,707 Bad Cop Team Colleague

I'm looking at the site in Firefox presently, and everything seems fine. What do you mean by "it didn't work properly"?

Narue 5,707 Bad Cop Team Colleague

Command line arguments are passed as parameters to main. The first parameter gives you the number of arguments (including the program name) and the second gives you an array of strings containing the actual arguments:

#include <iostream>

int main ( int argc, char *argv[] )
{
  // The first argument is the program name, if available
  if ( argc > 1 ) {
    for ( int i = 1; i < argc; i++ )
      std::cout<< argv[i] <<'\n';
  }
}
Narue 5,707 Bad Cop Team Colleague

>Now it somehow skips straight to 'else', missing out the 'if' and the 'else if'......
That's because getch doesn't perform text conversions. It's a straight detection of scan codes from the console. This means that on systems (such as Windows) where a newline is two values before being converted to '\n' by C++'s stream buffer, calling getch once will give you the first of those values.

Assuming you're on Windows, here's what is happening. When you press Return, a Windows newline is signaled and two values are sent to the console: 0xD and 0xA, in that order. This is the CR-LF pair you may have heard about. Let's say that '\n' in C++ represents 0xA. A standard C++ text stream will take 0xD-0xA and compact it into 0xA, but getch won't do anything of the sort and will simply return 0xD.

So the result is that you're comparing 0xD with 0xA, which is clearly not true, so the else statement is performed. You can catch the carriage return using the '\r' escape character:

#include <iostream>
#include <conio.h>

const int ESC = 0x1B;

int main()
{
  int a;

  while ( ( a = getch() ) != ESC ) {
    if ( a == '\r' )
      std::cout<< a <<"\tNewline detected\n";
    else
      std::cout<< a <<"\tNot a newline\n";
  }
}
Narue 5,707 Bad Cop Team Colleague

Inclusion guards are a preprocessor trick. If a macro with a unique name for that header is not defined, proceed to process the header. If it is defined, don't process the header. The macro itself is defined during the processing of the header, so the result is that the header is only processed once:

#ifndef FORM13_H
#define FORM13_H

// Your existing header contents go here

#endif
Narue 5,707 Bad Cop Team Colleague

Are you using inclusion guards in Form13.h?

Narue 5,707 Bad Cop Team Colleague

>In my example, Do you mean the default behavior is call the operator= of class B?
I fail to see how my reply was ambiguous. You want a member array to be properly copied even in the case where a non-default copy constructor or assignment operator exists for the array type, and that's what happens. Therefore, saying that the default behavior will do what you want is a complete answer to your question.

>If the default behavior is just memory copy and class B
>have pointer member, it will produce a undefined result.
That's strictly a quality of implementation issue for class B. If B implements a copy constructor/assignment operator then A will have to call it when copying any object of B, even if the object of B is a member of an array.

Narue 5,707 Bad Cop Team Colleague

That's a classic error. It means you created a Win32 application project but wrote code for a console mode application. The former requires WinMain instead of main as the entery point.

Narue 5,707 Bad Cop Team Colleague

>Does it need a copy constructor and an operator= for a class which have array member?
No, the default behavior will do what you want.

Narue 5,707 Bad Cop Team Colleague

>Do you think that an old compilator can differ a magnitude order from the new one?
Yes. Especially since Visual C++ 6.0's STL implementation was extremely weak.

>Can I do easily a dll for windows without using Microsoft Visual C++ compilator?
Writing DLLs in Windows is not difficult. Though I would highly recommend upgrading your Visual C++ to 2005 or 2008. Visual C++ 6.0 is painful if you plan on making use of templates.

Narue 5,707 Bad Cop Team Colleague

>that is the code for decimal to binary...
That's pretty awful. At least it kind of works.

>#include <conio.h>
You don't even use anything from this header, why destroy portability by including it?

>void main()
Here is the only correct definition of main with no parameters:

int main ( void )
{
  /* Your code here */

  return 0;
}

The return value can be any integer you want, but the only portable ones are 0, EXIT_SUCCESS, and EXIT_FAILURE. The latter two are macros defined in <stdlib.h>. Anything except what I've described is non-portable. For a definition of main that takes parameters, the following is the only correct definition:

int main ( int argc, char *argv[] )
{
  /* Your code here */

  return 0;
}

There are only two parameters. The first is an integer and the second is a pointer to the first element of an array of pointers to char. Any equivalent type is allowed, and the identifiers can be whatever you want (though argc and argv are conventional), for example:

typedef int FOO;

int main ( FOO ac, char **av )
{
  /* Your code here */

  return 0;
}

Anything else is non-portable.

>int n, a[30],i=0;
I'm gradually becoming more and more hostile toward this practice. It's a great way to hide variable declarations from readers, and it's also a great way to make maintenance more difficult. I've reached this conclusion after having to change …

Narue 5,707 Bad Cop Team Colleague

>How can one go about applying the same set of rules to the C and the Ruby forums?
My suggestion was to apply a language specific algorithm to the individual forums. At the very least we can cover the most active forums with a handful of cases and solve the majority of the problem. But I'm not the one writing the code to do this, Dani is. If she doesn't feel a solution is good enough, it won't get implemented, and there's a team of mods available to do the work manually.

Narue 5,707 Bad Cop Team Colleague

As I understand the issue, Dani isn't against an automated solution as long as it's generalized. Every time we bring up the issue, she asks us for a good way to handle checking for code tags with every language that Daniweb has a forum for. A good suggestion has yet to be made, so the issue still stands.

>In the c++/c section 'someone' could add a piece
>of PHP code that checks if the post contains main(
And if the post is a snippet without main? A good test is for an opening brace. If the text contains an opening brace, it's a pretty good assumption that the text is code in the C++, C, Java, and C# forums. However, that approach doesn't work well for pretty much any of the other forums, so it fails the generality test.

The only other option is to be excessively strict about code tags (ie. warn, infract, and ban with extreme prejudice), and that's counter-productive if the goal is keeping people around. :icon_rolleyes:

Now for the original request in this thread:

>Remove all mention of code tags from the site
That's a stupid idea, it's easy to figure out why, and the extremity of the suggestion leads me to believe that you're simply being melodramatic.

Removing all mention of code tags (ie. instructions and reminders) would destroy any positive effect that they have. It's silly to believe that the instructions and reminders are 100% ineffective, …

jasimp commented: Well said +8
Narue 5,707 Bad Cop Team Colleague

>But where I worry about it the age of the book
No worries, the contents are still relevant.

Narue 5,707 Bad Cop Team Colleague

>You're getting C++ errors. C++ is a lot stricter. Try using a C compiler.
So your advice is to ignore the warning and let the problem silently continue. Brilliant. :icon_rolleyes:

>"cannot convert from" is a C++ (and in general, OOP) only error afaik.
You get it in C as well if you do something stupid...just like C++.

Narue 5,707 Bad Cop Team Colleague

>I do not know if my code will be portable when I start learning it.
It won't be. Assembly languages are only portable to the processor family (and depending on the features you use, the family subset) they're implemented for.

>Do other Assembly languages conform to the same standards
The core concepts are the same across the board, and that's what you should be trying to learn. Don't focus so much on the difference in syntax because syntax is easy to learn. The hard part is learning how to program at such a low level.

>or is the Syntax incredibly different?
It depends. The difference between NASM and FASM is slight, the difference between FASM and GAS takes a bit of getting used to, and the difference between GAS and something like Terse is staggering. The most widely used assemblers implement similar enough syntax such that moving between them is relatively painless.

>Book recommendations would be great!
Since you're using HLA, The Art of Assembly by Randall Hyde would be helpful for the details of the language as well as the concepts of programming in assembly.

Salem commented: Nicely summed up. +19
Alex Edwards commented: Thanks again Professor =) +2
Narue 5,707 Bad Cop Team Colleague

I disagree, of course. Quality is paramount even when doing incremental releases. The increments refer to feature sets, not bug fixes.

Narue 5,707 Bad Cop Team Colleague

>Well from wot i no pseudocode isnt real code so how can i test it?
The same way you test real code without running it: trace the execution with paper and pencil using a small amount of sample data. Not only does this give you a better idea of what your code is doing, regular paper tests help you to think like a compiler when you're actually writing code.

Narue 5,707 Bad Cop Team Colleague

That usually happens to me when I forget to close the output window for a debug run.

Narue 5,707 Bad Cop Team Colleague

>I think it is something to do with srand( time(NULL);
Nope, your logic is just wrong if you want game to end at exactly 1001. Notice how you don't check that condition every time game is incremented. You only check the condition after the inner loop has terminated. game could be incremented several times in the inner loop.

Narue 5,707 Bad Cop Team Colleague

>But what I can't seem to access is the constructor.
This is where the C memory functions fall flat, they don't work well with constructors and destructors. I recommend ditching calloc and using new[]:

CObject *group = new CObject[N];

Or better yet, use the vector container class if you plan on resizing the collection:

#include <vector>

...

std::vector<CObject> group ( 1 ); // Start off with 1 element
Narue 5,707 Bad Cop Team Colleague

It means you probably wrote to memory you don't own, perhaps by overrunning a buffer, freeing memory more than once, or forgetting to initialize a pointer before using it.

Narue 5,707 Bad Cop Team Colleague

>I have little chance of actually getting anywhere in the game development
>industry - because I am not getting a degree that is specifically game related.
I'd say your friend is full of it. Ability trumps education every time, and the gaming industry is especially well-known for turning basement coders into millionaires. A degree in CS won't hurt you (it'll probably help more than the specialized degree, but I'll get to that in a moment), and a good portfolio will go much further than a degree from a "game programming college".

Now, the reality is this: game development is very competitive. You'll be going up against some extremely talented people, so unless you're one of them, it's likely that you won't end up with your dream job for a while. In that case, a general CS degree can benefit you more than a game programming degree as you work your way into the field.

Narue 5,707 Bad Cop Team Colleague

>So obviously I am missing a fundamental part of the logical not operator.
Let's break it down, starting with the if statement:

if ( ... ) {
  ...
}

If the condition is true, execute the block. Simple, right? Now let's add a boolean test:

if ( x < y ) {
  ...
}

If the result of x < y is true, execute the block. Or in more typical terms, if x < y, execute the block. Still simple, no? What about if we put the result of that boolean test in a boolean variable?

bool test = x < y;

if ( test ) {
  ...
}

If the condition evaluates to true, execute the block. The condition is an expression containing only a boolean variable whose value is the result of the test x < y. The current value of the variable acts as the condition. If the variable is true (ie. x < y), the block will be executed, otherwise it won't. Still simple, right? Now let's start negating:

bool test = x < y;

if ( !test ) {
  ...
}

If the condition evaluates to true, execute the block. The condition is still an expression containing only a boolean variable whose value is the result of the test x < y. But now we've negated that value, so if x < y, the block won't be executed because x < y is true, and negated it becomes false. In …

Vallnerik25 commented: Awesome help, good answers, got to the root of my problem with excellent examples. +1
Salem commented: Indeed it is +19
Narue 5,707 Bad Cop Team Colleague

>template <class T>
>Element* Btree<T>::recins( T& obj, Element* cpoz )
Element is a dependent type, so you need to be very specific about what it is:

template <class T>
typename Btree<T>::Element* Btree<T>::recins( T& obj, Element* cpoz )

>Element *p = new Element( T );
T is a type, you probably meant to use the default value for T like this:

Element *p = new Element( T() );

Also, recins needs to return a value for all execution paths, and you need to include a header (I recommend <cstddef>) that defines the NULL macro.

Narue 5,707 Bad Cop Team Colleague

>For some case such as break multiple-level of loops, goto is inevitable.
I wouldn't say inevitable, but goto can be the cleanest solution if you have deep nesting and can't refactor it. The alternative is to use flags and check them at each level, which is a "pure" structured solution, but it's almost always ugly as sin.

Narue 5,707 Bad Cop Team Colleague

>Now the idea is if b is equal to 5 then it breaks out of
>the if loop, the for loop c, the for loop b, the for loop a.
Nope. You can write as many breaks as you want, but only the first one will have any effect, and it'll break you out of the inner-most loop (the c loop).

>If not can someoone please correct me?
You have options here, but the best option is not to get into such a situation to begin with. If you need to break out of a nested loop, it's usually best (if you can manage it) to refactor the nested loops into a function and then use the return value to break:

for ( int a = 0; a < 10; a++ ) {
  if ( !some_function() )
    break;
}

...

bool some_function()
{
  for ( int b = 0; b < 10; b++ ) {
    for ( int c = 0; c < 10; c++ ) {
      if ( b == 5 )
        return false;
    }
  }

  return true;
}
Narue 5,707 Bad Cop Team Colleague

>cannot access private member declared in class 'System::Windows::Forms::Control
Well, the error message is pretty freaking clear about what the problem is. Are you not capable of figuring out why you can't access txtresult? It seems like you're trying to get to the Text property of a control on a different form, is that the case?

Narue 5,707 Bad Cop Team Colleague

How about removing the string streams and simply doing this?

txtresult->text = Convert::ToString ( method );
Narue 5,707 Bad Cop Team Colleague

That's what you get for taking the path of least resistance. Now why don't you tell us what you're trying to do so we can suggest a good solution using System::string?

Narue 5,707 Bad Cop Team Colleague

>I can't find any way around it because System::string
>has no stringstream, or any ostringstream.
You probably don't need a string stream. System::string is a surprisingly robust class that can probably handle what you want to do in a slightly different way. Perhaps if you described the problem you want to solve, someone can suggest a way to do it using System::string directly.

Narue 5,707 Bad Cop Team Colleague

>What is the situation where quadratic probing is better than chaining?
Chaining takes up more memory, so if storage is tight, one of the open addressing schemes would be better suited. The time it takes to chase pointers in a chained solution might also be prohibitive, in which case an open addressing scheme could be more efficient.

>if the Hashtable have a lot of elements in chaining it'll take a lot of time searching
If the hash table is well designed, the table itself will have enough buckets to keep the chains short, and the hash function will distribute values roughly equally, so this situation would only occur in degenerate cases. It's a good thing to keep in mind, but quadratic probing has degenerate cases as well (more of them), so I wouldn't accept this as an answer to the question.

>my answer for this question that It'll be like the code below
Sneaky, but that technically makes two passes through the list: one going forward and one going backward as you ride the recursion wave. To be strictly correct, you'd need an iterative solution that builds a new list from the existing nodes:

node *result = 0;

while ( head != 0 ) {
  node *save = head->next;

  head->next = result;
  result = head;

  head = save;
}

head = result;
Narue 5,707 Bad Cop Team Colleague

To pack? Are you going on a trip? :icon_rolleyes:

>for sure i want to knw C++ and JAVA. But i dnt knw the third!
If you want to go the practical route, select a language you're likely to use. Perl and Python are both good options. If you intend to do web programming then PHP would be a good idea, and depending on what systems you want to specialize in, one of the .NET languages could help.

If you want to push your limits as a programmer, select a language that's fundamentally different than C++ and Java, such as one of the LISP variants, Haskell, or assembly.

Alex Edwards commented: Mean, but funny =P +1
Narue 5,707 Bad Cop Team Colleague

Accelerated C++ by Koenig and Moo fits all of your requirements that aren't terribly subjective or reliant on a specific implementation. I think it's a fun read, but you might not. The code is standards compliant, which means it'll work on every compiler that conforms to the standard (Visual C++ is one of those, if it's newer than version 6.0).

Narue 5,707 Bad Cop Team Colleague

>Is that considered "wrong" or weird?
Falling off the end of a function automatically returns, so it's weird in that people don't expect to see that kind of redundancy in well written code. It's not wrong though.

Narue 5,707 Bad Cop Team Colleague

I meant, you can have more than one linked list:

node *it1 = head1;
node *it2 = head2;

while ( it1 != 0 && it2 != 0 ) {
  if ( it1->data != it2->data )
    break;
}

if ( it1 == 0 && it2 == 0 )
  cout<<"Equal";
Q8iEnG commented: You saved me :D +1
Narue 5,707 Bad Cop Team Colleague

>but for my question I didn't figure how to implement the code for TWO singly linked lists :'[
Dude, it's just like using two arrays. Please don't tell me you're as dense as that statement suggests.

Narue 5,707 Bad Cop Team Colleague
Narue 5,707 Bad Cop Team Colleague

>I was wondering if there are any differences between the Win32 API talked about in
>this book and the one that applies to Vista or anything newer than Windows 2000.
I'd be surprised if there aren't. The API is constantly evolving, but the stuff covered in your book is still relevant.

Narue 5,707 Bad Cop Team Colleague

>Is there anyway to make %s ignore whitespace?
No, but you can use a scanset to get a little more control. For example, to read an entire line with fscanf you could do this:

char buffer[100];

fscanf ( file, "%99[^\n]", buffer );

However, in your case, selecting which strings go where is a little more complicated. You'd be better off reading an entire line and then parsing it with custom logic.

Crushyerbones commented: Thanks +1
Narue 5,707 Bad Cop Team Colleague

>If a gm can close this... That'd be great
We don't close threads when they're solved, but it's possible to flag a thread as solved to notify readers that you got your answer.

Narue 5,707 Bad Cop Team Colleague

Don't use an array. A std::set or std::map seems much better suited to your problem.

Narue 5,707 Bad Cop Team Colleague

>Narue could you elaborate more on this...
People usually have an easier time understanding the rough idea of memory when I explain it using assembly. When a "function" is called, local variables are typically allocated by adjusting the stack pointer to make a big enough gap to hold values for all of the variables:

function:
  add sp, localsize ; Make a hole

  ; Use the locals

  sub sp, localsize ; Close the hole
  ret

That's the general idea behind figuring that the loop doesn't actually reallocate memory with each iteration. In that case, assuming that const_iterator is an class type rather than a built-in type, you can make the examples more accurate like so:

std::vector<int>::const_iterator pos;
for (int i = 0; i < N; i++) {
  pos = myvec.begin();
  // do something with pos
}
{
  std::vector<int>::const_iterator pos;
  for (int i = 0; i < N; i++) {
    pos.const_iterator ( myvec.begin() );
    // do something with pos
    pos.~const_iterator();
  }
}

That seems like a big difference until you consider that the temporaries for myvec.begin() are probably going to be handled identically and that copy construction and assignment are similar enough not to be significantly different in their performance characteristics. Thus the only real difference is that the destructor is called for pos on every iteration in the second example. Unless the destructor is a heavy hitter when it comes to performance (note that the memory isn't being released at this point), the two examples won't much …

Dave Sinkula commented: When's the book coming out? :icon_razz: +15
titaniumdecoy commented: Interesting analysis. +2
n.aggel commented: Thanks for the insight! :) +2
Narue 5,707 Bad Cop Team Colleague

>Is there a difference, in terms of performance, between the two loops below?
It depends on your compiler. There are tons of factors that come into play, such as how your compiler manages stack space, whether the underlying type of const_iterator is a simple pointer or an object, how temporary objects are managed, whether object creation/destruction outweighs copy construction/assignment or the creation/destruction is a bottleneck, etc...

The best answer is that if it matters you'll notice a problem during performance profiling, and until then you should limit the scope of variables as much as possible. Thus, the second example is preferred.

Narue 5,707 Bad Cop Team Colleague

Use a loop to read more than one line:

ifstream in ( "myfile" );

if ( in ) {
  string line;

  while ( getline ( in, line ) )
    cout<< line <<'\n';
}