473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lost in an Array

Sheepman
24 New Member
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 1540
scruggsy
147 New Member
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
Sheepman
24 New Member
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 Recognized Expert Moderator Expert
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
Hypnotik
87 New Member
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
2566
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 make sure I still have this functionality, version is php 4.3.3 should I look for something specific in the output from phpinfo()??? Thanks for all...
0
1997
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 to turn magic quotes would probably be better, and I am going to switch to that solution, but I am still trying to figure out what is causing my...
8
5081
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, post values go to a PHP processing page that adds values to a database and generates an email to someone in marketing. For three of these sites,...
2
1796
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 anybody know why in version 5.8 we lost the element and is that an error in Perl interpreter or does it work as designed. Thanks Farhad
3
1271
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 = array("activities", "things to do", "exclusive activities"); $m = array("enquiries", "tariffs"); $m = array("download", "media info");
0
1066
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
2012
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 teacher wants us to make the function and not use the library function. Here's the info: Write a C++ program that
2
2116
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> ..... myArray = document.getElementById("someId1"); myArray = document.getElementById("someId2");
3
2334
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
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.