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. -
int main () {
-
int item[5];
-
int counter;
-
int sum;
-
-
cout << " Enter 5 numbers: ";
-
sum = 0;
-
for (counter = 0; counter < 5; counter++) {
-
cin >> item[counter];
-
sum = sum + item[counter];
-
}
-
-
cout << endl;
-
cout << " The sum of the numbers is : " << sum << endl;
-
cout << " The numbers in reverse order are: ";
-
-
for (counter = 4; counter >= 0; counter--) {
-
cout << item [counter] << " ";
-
cout << endl;
-
return 0;
-
}
-
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!
4 1540
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.
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... -
for (counter = 0; counter < 5; counter++)
-
cin >> item[counter]
-
Places the user entered data in the array in the following order
item[0] ... item[4].
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.
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:
You can also do it in a loop style: -
-
for (j=0;j<5;++j)
-
{
-
item[j]=0;
-
}
-
-
Hope this helps,
J
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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,...
|
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
|
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");
| |
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>
|
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
|
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");
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |