Journal Articles
Browse in : |
All
> Journals
> CVu
> 131
(16)
All > Topics > Programming (877) Any of these categories - All of these categories |
Note: when you create a new publication type, the articles module will automatically use the templates user-display-[publicationtype].xt and user-summary-[publicationtype].xt. If those templates do not exist when you try to preview or display a new article, you'll get this warning :-) Please place your own templates in themes/yourtheme/modules/articles . The templates will get the extension .xt there.
Title: Questions and Answers
Author: Administrator
Date: 04 February 2001 13:15:43 +00:00 or Sun, 04 February 2001 13:15:43 +00:00
Summary:
Body:
Unless I have lost something, we are very short of answers in this issue. I have added a few more questions just to keep the pot simmering. Of course some of you just read C Vu for answers, but I know many of you read it to stimulate your grey matter. A lifetime of experience has taught me that I only understand something when I can write it up for someone else. I have lost count of the number of times when I thought I understood something only to hit a fatal flaw when forced to write about it.
Even when I have been correct in my understanding, it is the process of distilling it to simple words that allows me to take possession of the idea. Please do not short change yourselves by only reading this column. One reason that I do not give the answers myself is that that would destroy at least half of what this is about.
from: James Holland <jamie_holland@lineone.net>
The problem occurs when assigning a floating-point number to an integer. The C standard states that the fractional part is truncated. A floating-point value, say 11.99, being assigned to an integer variable would result in the variable containing 11. Presumably, what is required is for the integer variable to be set to the nearest integer representing the floating-point valve. I suggest the following expression.
int k; float p; ... k = floor(p + 0.5);
where floor() is declared in math.h.
This method works with negative numbers as well as positive ones. Incidentally, Borland 5.5.1 gives k = 12 as the result of the program in the original question.
The problem was about the use of a namespace directive when none of the included files declared that namespace (incidentally, do you declare or define a namespace?). The specific problem related to namespace std but is not confined to that. Before you can use a namespace directive, that namespace must be visible to the compiler. Some standard header files (such as cassert and climits) do not introduce names to std:: and so do not use it. So the question comes down to whether there is a way of declaring a namespace name in a way that is analogous to declaring a class name.
The solution once you see it is very simple. Namespaces can be re-opened (unlike classes) so all you need to do is:
namespace std {}
Now you can safely write:
using namespace std;
as the next line of your source code.
from Jim Hyslop <Jim.Hyslop@Leitch.com>
Silas Brown asked, given the expression:
f(g(a()), g(b()), g(c()))
"... is each call of a, b and c guaranteed to be followed by a call to g, or is the compiler free to call a, b and c first and then g three times..."
It is the latter, I'm afraid. Each of a(), b(), c() and each call to g is considered a separate sub expression. The compiler can evaluate each of these sub-expressions in any order (except, of course, that it cannot call g before it has called the corresponding one of a, b or c).
If you need the arguments evaluated in a particular order, then it is up to you to re-write the code to force that order, as in:
If you want the order a, g, b, g, c, g
arg1 = g(a()); arg2 = g(b()); arg3 = g(c()); f(arg1, arg2, arg3);
or, if you do not care which order the 'g's are called, but you do want a, then b, then c:
argx = a(); argy = b(); argz = c(); f( g(argx), g(argy), g(argz));
Thanks Jim, welcome to these pages.
Q1. (from Silas Brown) www.flatline.org.uk/~silas
Should a base class's methods still be visible when they are overloaded by a subclass? My compiler seems to think not.
class Base { public: void method() {}; }; class Sub : public Base { public: void method(int p) { Base::method(); // Compiles OK method(); // Error } };
Variations on this question are among the most common questions about C++. What we are looking for is a clear explanation of why the language rules are as they are. And in case you too are in doubt, the compiler is right. FG
I'm using CodeWarrior. I cannot find the bool.h file, so I can't run any program that uses a boolean. It must be an installation problem. Can anyone send me a copy of the bool.h file?
Note the source of the question. Now to answer you will need to know something about the latest C Standard. FG
How does this code work? In other words why does the output print out a value when operator<< does not exist for Point objects
#include <iostream> using namespace std; class Point { protected: double crd[3]; public: Point(double,double,double); operator double* (); operator double const* () const; }; Point::Point(double x, double y, double z) { crd[0]=x; crd[1]=y; crd[2]=z; } Point::operator double* () {return crd;} Point::operator double const* () const { return crd; } int main() { Point p(1,2,3); cout<< "y-component of p is " <<p[1] << endl; return 0; }
Note that this questions was motivated by trying to understand some published code. You might feel the need to comment on the technique as well as why it works. FG
Q4. Contributed by Alan Lenton <alan@ibgames.net>
Hi Mr. Lenton (if you would like me to use some other name, let me know, and I will).
I'm trying to do my programming homework, and I'm not good at number theory, but I've been trying. This is the assignment. We take a long int (up to 10 digits) and have the number displayed with commas. For instance, 123456789 should be displayed as 123,456,789.
I got it to work with a number up to 9 digits, but then my code goes haywire when it gets to 10 digits. If you're not too busy, I'd appreciate any help you can give me. If you have suggestions as to make the code cleaner, I'd appreciate them also. Like I said, I tried my best with this, so it might look haphazard. Also, this is in a function - I'm not including main.
Here goes:
#include <iostream.h> void displaywithcommas(long int num){ int set, set2, set3; if (num < 1000) // no commas if number is less than 1000 cout << num << endl; else if (num >= 1000 && num < 1000000){ set = num%1000; num-=set; num/=1000; cout << num <<","<< set << endl; } else if (num >= 1000000 && num < 1000000000){ set = num%1000; set2 = ((num%1000000)-set)/1000; num-=set; num/=1000000; cout<<num<< "," <<set2<< "," <<set<<endl; } else if (num >=1000000000){ set = num%1000; set2 = ((num%1000000)-set)/1000; set3 = ((num%1000000000)-set2)/1000; num-=set; num/=1000000000; cout << num << "," << set3 << "," << set2 << "," << set << endl; } }
Once again, I'd appreciate any help you can give me.
Well Alan answered the original enquiry (like authors, game designers get asked for help), but what would you have suggested? FG
Notes:
More fields may be available via dynamicdata ..