C++

Discussion in 'Scripting & Programming' started by Mof, Jul 10, 2008.

  1. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Lol!

    Am too busy to reply fully right now... :duel
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  2. The Zig

    The Zig Kilobyte Poster

    305
    19
    46
    Hey,
    Been reading this thread with interest. It's been a while since I did any C++ as part of an electronics degree. However, I always liked C programming and I'm looking to start it up again.

    Anyone recommend a compiler?
    Cheers!
     
    Certifications: A+; Network+; Security+, CTT+; MCDST; 4 x MTA (Networking, OS, Security & Server); MCITP - Enterprise Desktop Support; MCITP - Enterprise Desktop Administrator; MCITP - Server Administrator; MCSA - Server 2008; MCT; IOSH; CCENT
    WIP: CCNA; Server 2012; LPIC; JNCIA?
  3. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Well, that isn't the way I saw it. It looked completely out-of-context, and looking back at what was explained before, it is!


    First off, the '3.25e0f' abominiation is not my code. I pointed out the error in the most vague way possible to let Mof solve it for himself and actually learn a fair bit in the process.

    Code:
    float aFloat = 1.75;
    
    This is an example of an implicit cast from double to float.
    Code:
    float aFloat = 1.75e0;
    
    No casting going on here because the literal is in a float form.

    You are aware that you are suggesting that Microsoft are supporting a cast from a larger type to a smaller type as standard? You should be able to work out that this can't be true.

    No, it's required when you want your programs to be right and function correctly! :blink

    I think I've explained already what you've said that's wrong.

    Nope, the mistake wasn't deliberate. I was coding whilst doing it and was more focussed on my work.

    Seeing as you're rattling on about coding for 14+ years, I can rattle on about being one of the top 10-25% programmers on the planet because of the work I do and where I work. 8) But moving on...

    For your level of experience I am surprised that you haven't covered a lot of what has been explained here. And for the love of 'whatever' please stop linking to wikipedia to explain yourself! :x
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  4. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Opps double post !
     
  5. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    It is not out of context, in order to understand the IEEE floating point spec you must first understand the other topics. Both the mantissa and the exponent use a form of twos complement.

    You changed it to 3.25e1 effectively multiplying the constants by a factor of ten, so its no longer an equivalent example. I merely pointed out that you must either remove the exponent or change the exponent to e0 for it to be functionaly the same.

    Try and be open minded and listen, I was talking about the in memory representations and how computers process numbers. I'm was not generally talking about source code. Can you please try to leave your ego at the door ? :D

    Well i'm still not clear, its more helpful to give specific constructive critism, maybe even explain why, even provide some examples, rather than simply say 'you are wrong'.

    I was responding to your earlier point that I had overblown a simple exercise, my point is if you really want to know how computers work you need to know this stuff and its not advanced. I was also trying to assure you that I'm not completely ignorant on this topic despite your seemingly offhand remarks. I was also making the point that in some areas my knowledge is hazy due to sheer time !

    I think its a useful constructive way to teach people stuff without writing a post 200+ lines long, it helps them put the information in context, thats the point of the web no? To be a hypertext resource? You don't have to follow the links if you don't want to, others might enjoy learning this stuff in context. Anyway I've not linked to anything on this post so you can relax ! :D

    This might help get things back on track, heres an example to demonstrate some of the points :-

    #include "stdafx.h"
    #include <stdio.h>
    #include <iostream>
    using namespace std;

    int main()
    {
    float float_pi=0.0f; // declared and initalized
    double double_pi=3.14159265358979323846; // declared and initalized

    cout << sizeof(3.14) << endl; // double by default
    cout << sizeof(3.14f) << endl; // now a float

    float_pi = double_pi; // Potential unsafe loss of precision implicit cast, this is a warning not an error !

    printf("&#37;2.20f\n", double_pi); // Shows small loss of precision from value in source
    printf("%2.20f\n", float_pi); // Shows bigger loss of precision

    float_pi=3.14159265358979323846;
    printf("%2.20f\n", float_pi); // Shows bigger loss of precision

    return 0;
    }

    (Its a default MS VC++ console app with precompiled headers, hence the stdafx.h, you should be able to remove this easily.)
     
  6. Mof

    Mof Megabyte Poster

    526
    2
    49
    Blimey I only left work to go home and look what happens.
     
    WIP: C++ and A+
  7. Mof

    Mof Megabyte Poster

    526
    2
    49
    I feel this is my fault don t think I asked the right question

    The Heading In The Book Is "Employing Variable Arrays" the example they give is as follows

    #include <iostream>

    using namespace std;

    int main()

    {
    float nums[3];
    nums[0]=1.5; nums[1]=2.75; nums [2]=3.25;
    char name[5]={'m','i','k','e','\0'};
    cout<<"nums[0]:"<<nums[0]<<endl;
    cout<<"nums[1]:"<<nums[1]<<endl;
    cout<<"nums[2]:"<<nums[2]<<endl;
    cout<<"name[0]:"<<name[0]<<endl;
    cout<<"string:"<<name<<endl;
    cout<<"coords[0][2]:"<<endl;
    cout<<"coords[1][2]:"<<endl;
    return 0;
    }


    Now we are Employing Vector Arrays
     
    WIP: C++ and A+
  8. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    No worries mof hopefully somebody will learn something from this ! :biggrin

    Where is coords declared ? It looks like a multidimensional array.

    The STL also introduces vectors which are far more useful.

    http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR

    Basically arrays can not expand to suit the possible growth in data, STL vectors and lists can.

    This book is good
    http://www.amazon.com/Essential-Depth-Stanley-B-Lippman/dp/0201485184

    It focuses on teaching modern C++ from the beginning with STL and references and pays less attention to the baggage from C in an aim to get you up and running faster.
     
  9. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    dmarsh26,

    Every time I give advice on anything you always have to leap in with your half-knowledge, wikipedia links and weak explanations. I am endlessly having to correct things you say. Even with this thread and your very last post you still haven't understood what I'm getting at. When you down't understand you post links, possibly paraphrase what I've said and claim it to be your explanation, and just be a general pain.

    I code in C/C++ day-in, day-out, both at home and work - it's not a language that I know nothing or a little bit about.

    If you are offended when I point out your mistakes, then tough. I don't like half or incorrect information being paraded as chapter and verse.

    Who needs to leave their ego at the door, eh? :dry
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  10. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Yet another unconstructive post of flames and trolling...

    I coded professionally in C++ at home and at work for many years, however I freely admit to have barely touched it for 6 years, I still like to try to help others where I think I can, and am freely prepared to admit my limits, are you ?

    I'm not offended by constructive criticism, I'm still waiting for it, what does offend me is small minded abuse.

    Neither do I, so why not try to build a consensus and communicate your point like an adult ? Isn't that what a forums for ?

    Why not try and explain then ? Like I said, explain or post an example. Everything I've said I've tried to explain clearly and put into context.

    Mines well and truly outside... :D
     
  11. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Hey Mof, don't take it on. We appear to be having regular run-ins lately. Some don't like it when you take their perceived thrown. I'm here to give advice and not wave my ego around. Anyway... :)

    Not quite sure what this is supposed to be getting at. 'Variable arrays' and 'Vector Arrays are broad terms with a very unclear meaning.

    Variable Arrays

    Given that you didn't specify variable length arrays I'm assuming that you mean that there are arrays of elements that you can change the value of? for example if you add the following to your code:

    Code:
    name[1]= 'a';
    
    after you've printed you values than do so again, you will end up with the string 'make' instead of 'mike' being displayed. This is because all elements within the array can be changed since you didn't declare them 'const'.

    Vector Arrays

    A vector is part of the Standard Template Library (STL) and is a generic structure that permits the addition an deletion of elements, as well as iterators for performing various dynamic operations on the vector structure. All this means is that a vector can be any defined type, and because it is generic you can iterate through and do whatever the structure permits irrespective of its instantiated type.

    I don't think that the book would have moved you onto this topic yet, though. Pointers are normally covered first. :blink

    'Vector arrays' are actually arrays instantiated with vectors, but that is another story. :D
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  12. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Dude, whatever.

    I am well aware of my limits. Since when have you seen me offer any detailed advice on a language that I don't use professionally. You've only seen me offer any kind of advice on C/C++, Python and regular expressions where appropriate. I steer well clear of VB (even though I have used that as well, but don't consider myself an expert), or any other language posted about on the forums. But! As soon as anyone posts anything about any language, there you are with your links - like a bloody rash. You tell me about ego?

    'Meh 2.0'. :dry

    for the record I have been coding in C++ for nearly 14 years without a break across Windows (in its many forms) and UNIX. I have served as moderator on many programming forums, I work in games where we don't use managed code (everything is native, hence the need for detailed understanding), I could go on and on, but my fingers are getting tired. Give it a rest. :x
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  13. Mof

    Mof Megabyte Poster

    526
    2
    49
    Hi added "name[1] ='a';
    came up invalid token
     
    WIP: C++ and A+
  14. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Works for me. Did you do this?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    float nums[3];
    nums[0]=1.5; nums[1]=2.75; nums [2]=3.25;
    char name[5]={'m','i','k','e','\0'};
    cout<<"nums[0]:"<<nums[0]<<endl;
    cout<<"nums[1]:"<<nums[1]<<endl;
    cout<<"nums[2]:"<<nums[2]<<endl;
    cout<<"name[0]:"<<name[0]<<endl;
    cout<<"string:"<<name<<endl;
    cout<<"coords[0][2]:"<<endl;
    cout<<"coords[1][2]:"<<endl;
    
    name[1]= 'a';   // the change.
    
    cout<<"nums[0]:"<<nums[0]<<endl;
    cout<<"nums[1]:"<<nums[1]<<endl;
    cout<<"nums[2]:"<<nums[2]<<endl;
    cout<<"name[0]:"<<name[0]<<endl;
    cout<<"string:"<<name<<endl;
    cout<<"coords[0][2]:"<<endl;
    cout<<"coords[1][2]:"<<endl;
    
    return 0;
    }
    
    with output:

    Code:
    nums[0]:1.5
    nums[1]:2.75
    nums[2]:3.25
    name[0]:m
    string:mike
    coords[0][2]:
    coords[1][2]:
    nums[0]:1.5
    nums[1]:2.75
    nums[2]:3.25
    name[0]:m
    string:make
    coords[0][2]:
    coords[1][2]:
    Press any key to continue . . .
    
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  15. Mof

    Mof Megabyte Poster

    526
    2
    49
    i just added name[1]='a'; to the code and did nothing else but i can see now you you have to input the cout statement workks fine now.:dunce
     
    WIP: C++ and A+
  16. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    :biggrin
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  17. Mof

    Mof Megabyte Poster

    526
    2
    49
    ran this code

    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Size of float in 'bytes' is: " << sizeof(float) << endl;
    cout << "Size of double in 'bytes' is: " << sizeof(double) << endl;

    return 0;
    }

    float = 4
    double =8

    good example cheers
     
    WIP: C++ and A+
  18. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    No problem. Have a go at checking out the sizes of all the types available that you can find, including the arrays that you've created in your previous code. You should be able to spot some patterns. :)
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.
  19. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    At the risk of kicking this all off again ! Word size is generally related to the machine architecture.

    On most compilers / processors the size of the floating point types will be tied to the IEEE standard and not the size of the machine word.

    You could argue that a machine word on most systems is now 32 or 64 bits for example. Your explanation would require a 16 bit machine word.

    The compiler is free to assign the size it likes to certain types but in general there is a sort of standard.

    float = normally IEEE single float 4 bytes = 32 bits, 1 sign bit, 8 bit exponent, 23 bit mantissa.
    double = normally IEEE double 8 bytes = 64 bits, 1 sign bit, 11 bit exponent, 52 bit mantissa.
    long double = Compiler and architecture specific, commonly either just an alias for a double or possibly a 128 bit representation.
     
  20. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    This is 32-bit compiled code. A word is 16 bits. :blink

    [edit]

    From your own wikipedia link (you're starting to make me hate wikipedia!)

    Wish to shoot yourself in the foot again, dmarsh? :D

    [/edit]
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.