what does Traverse the array mean ?

as title says "what does Traverse the array mean" ? can someone please show it with a simple for loop example thx.
Go through each element.

1
2
3
4
for (int i=0; i< sizeOfArray; ++i)
{
  //do something with array[i]
}
can you please elaborate.
No. I can not make it any clearer. Perhaps someone else can.
closed account (S6k9GNh0)
The word "traverse" means "to go or travel across or over" (http://www.merriam-webster.com/dictionary/traverse). It just means you need to iterate (go through each element (an element being a portion of data the size of whatever data type the array holds)).

Arrays are simple but maybe a bit challenging to understand at first if you don't know how an array is laid out. An array is a contiguous section of data somewhere in RAM (contiguous meaning that it's whole and there are no spaces between the data the array holds). An array is useful for passing large sections of data at time, for instance, in a buffer of audio data or a networking packet.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
    int test[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    /*The above code allocates memory the size of sizeof(int)*10.
        int varies in size but generally, that's 4-bytes (32 bits) times 10 which is 320 bits of data (or 40 bytes). 
        We initialize the array with the variables in order, each number taking 4-bytes of data.
     */
    for (int i = 0; i < 10; ++i) std::cout << test[i] << std::endl;
    return 0;
}


I *suck* at explaining these things but I'm a bit better at answering specific questions.
Last edited on
ok thx for that, i tried to write a program for comparing 2 elements and displaying max number using traverse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

class Max
{
    int marks[2];
    public:
    void get()
    {
    for(int m=0;m<2;m++)
    {
      cout<<"enter marks"<<endl;
      cin>>Marks[m];
    }
    int com=0;
    while(com<2)
    {
     if(marks[com]>marks[com+1])
      {
          cout<<marks[com]<<"is the largest number";
      }
      else if(marks[com]<marks[com+1])
      {
          cout<<marks[com+1]<<"is the largest number";
      }
     com++;
    }
    }
};
int main()
{
    Max A;
    A.get();
    return 0;
}

it is giving an error but is this correct method to use traverse ?
Last edited on
closed account (S6k9GNh0)
1. If com is equal to 1 at some point, then you're accessing out-of-bounds junk which is wrong.
2. Given the size of the array, there's no need for a while loop. There's only 2 elements, not 10 or 20.
3. In addition to advice #2, using loops in such small situations are bad for performance and complicates things. Try not to overly complicate things.
1. If com is equal to 1 at some point, then you're accessing out-of-bounds junk which is wrong.

sorry but i didn't understand it. i wrote int com=0 because it would initialize com to 0.
2. Given the size of the array, there's no need for a while loop. There's only 2 elements, not 10 or 20.
3. In addition to advice #2, using loops in such small situations are bad for performance and complicates things. Try not to overly complicate things.

i actually wanted to input 10 values and make my code display the maximum and minimum number xD.

also as you said i removed while loop and fixed that error and its working now. so is this what traverse an array meant ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

class Max
{
    int marks[2];
    public:
    void get()
    {
    for(int m=0;m<2;m++)
    {
      cout<<"enter marks"<<endl;
      cin>>marks[m];
    }
    int com=0;
     if(marks[com]>marks[com+1])
      {
          cout<<marks[com]<<"is the largest number";
      }
      else if(marks[com]<marks[com+1])
      {
          cout<<marks[com+1]<<"is the largest number";
      }
   
    }
};
int main()
{
    Max A;
    A.get();
    return 0;
}
Last edited on
so is this what traverse an array meant ?


No. To "traverse the array" is simply to go through each element in turn. All that code where you do things with each element is why you're traversing the array, but traversing the array is just going through the array.

Here is where you traverse the array in your code:

1
2
3
4
  for(int m=0;m<2;m++)
    {
...marks[m]...
    }
Last edited on
ah so i am using the variable com or marks[com] to go through or access each elements of the array marks[m] right ?
Traverse the array:

I have 5 apples. The apples are lined up in a row.

I have one finger. It's name is iterator, and I use it to point at apples.

Right now iterator (my finger) is pointing at the apple on the plate that says '0' on it.

Now let's move iterator to the right one step so that it's pointing at the apple on the plate that says '1' on it.

Now move it again so that it's pointing at the apple on the plate that says '2' on it.

Keep moving it, and eventually you'll get to the fifth apple (the one on the plate that says '4').

Congratulations. Moving iterator (my finger) one apple at a time until you reached the end of the "array" of apples means you succesfully traversed all of the apples.
thx for the explanation ciphermagi that's probably the best explanation for traversing an array on the net.
1
2
3
4
for(int m=0;m<2;m++)
    {
...marks[m]...
    }

so in this code marks is the iterator(finger) and m values are apples right ?
instead of creating new topic im going to ask it here, i tried to write program to display minimum number in given 5 numbers ( this only works if the given numbers are in ascending order) but for some reason it isnt working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

class Max
{
    int marks[5];
    public:
    void get()
    {
    for(int m=0;m<5;m++)
    {
      cout<<"enter marks"<<endl;
      cin>>marks[m];
    }
    int com=0;
     while(com<5)
     {
      int temp;
      if(marks[com] < marks[com+1] )
      {
        temp=marks[com];
        marks[com]=temp;
      }
     com++;
    }
    cout<<marks[com]<<"is the min number"<<endl;
    }
};
int main()
{
    Max A;
    A.get();
    return 0;
}


here is how i thought it, if 2,3,4,5,6 are entered
1
2
3
4
5
6
 if(marks[com] < marks[com+1] ) // if(2<3)
      {
        temp=marks[com];            // temp=2
        marks[com]=temp;            //marks[com]=2. next time its going to check if 2<4 and etc.
      }
     com++;

so in this code marks is the iterator(finger) and m values are apples right ?


No, actually. In your example, marks is the name of the array (each apple is an apple, but together they are called marks). 'm' is the iterator.

The array can be considered to be the table upon which the apples are standing, and the iterator is the pointer that tells you which plate you're pointing at.
Topic archived. No new replies allowed.