confirmation of LOOP

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

confirmation of LOOP

 
0
  #1
Aug 20th, 2007
hey guyz!!! i'm just trying to check my code u all if it's correct. thankz!

the problem is " ask a number (N). calculate and display the value of x, where x = (1*N)+(2*N)+(3*N)...(N*N);
ex. if i input a number 5 the formula of x where: x = (1*N)+(2*N)+(3*N)+(4*N)+(5*N);

this is my code:
  1. #include <iostream>
  2. int main ()
  3. {
  4. int n,x,y;
  5. y=0;
  6. cout<<"\nEnter number: ";
  7. cin>>n;
  8.  
  9. while (y<=n)
  10. {
  11.  
  12. cout<<"\n"<<y;
  13. y++;
  14. x += (y*n);
  15.  
  16. }
  17. cout<<"\n\nx = "<<x;
  18.  
  19. return 0;
  20. }
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:12 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: confirmation of LOOP

 
0
  #2
Aug 20th, 2007
>>i'm just trying to check my code u all if it's correct
does it do what its supposed to do ? If yes, then its ok. If not then you need to fix it. One problem though is that your program will produce data overflow errors if the value of n is too large.
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:15 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: confirmation of LOOP

 
0
  #3
Aug 20th, 2007
not! the display is different with my calculation on my own..
x += (y*n); << i'm not sure with this formula...
can you help me with this?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,629
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: confirmation of LOOP

 
0
  #4
Aug 20th, 2007
The value of x is uninitialized, and the value of y should start at 1 not 0. It might be better to use a for loop instead of a while loop.
Last edited by Ancient Dragon; Aug 20th, 2007 at 10:31 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: confirmation of LOOP

 
0
  #5
Aug 20th, 2007
NICE ONE MAN!! I GOT IT!! THANKS, MORE POWER TO YOU...

i'm using the "for loop" its results, like my calculation...huh!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: confirmation of LOOP

 
0
  #6
Aug 20th, 2007
(1*n)+(2*n)+(3*n)...(n*n) == (1+2+3+..+n)*n == (n+1)/2*n*n
Last edited by vijayan121; Aug 20th, 2007 at 10:57 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC