Arrays are one of the most important data types in any programming language. Pointers are the most important, but will be saved for another tutorial. Arrays also have a habit of confusing people. We'll talk about how C++ handles arrays in this tutorial, and some standard tools for working with them.
As a quick review, the basic variables that are available are:
bool (true or false)
char (usually 8 bits, can store an ASCII character)
int (short is at least 16 bits, long is at least 32 bits, standard is between them (inclusive))
double (float <= double <= long double)
For this tutorial, we'll assume bool is 8 bits, char is 8 bits, int is 32 bits, and double is 64 bits. On most 32 bit processors, this is reasonable. Why do we care? Because arrays are usually built from these basic types.
An array is a collection of several variables of the same type, under the same name. To compare, if I wanted to create ten ints, I could do it like this:
This will create a memory structure similar to this:Code:int main(){ int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10; return 0; }
Using arrays, we can simplify the code to get a very similar memory structure:Code:|int1 ||int2 ||int3 ||int4 ||int5 ||int6 ||int7 ||int8 ||int9 ||int10 |
The resulting array as values in myint[0] to myint[9]Code:int main(){ int myint[10]; return 0; }
If you make a mistake and try to use myint[10], you will access whatever happens to be in the next block after myint[9]. That may mean accessing part of a double, two short ints, four chars, or most anything else. When this happens in programs that many people use, it's called a buffer overflow, and can overwrite function code, critical variables, or most anything else. This is the source of many security exploits. Be Careful!Code:|myint[0]||myint[1]||myint[2]||myint[3]||myint[4]||myint[5]||myint[6]||myint[7]||myint[8]||myint[9]|
Given the danger of turning an MP3 player into a means for spreading viruses, what is the use of arrays and how do we use them safely? For one thing, populating the array with data is much easier. To populate ten variables with values 1 through 10, you can do either of the following:
This takes many lines to initialize the variables. Compare it with the following:Code:int main(){ int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10; int1 = 1; int2 = 2; int3 = 3; int4 = 4; int5 = 5; int6 = 6; int7 = 7; int8 = 8; int9 = 9; int10 = 10; return 0; }
This will populate the corresponding memory locations with the exact same values. The reason this works is that myint[i] = i+1; is equivalent to myint[0] = 0+1;, myint[1] = 1+1;, myint[2] = 2+1;, etc. The advantage of arrays is that you can use variables to keep track of positions in the array (such as i above). This concept is the key behind sorting. Rather than having to awkwardly associate a variable with it's value, you can use the position to access the value. Compare these two versions of a modified bubble sort:Code:int main(){ int myint[10],i; for (i=0;i<10;i++) myint[i] = i+1; return 0; }
Inputting 3 5 8 2 9 5 3 4 2 0 produces output 0 2 2 3 3 4 5 5 8 9. The sequence after each round of i is:Code:#include <iostream> int main(){ int myint[10], i, j, temp; for (i=0;i<10;i++) std::cin>>myint[i]; //input 10 integers from user //start bubble sort here for (i=0;i<9;i++){ //go from the first to next to last item for (j=0;j<9;j++){ if (myint[j]>myint[j+1]){//check for a pair of items out of order temp=myint[j]; //these three lines swap adjacent array elements myint[j]=myint[j+1]; myint[j+1]=temp; } } } //end bubble sort for(i=0;i<10;i++) std::cout<<myint[i]<<" "; //output sorted results std::cout<<"\n"; }
i==0: 3 5 2 8 5 3 4 2 0 9
i==1: 3 2 5 5 3 4 2 0 8 9
i==2: 2 3 5 3 4 2 0 5 8 9
i==3: 2 3 3 4 2 0 5 5 8 9
i==4: 2 3 3 2 0 4 5 5 8 9
i==5: 2 3 2 0 3 4 5 5 8 9
i==6: 2 2 0 3 3 4 5 5 8 9
i==7: 2 0 2 3 3 4 5 5 8 9
i==8: 0 2 2 3 3 4 5 5 8 9
The same code without arrays would look like this:
The difference between using arrays and not using arrays should now be very clear, especially if you consider sorting 100 or 1000 numbers. At that point, NOT using an array becomes very dangerous, simply because it is so easy to mistype one value.Code:#include <iostream> int main(){ int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, i, temp; //input 10 integers from user std::cin>>int1; std::cin>>int2; std::cin>>int3; std::cin>>int4; std::cin>>int5; std::cin>>int6; std::cin>>int7; std::cin>>int8; std::cin>>int9; std::cin>>int10; //start bubble sort here for (i=0;i<9;i++){ //go from the first to next to last item if (int1>int2){//check for a pair of items out of order temp=int1; //these three lines swap adjacent array elements int1=int2; int2=temp; } if (int2>int3){//check for a pair of items out of order temp=int2; //these three lines swap adjacent array elements int2=int3; int3=temp; } if (int3>int4){//check for a pair of items out of order temp=int3; //these three lines swap adjacent array elements int3=int4; int4=temp; } if (int4>int5){//check for a pair of items out of order temp=int4; //these three lines swap adjacent array elements int4=int5; int5=temp; } if (int5>int6){//check for a pair of items out of order temp=int5; //these three lines swap adjacent array elements int5=int6; int6=temp; } if (int6>int7){//check for a pair of items out of order temp=int6; //these three lines swap adjacent array elements int6=int7; int7=temp; } if (int7>int8){//check for a pair of items out of order temp=int7; //these three lines swap adjacent array elements int7=int8; int8=temp; } if (int8>int9){//check for a pair of items out of order temp=int8; //these three lines swap adjacent array elements int8=int9; int9=temp; } if (int9>int10){//check for a pair of items out of order temp=int9; //these three lines swap adjacent array elements int9=int10; int10=temp; } } //end bubble sort //output sorted results std::cout<<int1<<" "; std::cout<<int2<<" "; std::cout<<int3<<" "; std::cout<<int4<<" "; std::cout<<int5<<" "; std::cout<<int6<<" "; std::cout<<int7<<" "; std::cout<<int8<<" "; std::cout<<int9<<" "; std::cout<<int10<<" "; std::cout<<"\n"; }


LinkBack URL
About LinkBacks





Reply With Quote




Posted via 

After some research the "famous" example is 10 foot long @ 1 foot apart and the error can be referred to as the "Fencepost Error" also known as the "One-By-One" error.

Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum