Lost in an Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheepman
    New Member
    • Jun 2007
    • 24

    Lost in an Array

    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.

    Code:
    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!
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by Sheepman
    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.

    Comment

    • Sheepman
      New Member
      • Jun 2007
      • 24

      #3
      Originally posted by scruggsy
      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...
      Code:
      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].

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This is not correct:
        Originally posted by scruggsy
        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:
        Originally posted by scruggsy
        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:
        Originally posted by Sheepman
        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.

        Comment

        • Hypnotik
          New Member
          • Jun 2007
          • 87

          #5
          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:

          Code:
          item[5]={0};
          You can also do it in a loop style:

          Code:
          for (j=0;j<5;++j)
          {
            item[j]=0;
          }

          Hope this helps,
          J

          Comment

          Working...