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.
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 ?
#include <iostream>
usingnamespace 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";
}
elseif(marks[com]<marks[com+1])
{
cout<<marks[com+1]<<"is the largest number";
}
}
};
int main()
{
Max A;
A.get();
return 0;
}
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:
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.
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.
#include <iostream>
usingnamespace 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.