473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.

Feb 18 '06 #1
9 1680
* 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?
Feb 18 '06 #2
"Brian" <br************@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.

Feb 18 '06 #3
On 17 Feb 2006 22:21:05 -0800, "Brian" <br************@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
Feb 18 '06 #4
> 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.

Feb 18 '06 #5
"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?
Feb 18 '06 #6
TB
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
Feb 18 '06 #7
> 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.

Feb 18 '06 #8

"Brian" <br************@gmail.com> wrote in message
news:11**********************@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.
Feb 19 '06 #9
In article <Jz*****************@fe04.lga>,
"Jim Langston" <ta*******@rocketmail.com> wrote:
"Brian" <br************@gmail.com> wrote in message
news:11**********************@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.
Feb 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Philip Ronan | last post by:
OK, here's my 2p worth: === Q. Why am I getting the error message 'Headers already sent'? A. PHP produces this error message when you try to set a header for a web page after you have already...
2
by: dont bother | last post by:
Hi, I have done some string concatenation: feature_vector={} feature_element={} #inside a loop if (v==0): feature_element=str(1)+"
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
1
by: Don Leverton | last post by:
Hi Folks, I *can* calculate the Gross Profit Percentage where both the Cost and SellPrice are known, using the formula: (SellPrice - Cost) / SellPrice = GPP eg ($24.92 - $14.95) / $24.92 =...
2
cassbiz
by: cassbiz | last post by:
I am using strtotime and I have read up on some examples and am getting the wrong output, it jumps by several days instead of one day at a time. Ultimately what I am trying to accomplish is to set...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
2
by: hpbrothers | last post by:
#include<iostream.h> void main() { char mstr; char cstr; int c=1,k=0,i,j; cout<<"enter main string"; cin.getline(mstr,100); for(i=0;mstr!='\0';i++) {
3
dmjpro
by: dmjpro | last post by:
I am testing the value of document.body.offsetWidth in Mozilla and IE. IE gets the desired value but Mozilla is not getting the desired value. Actually what happens, i want to position an element...
4
by: ahmurad | last post by:
Dear Brothers, I am struggling the following four Date-Time type values which were inputted into MYSQL database in different tables. As MYSQL Default Time Format: YYYY-MM-DD HH:MM:SS, So I used...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.