Not getting desired output

Discussion in 'C++' started by Brian, Feb 18, 2006.

  1. Brian

    Brian Guest

    So I have this:

    double x = .25;
    double y = .5;
    double z = .25;
    double probability;
    int index;

    while(index < 30)
    {
    probability = (1/2)(y/(y+z));
    index = index++;
    cout << index << " " << probability << endl;
    }

    return 0;
    }

    I am getting 0 for probability for some reason. Considering x,z = .25
    and y = .5 I dont see how I am getting a 0.
    Brian, Feb 18, 2006
    #1
    1. Advertising

  2. * Brian:
    > So I have this:
    >
    > double x = .25;
    > double y = .5;
    > double z = .25;
    > double probability;
    > int index;
    >
    > while(index < 30)
    > {
    > probability = (1/2)(y/(y+z));
    > index = index++;
    > cout << index << " " << probability << endl;
    > }
    >
    > return 0;
    > }
    >
    > I am getting 0 for probability for some reason. Considering x,z = .25
    > and y = .5 I dont see how I am getting a 0.


    1. Error. You're using an uninitialized variable 'index'. Most likely
    your compiler is issuing a warning about that. The effect is Undefined
    Behavior.

    2. Style. 'probability' should be declared locally in the loop and it
    should be 'const'.

    3. Error (logical). The expression 1/2 evaluates to zero.

    4. Error (syntax). Lacking '*'. C++ does not support implicit
    multiplication.

    5. Error (syntax). Unmatched parenthesis.

    6. Error. You modify the variable 'index' twice with no intervening
    sequence point, yielding Undefined Behavior. Replace with '++index'.

    7. Style. The loop should be a 'for' loop.

    8. Style. Don't write '.25', write '0.25'.

    9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    'using namespace std'.

    10. Style. Don't write everything at the same level of indentation, let
    indentation reflect the logical structure of the code (or perhaps
    investigate how to post news articles, if it's the posting interface
    that has removed indentation).

    11. Error. You have posted something that isn't your actual code,
    something that doesn't compile and hence can not produce the result you
    claim, i.e., you're effectively lying to the group (perhaps
    unintentional, but still it's lying). See the FAQ on how to to post.

    Hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 18, 2006
    #2
    1. Advertising

  3. Brian

    Marco Wahl Guest

    "Brian" <> writes:

    > So I have this:
    >
    > double x = .25;
    > double y = .5;
    > double z = .25;
    > double probability;
    > int index;
    >
    > while(index < 30)
    > {
    > probability = (1/2)(y/(y+z));
    > index = index++;
    > cout << index << " " << probability << endl;
    > }
    >
    > return 0;
    > }
    >
    > I am getting 0 for probability for some reason. Considering x,z = .25
    > and y = .5 I dont see how I am getting a 0.


    I slightly modified and commented your code and tried to compile it on
    my computer.

    The program is

    [[[
    #include <iostream>
    using namespace std;
    int main()
    {
    double x = .25;
    double y = .5;
    double z = .25;
    double probability; // Will this variable be initialized?
    int index; // Will this variable be initialized?
    while(index < 30)
    {
    probability = (1/2)(y/(y+z)); // 0(y/(y+z)) really wanted? ;-)
    index = index++; // Redundance or even danger here?
    cout << index << " " << probability << endl;
    }
    return 0;
    }
    ]]]

    and the compiler-run gives

    [[[
    g++ -c a.cpp -o a.o
    a.cpp: In function ‘int main()’:
    a.cpp:12: error: ‘0’ cannot be used as a function
    ]]]

    I just saw Alfs post which looks good. Hopefully my posting is of
    some use anyway.
    Marco Wahl, Feb 18, 2006
    #3
  4. Brian

    Jack Klein Guest

    On 17 Feb 2006 22:21:05 -0800, "Brian" <>
    wrote in comp.lang.c++:

    > So I have this:
    >
    > double x = .25;
    > double y = .5;
    > double z = .25;
    > double probability;
    > int index;
    >
    > while(index < 30)
    > {
    > probability = (1/2)(y/(y+z));

    ^^^^^

    Aside from the other two posts, you are always going to get a result
    of 0. The '1' and '2' in the expression "1/2" are integer literals.
    That means that the program will perform integer division, which
    discards any remainder. In integer division, 1/2 results in 0.

    The type of an arithmetic operation in C++ depends completely on the
    types of its operands. Regardless of what you might want to do with
    the result of the operation afterwards.

    Change this to 1.0/2.0, or even better rewrite the right hand side of
    the assignment as:

    (y/(y+z))/2;

    Note in this case it is not necessary to write 2.0. The subexpression
    (y/(y+x)) has type double, and this will cause automatic promotion of
    the int value 2 to the double value 2.0.


    > index = index++;
    > cout << index << " " << probability << endl;
    > }
    >
    > return 0;
    > }
    >
    > I am getting 0 for probability for some reason. Considering x,z = .25
    > and y = .5 I dont see how I am getting a 0.


    ....and fix all the things that Alf pointed out.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Feb 18, 2006
    #4
  5. Brian

    Brian Guest

    > 9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    > 'using namespace std'.

    Why is this necessary? I've been writting cout for every program I've
    written and haven't run into any problems. Can you explain why this is
    better than just cout?
    >
    > 11. Error. You have posted something that isn't your actual code,
    > something that doesn't compile and hence can not produce the result you
    > claim, i.e., you're effectively lying to the group (perhaps
    > unintentional, but still it's lying). See the FAQ on how to to post.

    My appologies I was trying to keep it short and to the point.

    I understand all the other comments, thanks for the help guys.
    Brian, Feb 18, 2006
    #5
  6. Brian

    osmium Guest

    "Brian" writes:

    >> 9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    >> 'using namespace std'.

    > Why is this necessary? I've been writting cout for every program I've
    > written and haven't run into any problems. Can you explain why this is
    > better than just cout?


    Because someday you may be a programmer in a project that has 1,000
    programmers and the main application is 100 MB and it incorporates 57
    libraries of various sorts picked up from around the world. And you are too
    damned dense to realize that the environment has changed and you won't adapt
    properly and can't even be re-trained. Can't you SEE that?
    osmium, Feb 18, 2006
    #6
  7. Brian

    TB Guest

    Brian sade:
    >> 9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    >> 'using namespace std'.

    > Why is this necessary? I've been writting cout for every program I've
    > written and haven't run into any problems. Can you explain why this is
    > better than just cout?


    'using namespace std' causes heavy pollution in the global* namespace.
    If you don't want to write 'std::cout', then import only the necessary
    names, e.g.:

    using std::cout;

    Namespaces exists for a purpose. But of course, in the end, it's
    your decision. As he wrote, it's about style.

    *Actually, the statement pollutes any namespace that contains it.

    --
    TB @ SWEDEN
    TB, Feb 18, 2006
    #7
  8. Brian

    Brian Guest

    > Because someday you may be a programmer in a project that has 1,000
    > programmers and the main application is 100 MB and it incorporates 57
    > libraries of various sorts picked up from around the world. And you are too
    > damned dense to realize that the environment has changed and you won't adapt
    > properly and can't even be re-trained. Can't you SEE that?


    I do see that. I will change all my couts to std::cout then. Thank you.
    Brian, Feb 18, 2006
    #8
  9. Brian

    Jim Langston Guest

    "Brian" <> wrote in message
    news:...
    >> 9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    >> 'using namespace std'.

    > Why is this necessary? I've been writting cout for every program I've
    > written and haven't run into any problems. Can you explain why this is
    > better than just cout?


    Namespaces were designed into C++ to fix the problem of name collision. Say
    you decided to make a class called string (not neccessarily a good idea, but
    just to show my point). Something like this (the #include of <cmath> is
    just so it'll compile with the using statement).

    #include <cmath>
    using namespace std;
    class string
    {
    public:
    char data[1000];
    };

    int main()
    {
    string MyString;
    }

    Seems simple enough, and compiles. Now, however, say I want a vector of my
    string class. I would attempt to do this:

    #include <vector>
    using namespace std;
    class string
    {
    public:
    char data[1000];
    };

    int main()
    {
    vector< string > MyVector;
    }

    But, it doesn't compile (in VC++ .net 2003 anyway). The error I get is
    this:

    c:\Source\working\Console2\Console2.cpp(14) : error C2872: 'string' :
    ambiguous symbol
    could be 'c:\Source\working\Console2\Console2.cpp(6) : string'
    or 'c:\Program Files\Microsoft Visual Studio .NET
    2003\Vc7\include\xstring(1562) : std::string'

    That's a name collision. <vector> also includes <string> somewhere, which
    is the std::string. Now the compiler doesn't know which vector< string >
    I'm talking about. Is it std::string or my local string which is in the
    unnamed namespace? To fix it I would have to:

    vector< ::string > MyVector;

    saying use the string from the unnamed namespace. But, to get rid of all
    this finagelling, just don't import every single thing from the std it finds
    and either just import the ones you want to use or prefix them with std::.
    Personally, I prefix everything with std::, it's not really that difficult.
    Which makes the program become:

    #include <vector>

    class string
    {
    public:
    char data[1000];
    };

    int main()
    {
    std::vector< string > MyVector;
    }

    Please note, I am not suggesting in the least that you make classes with the
    same name as templates/classes in the STL. I just used string cause it was
    easiest to show.

    Namespaces were designed to avoid name collision. using namespace whatever
    totally defeats the purpose.
    Jim Langston, Feb 19, 2006
    #9
  10. Brian

    Daniel T. Guest

    In article <JzOJf.3480$>,
    "Jim Langston" <> wrote:

    > "Brian" <> wrote in message
    > news:...
    > >> 9. Style. Don't write 'cout', write 'std::cout' and get rid of any
    > >> 'using namespace std'.

    > > Why is this necessary? I've been writting cout for every program I've
    > > written and haven't run into any problems. Can you explain why this is
    > > better than just cout?

    >
    > Namespaces were designed into C++ to fix the problem of name collision. Say
    > you decided to make a class called string (not neccessarily a good idea, but
    > just to show my point). Something like this (the #include of <cmath> is
    > just so it'll compile with the using statement).
    >
    > #include <cmath>
    > using namespace std;
    > class string
    > {
    > public:
    > char data[1000];
    > };
    >
    > int main()
    > {
    > string MyString;
    > }
    >
    > Seems simple enough, and compiles. Now, however, say I want a vector of my
    > string class. I would attempt to do this:
    >
    > #include <vector>
    > using namespace std;
    > class string
    > {
    > public:
    > char data[1000];
    > };
    >
    > int main()
    > {
    > vector< string > MyVector;
    > }
    >
    > But, it doesn't compile (in VC++ .net 2003 anyway). The error I get is
    > this:
    >
    > c:\Source\working\Console2\Console2.cpp(14) : error C2872: 'string' :
    > ambiguous symbol
    > could be 'c:\Source\working\Console2\Console2.cpp(6) : string'
    > or 'c:\Program Files\Microsoft Visual Studio .NET
    > 2003\Vc7\include\xstring(1562) : std::string'
    >
    > That's a name collision. <vector> also includes <string> somewhere, which
    > is the std::string. Now the compiler doesn't know which vector< string >
    > I'm talking about.


    And in that rare instance, you replace the "using namespace std", with
    "using std::vector" and all is once again well with the world...

    What's the big deal? If YAGNI, then don't bother. However, *never* put
    using namespace std in the global scope of a header file, fixing it
    becomes much more complicated...


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Feb 19, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chuck amadi
    Replies:
    1
    Views:
    478
    Larry Bates
    Jun 23, 2004
  2. Replies:
    0
    Views:
    468
  3. Vasu
    Replies:
    24
    Views:
    845
    Daniel Pitts
    Nov 29, 2008
  4. bg
    Replies:
    0
    Views:
    97
  5. Jack
    Replies:
    2
    Views:
    239
Loading...

Share This Page