Hi guys ! I need your help.I have to make a program that reads two numbers a and b and displays all the numbers that can be formed by replacing the first and the last digit of b with every digit of a step by step.
For example,if a = 19,b = 913 it should display 113,911,919,913.
The problem is it doesn't work properly and I don't know how to fix it. What did I do wrong?I used two arrays va and vb that hold the values of every digit in a and b.Then,na is the number of digits of a and nb the number of digits of b.I think the program should display at least one number but it does nothing...
You're wrong about the loop performance. (i>=0) will be read as "must be positive", telling to compiler that it only needs to check a single bit (the sign bit). Your changed version is more specific ("be larger than a specific value"). Perhaps the compiler is smart enough to realize that it is the same (-1 being the "first" integer for which the sign bit is 1), but there's no guarantee. Secondly, the appearance of lonely pre or post-decrement operators is perhaps the easiest to optimize: unless it is part of a compound statement, there is no reason to keep a copy of the old value of 'i', thus both operators will do the exact same.
Also, micro-optimization in these projects in obviously useless. Chances are you're just confusing the OP.
First of all, that was with optimizations disabled (because optimizations are possibly the most compiler-specific part of it all). Secondly, the code doesn't actually matter, because the meaning of 'i' remains the same: it is incremented and read. The increment is done separately before anything else. Just think about it for a second:
1 2 3
int i = 0;
++i;
cout << i;
Does it matter if you use ++i or i++? No, because the "current value" is discarded anyway. If I do this:
1 2
int i = 0;
printf("%d", i++);
..then it does matter, because the old value of 'i' has to be printed and the result would be different if I used ++i. However, in case of for loops, the increment is, again, separate.
Unless compilers are completely retarded in their implementation of pre/post in/decrements, there should be no difference in performance between the two if they are a separate statement.