0
jasonRose

C++ help!!

Recommended Posts

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!!!

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.:S

Share this post


Link to post
Share on other sites
Quote

God, I must be getting old... I used to know these rules inside and out.:S



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.

Share this post


Link to post
Share on other sites
Quote

There'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.

Share this post


Link to post
Share on other sites
Quote

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)



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

Share this post


Link to post
Share on other sites
Quote

1. 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?



Share this post


Link to post
Share on other sites
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++)

Share this post


Link to post
Share on other sites

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? :ph34r:

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)

Share this post


Link to post
Share on other sites
Quote

Quote

1. 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.

Share this post


Link to post
Share on other sites
Quote

i 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!!!

Share this post


Link to post
Share on other sites
Quote

In 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.

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.

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :D) most of that "useless" crap.

Thanks for enlightening me (and motivating me to do my own searching :P).
Sorry to everybody else for some of the misleading info in my last post.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0