Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Not getting desired output (http://www.velocityreviews.com/forums/t451900-not-getting-desired-output.html)

Brian 02-18-2006 06:21 AM

Not getting desired output
 
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.


Alf P. Steinbach 02-18-2006 06:45 AM

Re: Not getting desired output
 
* 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?

Marco Wahl 02-18-2006 07:06 AM

Re: Not getting desired output
 
"Brian" <brian.digipimp@gmail.com> 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.


Jack Klein 02-18-2006 07:16 AM

Re: Not getting desired output
 
On 17 Feb 2006 22:21:05 -0800, "Brian" <brian.digipimp@gmail.com>
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/~a...FAQ-acllc.html

Brian 02-18-2006 05:58 PM

Re: Not getting desired output
 
> 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.


osmium 02-18-2006 06:44 PM

Re: Not getting desired output
 
"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?



TB 02-18-2006 06:48 PM

Re: Not getting desired output
 
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

Brian 02-18-2006 07:01 PM

Re: Not getting desired output
 
> 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.


Jim Langston 02-19-2006 12:01 AM

Re: Not getting desired output
 

"Brian" <brian.digipimp@gmail.com> wrote in message
news:1140285522.474785.242080@g44g2000cwa.googlegr oups.com...
>> 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.



Daniel T. 02-19-2006 12:45 AM

Re: Not getting desired output
 
In article <JzOJf.3480$4v7.1253@fe04.lga>,
"Jim Langston" <tazmaster@rocketmail.com> wrote:

> "Brian" <brian.digipimp@gmail.com> wrote in message
> news:1140285522.474785.242080@g44g2000cwa.googlegr oups.com...
> >> 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.


All times are GMT. The time now is 01:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.