+ Reply to Thread
Results 1 to 10 of 10

Thread: How to use arrays, and why they're important

  1. #1
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    How to use arrays, and why they're important

    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:
    Code:
    int main(){
      int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10;
      return 0;
    }
    This will create a memory structure similar to this:
    Code:
    |int1    ||int2    ||int3    ||int4    ||int5    ||int6    ||int7    ||int8    ||int9    ||int10   |
    Using arrays, we can simplify the code to get a very similar memory structure:
    Code:
    int main(){
      int myint[10];
      return 0;
    }
    The resulting array as values in myint[0] to myint[9]
    Code:
    |myint[0]||myint[1]||myint[2]||myint[3]||myint[4]||myint[5]||myint[6]||myint[7]||myint[8]||myint[9]|
    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!

    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:
    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 takes many lines to initialize the variables. Compare it with the following:

    Code:
    int main(){
      int myint[10],i;
      for (i=0;i<10;i++)
        myint[i] = i+1;
      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:
    #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";
    }
    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:
    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:

    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";
    }
    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.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: How to use arrays, and why they're important

    Your demonstration shows how convenient arrays are. One thing that new developers must become accustomed to is starting to count from 0. Consider the famous picket fence example, if each horizontal board is 1 foot long how many posts do you need in order to build a 5 foot fence? It is tempting to say 5 but you will actually need 6.

    Another excellent read! +rep

  4. #3
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: How to use arrays, and why they're important

    Your example is wrong jordan - if I lay 5, 1 foot long boards in a row and measure the length it will be 5 foot long. Posted via CodeCall Mobile

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: How to use arrays, and why they're important

    He's referring to the vertical posts that are in the ground. You start with two posts one foot apart, then add another to make 3 posts covering 2 feet. Extend to 6 posts at 5 feet.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Join Date
    Oct 2008
    Posts
    4,060
    Blog Entries
    6
    Rep Power
    45

    Re: How to use arrays, and why they're important

    Oh ok then sorry about that jordan my mistake! Good tutorial btw winged i would give you some rep tonight but my rep power is only 1 so it wouldnt be even worth it Posted via CodeCall Mobile

  7. #6
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: How to use arrays, and why they're important

    Don't worry, Termana, I care about the verbal high-fives more than the rep (which I basically ignore).
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  8. #7
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: How to use arrays, and why they're important

    One suggestion, in the top where you are talking about the different data types.

    I would add some specification type stuff that talks about how a byte can be any size, chars are guaranteed to be a byte, a byte is the smallest addressable unit, an int is usually a word, etc.

  9. #8
    Jordan Guest

    Re: How to use arrays, and why they're important

    Quote Originally Posted by Termana View Post
    Oh ok then sorry about that jordan my mistake! Good tutorial btw winged i would give you some rep tonight but my rep power is only 1 so it wouldnt be even worth it Posted via CodeCall Mobile
    No worries, you just proved my point. 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.

    Interesting Reads:
    History of fence-post error
    Off-by-one error - Wikipedia, the free encyclopedia

  10. #9
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: How to use arrays, and why they're important

    Quote Originally Posted by MeTh0Dz|Reb0rn View Post
    One suggestion, in the top where you are talking about the different data types.

    I would add some specification type stuff that talks about how a byte can be any size, chars are guaranteed to be a byte, a byte is the smallest addressable unit, an int is usually a word, etc.
    I may address that issue in a tutorial on data-types. I think it is an important topic that deserves a more extended discussion than I wanted to apply here. What I'm setting up for is a discussion of pointers and multi-variable arrays, as well as what can happen when you walk off the end of an array.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    Join Date
    May 2008
    Posts
    2,126
    Blog Entries
    1
    Rep Power
    33

    Re: How to use arrays, and why they're important

    Alright.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C Defining Data Arrays And Not BSS Arrays?
    By RhetoricalRuvim in forum C and C++
    Replies: 1
    Last Post: 09-02-2011, 07:59 AM
  2. IMPORTANT!!
    By Logan in forum The Lounge
    Replies: 29
    Last Post: 02-15-2009, 12:42 PM
  3. An important exam!
    By Zael in forum C and C++
    Replies: 12
    Last Post: 01-22-2008, 07:08 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts