Hi,
I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r s) return r;
else
return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}
Any ideas ?
Thx in advans,
Karthik Balaguru 9 1212
On 2007-09-03 11:32, karthikbalaguru wrote:
Hi,
I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r s) return r;
else
return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
This sets the value of m to 10, what did you think the '=' meant?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
Here you increase the value of m by one, what else did you expect the
'++' to do?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}
Any ideas ?
Read up on references, you clearly have not understood what they are.
--
Erik Wikström
karthikbalaguru wrote:
Hi,
I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r s) return r;
else
return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}
Any ideas ?
Thx in advans,
Karthik Balaguru
Hey, I got it correct now.
biggest(k,m) returns m as &m which is an alias for m and so m gets
modified to 10 .
and the same thing happens with biggest(k,m)++ and so the m gets
incremented to 11.
Thx,
Karthik Balaguru
Erik Wikström wrote:
On 2007-09-03 11:32, karthikbalaguru wrote:
Hi,
I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r s) return r;
else
return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
This sets the value of m to 10, what did you think the '=' meant?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
cout << endl;
biggest (k, m) ++;
Here you increase the value of m by one, what else did you expect the
'++' to do?
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
cout << endl;
return 0;
}
Any ideas ?
Read up on references, you clearly have not understood what they are.
--
Erik Wikström
Apologies, there was some misunderstanding. :(:(
I did not look into it properly. :(:(
Karthik Balaguru
karthikbalaguru wrote:
Hi,
I wonder how the 'm' value is 10 in the program below . It should be
7.
Further, it turns later to 11. Strange. :( How is it possible ? Why ?
Need clarification. Any ideas ?
using namespace std;
#include <iostream>
double &biggest (double &r, double &s)
{
if (r s) return r;
else
return s;
}
int main ()
{
double k = 3;
double m = 7;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 7
cout << endl;
biggest (k, m) = 10;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 10 /* How is this
possible . m was actually 7. how it prints 10 */
10 is right, what else you expect?
cout << endl;
biggest (k, m) ++;
cout << "k: " << k << endl; // Displays 3
cout << "m: " << m << endl; // Displays 11 /* How is it 11
here */
11 is right too.
cout << endl;
return 0;
}
Any ideas ?
biggest() always return a reference to m, as m is always bigger than k
changes made to the returned reference (m), will directly affect referee
m. which is a reference do.
see cout you use
cout << ".." << ".." is also the case,
which is changing your buffer in the output stream that cout references
--
Thanks
Barry
On Sep 3, 12:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-03 11:32, karthikbalaguru wrote:
biggest (k, m) ++;
Here you increase the value of m by one, what else did you expect the
'++' to do?
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????
thanks,
FM.
On 2007-09-03 19:17, terminator wrote:
On Sep 3, 12:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-09-03 11:32, karthikbalaguru wrote:
biggest (k, m) ++;
Here you increase the value of m by one, what else did you expect the '++' to do?
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????
It can be used on any arithmetic (integral or floating-point) type.
--
Erik Wikström
terminator <fa***********@gmail.comwrote in message
Here you increase the value of m by one, what else did you expect the
'++' to do?
/* """
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????
""" */
for( double v( 1.0 ); v <= 3; ++v ){
std::cout<<"v = "<< v <<std::endl;
} // for(v)
/* - output -
v = 1.000000
v = 2.000000
v = 3.000000
*/
I was surprised it worked when I tried it.
--
Bob R
POVrookie
On Mon, 03 Sep 2007 20:15:44 GMT, Erik Wikström
<Er***********@telia.comwrote in comp.lang.c++:
On 2007-09-03 19:17, terminator wrote:
On Sep 3, 12:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-03 11:32, karthikbalaguru wrote:
biggest (k, m) ++;
Here you increase the value of m by one, what else did you expect the
'++' to do?
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????
It can be used on any arithmetic (integral or floating-point) type.
Actually it can be used to any arithmetic type or pointers to any
completely defined types.
--
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.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
On Sep 4, 4:08 am, Jack Klein <jackkl...@spamcop.netwrote:
On Mon, 03 Sep 2007 20:15:44 GMT, Erik Wikström
<Erik-wikst...@telia.comwrote in comp.lang.c++:
On 2007-09-03 19:17, terminator wrote:
On Sep 3, 12:42 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>On 2007-09-03 11:32, karthikbalaguru wrote:
biggest (k, m) ++;
>Here you increase the value of m by one, what else did you expect the
>'++' to do?
hey,incing a double???
I used to think that '++' operates on integral types.Were I
wrong ?????
It can be used on any arithmetic (integral or floating-point) type.
Actually it can be used to any arithmetic type or pointers to any
completely defined types.
pointers look much like integrals indeed.but double/float was news to
me.
thanks every 1,
FM This thread has been closed and replies have been disabled. Please start a new discussion. |