jasonRose 0 #1 October 2, 2007 Ok I suck I should know this but I got it wrong on a quiz. I hate programming but I do respect those who are good at it!! Help please.... Question 1 One can declare the whole number 4 as two different data types like so: int Afour = 4; double Bfour = 4; When is a variable of type int needed instead of type double? My answer:(to store numbers with larger magnitude and finer detail) Question 2 5. Why would a programmer want to use extra () parenthesis in his/her program? My answer: (to make the expression clearer) Some day I will have the best staff in the world!!! Quote Share this post Link to post Share on other sites
piratemike 0 #2 October 2, 2007 I'm not sure what they are looking for in question 1. My answer would be that I need the type int when Im on small systems, where multiplying two longs together can equal more than the max var size for the system, therefor, multiplying together a long and a int will be less than a double long. But I doubt that is the right answer. Question 2: another possible use of a extra parenthesis would be to make order of operations calculations correct. Some compilers honer the order of operations, some do not. example: a * b + c (a * b) +c != a * (b + c) Quote Share this post Link to post Share on other sites
bigway 4 #3 October 2, 2007 Wow.... simply wow. .Karnage Krew Gear Store . Quote Share this post Link to post Share on other sites
jasonRose 0 #4 October 2, 2007 Dang that what I ment .... To clarify the order of operations. I am not sure what they are talking about for the first problem either. Some day I will have the best staff in the world!!! Quote Share this post Link to post Share on other sites
tweak 0 #5 October 2, 2007 First question 2: Both your answer and piratemike's answer would be correct. Sometimes I use extra parenthesis to make the expression clearer and to make sure that the order of operations is enforced. With Java the order of operations is guaranteed but, as piratemike said, with C++ it all depends on the implementation of the compiler as to what gets evaluated and in what order. Most implementations usually follow the correct mathematical order of operations with the standard mathematical operators but with logical operations you can't assume complex operations will be evaluated from left to right (i.e. short circuiting). Also the oddball ?: operation (which I use a lot) can be rather squirrely sometimes when it comes to evaluating non mathmatical expressions (e.g. object comparisons, function/method return values, etc...). As far as question 1, I have to agree with piratemike again. There are many answers to that question. One answer is if the variable will be used in calculations that will yield a value other than a whole number without having to explicitly having to cast. Other than that the other worry would be if you were going to assign a double value or a larger than int value to it but that's streching things a bit since the question seems to revolve around the declaration and not the use of the variable. I have to say question 1 is very poorly worded. Awfully vague. Good luck whith that one. Quote Share this post Link to post Share on other sites
bob.dino 1 #6 October 2, 2007 1. int is for integers. double is double-precision floating point. 2. Precedence. If those answers don't enlighten you, go get a good book on C++ and spend time with it. Effective C++ by Scott Myers is one such. C++ is not an easy language and if you're struggling with such relatively simple concepts you need to spend significantly more time on the fundamentals of computer science - an understanding of the two questions you asked are fundamental to effective programming in many languages. You won't be writing Java or C# without understanding 'em. edit: I misread q2 and thought it was on about { }. Changed. Quote Share this post Link to post Share on other sites
bob.dino 1 #7 October 2, 2007 Quote...with C++ it all depends on the implementation of the compiler as to what gets evaluated and in what order. You sure? My copy of The C++ Programming Language has a table of operator precedence. (Section 6.2; p120 in the 3rd ed). Quote Share this post Link to post Share on other sites
tweak 0 #8 October 2, 2007 Regarding mathematical or logical order? I'd have to agree with you on the mathematical after thinking about it now. I'm probably just a little gunshy from C and the loose standards from long ago. I'm still pretty sure that the standard doesn't mandate that logical operations have to be evaluated from left to right. I could be wrong or could be remembering older standards but I sure I remember reading several warnings about short circuiting in C++. God, I must be getting old... I used to know these rules inside and out. Quote Share this post Link to post Share on other sites
bob.dino 1 #9 October 2, 2007 Quote God, I must be getting old... I used to know these rules inside and out. There's better things to use your brain for. I always use brackets to make precedence obvious, because it makes understanding the code at a later date so much easier. And since debugging is twice as hard as writing code, that's a damn good thing. Quote Share this post Link to post Share on other sites
tweak 0 #10 October 2, 2007 QuoteThere's better things to use your brain for. Yep, that's one reason I got lazy - working with the same technology/platform for so long. I thought I'd never make a major switch from that (UNIX/C++). But now I've taken a perm position in a Microsoft (.NET) shop. Almost like starting over again... .NET is kinda cool at first glance (compared to J2EE stuff anyway). Quote And since debugging is twice as hard as writing code, that's a damn good thing. Yep, agreed... Debugging a real biotch of a bug sux, but really gratifying to find/fix. Quote Share this post Link to post Share on other sites
vpozzoli 0 #11 October 2, 2007 QuoteOk I suck I should know this but I got it wrong on a quiz. I hate programming but I do respect those who are good at it!! Help please.... Question 1 One can declare the whole number 4 as two different data types like so: int Afour = 4; double Bfour = 4; When is a variable of type int needed instead of type double? My answer:(to store numbers with larger magnitude and finer detail) Question 2 5. Why would a programmer want to use extra () parenthesis in his/her program? My answer: (to make the expression clearer) Ok here's my take on it, for what it's worth. Question 1 must be some sort of trick question. The first answer that comes to mind is: you use a variable of type int when you need to store an integer, a variable of type double when you need to store a floating point number with that kind of precision, but that seems too obvious. Also technically you can store an integer number in a variable of type double, you'll just have to explicitly round everything off, whereas by using an int you get automatic rounding (that is, if you are actually mixing integers and floats in your expressions, otherwise there is nothing to round off to begin with). Definitely a tricky question. As for question 2 I totally agree with your answer, i.e. to improve readability. It cannot be in order to enforce the order of the operations as in that case the parentheses wouldn't qualify as "extra", being as it is that removing them will change the result makes them part of the expression and not something added as "extra" by the programmer. And yes, operator order and precedence is mandated, both in vanilla C and C++, nothing is left to the compiler implementation. That is, unless you have a non-standard compiler, in which case you'll just have to refer to your compiler manual for that. Cheers, Vale Quote Share this post Link to post Share on other sites
Erroll 80 #12 October 2, 2007 Quote1. int is for integers. double is double-precision floating point. While the above statement is technically correct, I don't think that it answers the actual question that was posed. (I don't know C++) - Are both definitions (implicitly)signed? Quote Share this post Link to post Share on other sites
Freefly710 0 #13 October 2, 2007 i always write the small, engineering programs that i use in the MATLAB software..... little easier... lots of built-in-functions and such.. Quote Share this post Link to post Share on other sites
DontPanic 0 #14 October 2, 2007 Quote When is a variable of type int needed instead of type double? My answer:(to store numbers with larger magnitude and finer detail) I would answer: When you want to constrain a variable to interger values, and not allow the variable to ever become floating point. There are many instances where it makes sense to constrain a variable to whole integers, and where you wouldn't want/need to allow for floating point numbers. For instance, when controlling a loop, such as: for (i=0; i<10; i++) Quote Share this post Link to post Share on other sites
MrBrant 0 #15 October 2, 2007 In the embedded world, an int is usually a 16 bit number. (integer) So, if you need to store a number larger than 65535, (or, during the course of a calculation, your result at any stage will exceed that) you'll need to use a Long. (i think sometimes called a double - I always just use long). A long can usually either be 24bits (16.7 million), or 32 bits (4 billion) - depending on your compiler and the options you set in your preprocessor directives. All of these can be unsigned (positive only) as the examples given above, or signed (+ or -). For signed variables, basically divide your range by 2. (and if memory serves me right, the negative number will be 1 number "longer" than the positive number. i.e. a signed INT is -32768 to +32767 I think) Somebody mentioned doubles as floating point number - I don't think this is the case - in the embedded world anyway. A "float" is usually the only data type that is floating-point - and tend to be 32 bit I believe - I'd have to check on that one. I don't usually use them - takes too much memory and time (instruction cycles). Ever try to do floating point division running at 32kHz? I'm not sure how any of that translates to the C++ computer world. As for your extra brackets. As was mentioned - it's a good habit to get into. Some quasi-C compilers might not follow the order of operations. They also help to make your expressions clearer for reading - so I would agree with your answer. (even if it wasn't the one they were looking for) Quote Share this post Link to post Share on other sites
bob.dino 1 #16 October 2, 2007 QuoteQuote1. int is for integers. double is double-precision floating point. While the above statement is technically correct, I don't think that it answers the actual question that was posed. It answers the C++ part of the question. Knowing the difference between an integer and a double-precision floating point number is not a C++ problem. It's part of the most basic fundamentals of computer science. If the OP/you/anyone on this thread is unclear on the difference, he/you/they really need to sit down with Google or a good book and do a lot of reading. Me giving him the answer here won't help him to help himself, which is the object of the exercise. Quote Share this post Link to post Share on other sites
jasonRose 0 #17 October 2, 2007 Quotei always write the small, engineering programs that i use in the MATLAB software..... little easier... lots of built-in-functions and such.. I am with you!! I have used MATLAB and SCILAB and found them relatively easy to operate unfortunatly I have dicked the dog and never took C++ and have gotten away with it. Now they are telling me I have to complete the course or no graduation for me.... Thanks all for your help!! Some day I will have the best staff in the world!!! Quote Share this post Link to post Share on other sites
bob.dino 1 #18 October 2, 2007 QuoteIn the embedded world, an int is usually a 16 bit number. (integer) Depends on your processor. Not on a PowerPC, for example. Quote(i think sometimes called a double - I always just use long). They're fundamentally different. One is a long integer and one is a double precision floating-point number. QuoteA long can usually either be 24bits (16.7 million), or 32 bits (4 billion) - depending on your compiler and the options you set in your preprocessor directives. As you say, that depends on your compiler. The spec only mandates that a long int is at least as large as an int. long is a synonym for long int. Microsoft Visual C++ 2002, for example, uses 4 bytes for both an int and a long. QuoteSomebody mentioned doubles as floating point number - I don't think this is the case - in the embedded world anyway. Unless the C++ spec is being ignored in your space (and that's quite possible), a double must be a floating point number. More here. C may be different - I don't know what's mandated by the spec wrt data types. Quote Share this post Link to post Share on other sites
d123 3 #19 October 3, 2007 You guys are a bunch of geeks and this is a typical C question not C++ ... wait or is it --(C++) ? Lock, Dock and Two Smoking Barrelrolls! Quote Share this post Link to post Share on other sites
opurt 0 #20 October 3, 2007 1) You want to use an int if you are comparing numbers. Decimal numbers are not stored precisely in the computer (IEEE 754). Integers are. If you ever do an if (a == b), you must use ONLY INTEGERS. Floating point numbers are subject to rounding errors. Integers are not. 2) Order of operations. Don't be too clever. Use parentheses to explicitly show how you expect the operation to proceed. Quote Share this post Link to post Share on other sites
MrBrant 0 #21 October 3, 2007 Quote Quote In the embedded world, an int is usually a 16 bit number. (integer) Depends on your processor. Not on a PowerPC, for example. Good catch. I apologize for the generalization. As an egotistical bastard, I should have said "My embedded world" - which mainly means 8-bit micro's - predominantly Microchip. Quote Quote A long can usually either be 24bits (16.7 million), or 32 bits (4 billion) - depending on your compiler and the options you set in your preprocessor directives. As you say, that depends on your compiler. The spec only mandates that a long int is at least as large as an int. long is a synonym for long int. Microsoft Visual C++ 2002, for example, uses 4 bytes for both an int and a long. Ah, now that is interesting. I didn't know that. I wonder if it's the same for the ANSI C standard. Quote Quote (i think sometimes called a double - I always just use long). They're fundamentally different. One is a long integer and one is a double precision floating-point number. Quote Somebody mentioned doubles as floating point number - I don't think this is the case - in the embedded world anyway. Unless the C++ spec is being ignored in your space (and that's quite possible), a double must be a floating point number. More here. C may be different - I don't know what's mandated by the spec wrt data types. Hummmm. it appears that I may have to retract a good portion of my last statements. As I said earlier, floating point operations can be pretty impractical for most of my stuff - It appears that I have slacked off learning (well, remembering anyway ) most of that "useless" crap. Thanks for enlightening me (and motivating me to do my own searching ). Sorry to everybody else for some of the misleading info in my last post. Quote Share this post Link to post Share on other sites
Derekbox 0 #22 October 3, 2007 These are simple questions - simple answers. 1) int is strictly for a whole number (-1,0,2,3) double is a floating point (decimel) number (1.1, 1.0, -6.3, 122.4) etc. 2) I think the answer you need like metioned before is precendce, but think forcing order of operation for math. Ex: 1 / 5 + 3 * 2 = 6.2 Is different than 1 / (5 + 3) * 2 = 0.25 Is different than 1 / ((5 + 3) * 2) = 0.0625 Quote Share this post Link to post Share on other sites