473,387 Members | 1,619 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,387 software developers and data experts.

Lost in an Array

Sheepman
Could someone please walk me through the following program. It's only 15 lines. My comments and questions are below dealing mostly with the array and "for" loops. Hopefully my questions make sense as I'm am so confused they may not be clear. I know the code works as I copied it straight from a book and ran it. I just can't follow what it's doing and I need to in order to do an assignment.

Expand|Select|Wrap|Line Numbers
  1. int main () { 
  2. int item[5]; 
  3. int counter; 
  4. int sum;                                                 
  5.  
  6. cout << " Enter 5 numbers: ";                                     
  7. sum = 0;                                                
  8. for (counter = 0; counter < 5; counter++) { 
  9. cin >> item[counter];
  10. sum = sum + item[counter];
  11. }
  12.  
  13. cout << endl;
  14. cout << " The sum of the numbers is : " << sum << endl;    
  15. cout << " The numbers in reverse order are: ";
  16.  
  17. for (counter = 4; counter >= 0; counter--) {         
  18. cout << item [counter] << " "; 
  19. cout << endl;
  20. return 0;
  21. }
  22.  
Here's my understanding;

Line 2, initializes an array called "item" with 5 components.
Lines 3,4 - initializes two variables

Here's where I'm really confused'

Line 8 - Initializes counter to zero, checks that counter is less than 5, if true then it advances the counter by one.

so in line 9 if you cin >> a number bigger than 5 why doesn't it prematurely end the "for" loop by make "counter" greater than 5?.

If counter is counting the components of the array, what variable is holding the user entered values.

Since sum = sum + item[counter]; is adding the user entered values it appears that counter is what is holding the user entered numbers. So then what is tracking the array components?

Lost in an array!
Jul 5 '07 #1
4 1534
scruggsy
147 100+
Line 2, initializes an array called "item" with 5 components.
Lines 3,4 - initializes two variables
Yup.
so in line 9 if you cin >> a number bigger than 5 why doesn't it prematurely end the "for" loop by make "counter" greater than 5?.

If counter is counting the components of the array, what variable is holding the user entered values.
You're on the right track here.
Since sum = sum + item[counter]; is adding the user entered values it appears that counter is what is holding the user entered numbers. So then what is tracking the array components?
Counter is tracking the array components.

int item[5] creates 5 int variables named item[0], item[1], item[2] ... item[4].
cin >> item[counter] stores the input integer in one of those variables. If counter == 4, the input is stored in item[4].

Since you got this example out of a book, presumably the book has a description of arrays and how they work, so that would be a good place to look for clarification.
If not, try here.
Jul 5 '07 #2
Yup.

You're on the right track here.

Counter is tracking the array components.

int item[5] creates 5 int variables named item[0], item[1], item[2] ... item[4].
cin >> item[counter] stores the input integer in one of those variables. If counter == 4, the input is stored in item[4].
The book seems to get real complicated, real fast. I do think I understand your explanation though.

So the array is named "index", each component of the array index is named counter and inside of array: index[counter] is where the data is stored.

One more clarification, so...
Expand|Select|Wrap|Line Numbers
  1. for (counter = 0; counter < 5; counter++)
  2. cin >> item[counter]
  3.  
Places the user entered data in the array in the following order
item[0] ... item[4].
Jul 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This is not correct:
Quote:
Originally Posted by Sheepman
Line 2, initializes an array called "item" with 5 components.
Lines 3,4 - initializes two variables


Yup.
Line 2 defines an array called item with 5 elements. The variables are not initialized. They have garbage values at this point
Line 3,4 defines two int variables. They are not intialized. They have garbage values.

A garbage value is whatever bits are in memory at that location. You can tell thse are not initialized because there are no values int the code assigned to them.

This is also not correct:
Quote:
Originally Posted by
so in line 9 if you cin >> a number bigger than 5 why doesn't it prematurely end the "for" loop by make "counter" greater than 5?.

If counter is counting the components of the array, what variable is holding the user entered values.

You're on the right track here.
Here's the original code:
sum = 0;
for (counter = 0; counter < 5; counter++) {
cin >> item[counter];
sum = sum + item[counter];
}
Here, the variable sum is set to 0.
Next, a loop is entered. The loop counter is set to zero and then checked to be less than 5. 0 is less than 5 so you do the code between the braces. Whatever you enter goes into item[0]. Then sum is increased by what you entered. Then you reach the } brace. Then count is incremented to 1. And the loop repeats.

Notice the counter is not affeected by what you enter. You cna enter any value.

The loop will cycle while the counter is 0, 1,2 3 and 4. Those are the five values needed to identify your array elements.
Jul 6 '07 #4
It was drilled into my head early on to initlalize the arrays to zero. Easiest way to do it for the array you have is:

Expand|Select|Wrap|Line Numbers
  1.  
  2. item[5]={0};
  3.  
  4.  
You can also do it in a loop style:

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (j=0;j<5;++j)
  3. {
  4.   item[j]=0;
  5. }
  6.  
  7.  

Hope this helps,
J
Jul 6 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Simon Redmond | last post by:
my hosting provider just upgraded the server and now some of my scripts don't work.... Fatal error: Call to undefined function: imagecreatefromjpeg() What do I need to look for in order to...
0
by: Joshua Beall | last post by:
Hi All, I cannot turn off magic quotes, since I am just leasing space from a hosting company, and they determine the magic_quotes settings. I realize in retrospect that using a .htaccess file...
8
by: dmcconkey | last post by:
Hi folks, I have a client with four websites. Each site has a contact form that is identical. They all have "required" fields validated through a JavaScript onSubmit() function. Upon validation,...
2
by: Farhad | last post by:
Hi, in the following example there is a output difference in version 5.00 and 5.8 of Perl. In case of a missmatch in element 2, the whole element gets lost instead of an "undef" value. Does...
3
by: windandwaves | last post by:
Hi Folk I have the following array: $m = array(); $m = array("lodgings", "rooms"); $m = array("location", "there and away"); $m = array("functions", "events and groups"); $m =...
0
by: silvrique | last post by:
I am totally lost. I have an xml script example as follows: <?xml version="1.0" encoding="utf8" ?> <starter> <second> <name>Silver</name> </second> </starter>
3
by: c++dummy | last post by:
I got this project for my class and I'm totally lost as to how to copy the 1d array with the bone name into a 2d array using this supposed strncpy function I'm supposed to create. I believe the...
2
by: vunet.us | last post by:
Please, explain an interesting phenomenon, if you can. I have an array of references to an HTML element: <div id='container'> <div id='someId1'></div> <div id='someId2'></div> </div> ........
3
by: Saint | last post by:
My Server Enivronment: MS Windows Server 2003 Standard Edition, Service Pack 1 (running as a VM on a Intel Xeon server) IIS running on Port 80 Apache 2.0.63 running on port 8080 PHP 5.2.5
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.