Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 9th, 2004, 05:24 AM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help - C++ beginner

#include<iostream.h>
int main()
{
int k = 0;
int i = 2;
//try out all the cases given below one by one,,comment out the irrelevant cases
//k = ++i + i++ * ++i; // case1
//k = i++ * ++i + i++; // case2
//k = i++ * ++i + ++i; // case3
//k = i++ + ++i * i++; // case4
k = ++i + i++ * i++; // case5
cout << k << endl;
cout << i << endl;
return 0;
}

Case1 gives me 'k'= 20 How is this computed ?
Case2 to case 5 gives k = 12 how is this computed ???

I use VC++ compiler

With Thanks :)

ASK

Is it possible for me to check others postings, queries and reply from my outluk mail id,,
Reply With Quote
  #2 (permalink)  
Old July 9th, 2004, 06:00 AM
Authorized User
 
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, case 1 gives me 15. That is what I would expect;

++i (3) + i++ (3) * ++i (4)

3 + (3*4) = 15!

++i is not augmented until completion of the statement but i has the value 3 because of the leftmost incrementation.

Georges
Reply With Quote
  #3 (permalink)  
Old July 12th, 2004, 01:11 AM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thnx for ur reply,,but when I tried the same code chunk ( needless to say using a vc++ compiler) I recvd K= 20 for case1 alone and in all the other case viz 2 -5 K yielded 12. Did u try the code in any of the compilers or was ur answer just wat u thought will be the value of 'K' ( deduced manually ) ???

ASK
Reply With Quote
  #4 (permalink)  
Old July 12th, 2004, 02:35 AM
Authorized User
 
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I used the Borland compiler, which I think gives the right answer according to the rules of 'C'. You could try putting this query on the Borland web-site.

http://info.borland.com/newsgroups/ng_bcpp.html

 Don't say what answer you get but ask what answer you should expect! Compilers do differ !!!

Georges
Reply With Quote
  #5 (permalink)  
Old July 12th, 2004, 03:48 AM
Registered User
 
Join Date: Jul 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I just now tried the code chunk in Borland C compiler and K was 13 for case1. Though this looks pretty simple none of my friends have really been able to come up with a definite solution for this.

Well wat I wud expect the answer to be, is as given below.

++i (3) + i++ (3) * ++i (5) and hence k = 3 + (3x5) = 18

I want more members in the forum to take this up and come out with their views...

With thanks.
Satheesh

ASK
Reply With Quote
  #6 (permalink)  
Old July 12th, 2004, 05:49 AM
Authorized User
 
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Interesting that you now get 18!

++i (3) + i++ (3) * ++i (5) and hence k = 3 + (3x5) = 18

but I would say:

++i (3) + i++ (3) * ++i (4) and hence k = 3 + (3x4) = 15

The i++ term will/should not be implemented until the statement is completed.

Put it on the Borland C++ group and it will bring lots of ideas.




Georges
Reply With Quote
  #7 (permalink)  
Old July 16th, 2004, 04:01 AM
Authorized User
 
Join Date: Dec 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hve a look at:

http://www.eskimo.com/~scs/C-faq/s3.html

explains it all!

Georges

Georges
Reply With Quote
  #8 (permalink)  
Old August 6th, 2004, 02:23 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

the answer is 18
k=++i + i++ * ++i
first because of (++i) i become 3 then becuse of (i++) again 3 contributes in computation but becuse we have another arithmetic instruction i becomes 4
and again becouse of(++i) i becomes 5.
3 + (3x5) =18
now answer this one
what values for a,b,c,r after below instruction(r=a=b=c=0)
r=++a||b++||++c


--------------------------------------------
Mehdi.:)
Reply With Quote
  #9 (permalink)  
Old August 7th, 2004, 10:10 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Gentlemen (and/or Ladies): the C-language behavior of these expressions is undefined, as Nokomis said. There is no "right answer" for a C program, and, therefore, no "wrong" answer. Different compilers can give different answers, but that does not mean that one is "right" and the other is "wrong".

For example, in case 3, Borland bcc32 version 5.1.1 gives 20, and Gnu gcc 3.3.1 gives 13.

Try the following on your favorite C++ compiler(s). Not using C++? Change cout<< to equivalent printf() and try your favorite C compiler(s).

[code]
#include <iostream>

int main()
{
  int i;
  int k;
  int k2;

  std::cout << std::endl << std::endl;
  i = 2;
  std::cout << " Before calculation, i = " << i << std::endl;
  k = i++ * ++i;
  std::cout << " k = " << k << std::endl << std::endl;
  std::cout << " Before calculation, i = " << i << std::endl;
  k = i++ * ++i;
  i = 2;
  k2= ++i * i++;
  std::cout << " k2= " << k2<< std::endl << std::endl;

  i = 2;
  std::cout << " Before calculation, i = " << i << std::endl;
  std::cout << " i++ * ++i = " << i++ * ++i << std::endl << std::endl;

  i = 2;
  std::cout << " Before calculation, i = " << i << std::endl;
  std::cout << " ++i * i++ = " << ++i * i++ << std::endl << std::endl;

  return 0;
}


Borland gave

Code:
  Before calculation, i = 2
                      k = 9

  Before calculation, i = 4
                      k2= 9

  Before calculation, i = 2
              i++ * ++i = 8

  Before calculation, i = 2
              ++i * i++ = 9

Gnu gave

Code:
  Before calculation, i = 2
                      k = 9

  Before calculation, i = 4
                      k2= 9

  Before calculation, i = 2
              i++ * ++i = 9

  Before calculation, i = 2
              ++i * i++ = 9
Which one is right? Does either one give the answers that you would have guessed?

Since the value of C-language expressions like this is undefined, the "answers" are meaningless.

You could use the outputs to infer something about how each compiler evaluates expressions, but that could change with the next release of the same compiler.

Best regards to all inquisitive minds!

Dave
Reply With Quote
  #10 (permalink)  
Old August 7th, 2004, 01:40 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

all of them are right but if someone comes and tells the answer is -10
it would be a wrong answer.
Thanks anyway for your comments.
Cheers.

--------------------------------------------
Mehdi.:)
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
A beginner who needs need help Jamesbizprocom Beginning PHP 1 June 28th, 2007 11:02 PM
c++ beginner, need help please jmarsh56 Visual C++ 0 December 7th, 2005 10:38 AM
Beginner Tilak_1 C# 2 January 21st, 2005 11:19 PM
Beginner [email protected] Beginning PHP 4 December 23rd, 2004 03:22 PM
Beginner programmed XML 3 February 21st, 2004 05:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.