Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 16th, 2006, 12:21 PM   #1
Edgar.
Newbie
 
Edgar.'s Avatar
 
Join Date: Jun 2006
Location: Bath Room
Posts: 11
Rep Power: 0 Edgar. is on a distinguished road
Help with array

Can you guys tell me why a have the result of 12206, I thought that the result will be the values o billy(16, 2, 77, 40, and 12071)

Quote:
// arrays example
#include <iostream>
using namespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}
I don't want to display the billy,s values I just want to know why a got the results of 12206 thank you guys.
Edgar. is offline   Reply With Quote
Old Jun 16th, 2006, 12:29 PM   #2
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,671
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
What you are doing is adding the values of billy(16, 2, 77, 40, and 12071) and that equals 12206, you are getting the results that your program is suppose to give you if you are getting 12206
__________________
BIG K aka Kyle
Programming Forums
Kyle K Online

Please do not PM or email me programming questions. Post them in the forums instead.
big_k105 is offline   Reply With Quote
Old Jun 16th, 2006, 12:34 PM   #3
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
  for ( n=0 ; n<5 ; n++ )
  {
	  result += billy[n];
  }

What this part is doing is:

result = result + billy[n];

If result is originally 0, then after the first iteration of the loop result will be:

0 + billy[0], or 0 + 16.

After the second iteration it will be:

result = result + billy[1] or result = 16 + 2

because result retains the value was previously assigned to it, which in this case was 16. It repeats that process over and over until it adds everything in the array up.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Jun 16th, 2006, 12:39 PM   #4
Edgar.
Newbie
 
Edgar.'s Avatar
 
Join Date: Jun 2006
Location: Bath Room
Posts: 11
Rep Power: 0 Edgar. is on a distinguished road
huuuu, got it, thank you guys, you are the best.
Edgar. is offline   Reply With Quote
Old Jun 19th, 2006, 5:18 PM   #5
imagikricei
Newbie
 
imagikricei's Avatar
 
Join Date: Jun 2006
Location: texas
Posts: 22
Rep Power: 0 imagikricei is on a distinguished road
indeed.
imagikricei is offline   Reply With Quote
Old Jun 19th, 2006, 6:18 PM   #6
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 773
Rep Power: 3 Jimbo is on a distinguished road
indeedydo. Please keep your posts contributory to the threads.

Oh, and change the avatar please.
Quote:
Originally Posted by Forum Rules
No animations - Regardless of how small the animation is. Some people find animations distracting when reading a thread. Because of the issues that would arise if we allowed some small animations but not others, the policy is no animations at all.
Jimbo is offline   Reply With Quote
Old Jun 19th, 2006, 10:20 PM   #7
JamesCEdmonds
Newbie
 
Join Date: May 2006
Posts: 19
Rep Power: 0 JamesCEdmonds is on a distinguished road
http://www.cplusplus.com/doc/tutorial/arrays.html
JamesCEdmonds is offline   Reply With Quote
Old Jun 20th, 2006, 5:43 PM   #8
frankish
Hobbyist Programmer
 
frankish's Avatar
 
Join Date: Oct 2005
Location: Ohio
Posts: 177
Rep Power: 0 frankish is an unknown quantity at this point
What was the point of this thread?
frankish is offline   Reply With Quote
Old Jun 20th, 2006, 6:01 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The point of the thread was that Edgar had a problem understanding some things about arrays. The point of a number of the responses (including yours) is anyone's guess. The point of my response (similar to Jim's) is making a point about points in response to your response. I actually thought I might puke in my shirtpocket to see a link reproducing the OP's code.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:07 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC