Array

Arrays are very useful tools when it comes to storing data in a single variable name, they allows quick and simple access to specific data. Arrays bring much needed organization to larger amounts of data that need to be accessed and become even more of a handy tool when used with loops. Before we look at the syntax imagine if you wanted to store the age of five individuals, up until now it would look something like this.
Click here to see programs of array

int age1=15;
int age2=24;
int age3=54;
int age4=33;
int age5=120;

The most amazing thing in this code is how the fifth person lived to be 120! I wonder what their diet was like....anyone? Ok back to business, obviously this looks a little tedious and it could be worse, what if there was 20 people! There must be simpler way, well when have I ever let you down? An array can store all these values with a single name, first here is the syntax!

datatype name [index];

So going back to the last example, say we wanted an array that stored integers, was named ages, and that held five values. Here is how we would declare something like that.

int ages [5];

Ok pretty straight forward so far, so as of right now our array 'ages' has no value, we must intialize them, there are actually a couple different ways you can do this I'll show you both and you be the judge.

int ages [5]; //declare an array

//now intialize
ages[0]=15;
ages[1]=24;
ages[2]=54;
ages[3]=33;
ages[4]=120;

This way is almost just as tedious as it was in the beginning but it does serve as a good example. Notice how we designed this array to hold five values hence the [5]. However when we initialize the highest number is [4]. In C++ if the index number is a number [N] then the highest index number when assigning a value will be [N-1]. This is obviously because the first index number is [0] and this is how it will ALWAYS be. Yes, it would be easier if the first index number was [1] but at the machine level (when data is just ones and zeros) starting array at [0] makes perfect sense and is more efficient. Here is another way to assign values to an array.
Click here to see programs of array

int ages [5];//declare an array

//now intialize
ages = {15,24,54,33,120};

This method is much more efficient and works exactly the same way, ages[0]=15, ages[1]=24, and so on. Ok so now that we have assigned values lets print them out, first let me show you a typical beginners mistake.

//program does not work, for example only
#include <iostream>

using namespace std;

int main()
{
    int ages [5];
    ages={15,24,54,33,120};
    cout<<ages<<endl;//incorrect

return 0;
}

This mistake is common because up until now it makes sense, but in C++ there is no one statement that can print out an entire array, you are required to tell the compiler what specific array you would like to print to the screen, if we wanted to print out 33 then we would have to use ages [3]. If we wanted to print out the entire array it would become somewhat tedious.

//Full working program
#include <iostream>

using namespace std;

int main()
{
    int ages [5];
    ages={15,24,54,33,120};
    cout<<ages[0]<<endl;
    cout<<ages[1]<<endl;
    cout<<ages[2]<<endl;
    cout<<ages[3]<<endl;
    cout<<ages[4]<<endl;

return 0;
}

Whew! I don't know about you but that was tiring, good thing we didn't have to print out twenty values, there has to be a better way! Luckily there is, notice how each index number is an integer because indexes in arrays can only hold integers and nothing else, because of this we are able to put a variable that holds a different integer through each cycle or should I say loop. That's right all we need is a for loop and a variable. Here is how.

//Full working program
#include <iostream>

using namespace std;

int main()
{
    int k;//delcare a variable 'k' that holds integer values
    int ages [5];
    ages={15,24,54,33,120};

       for (k=0; k<5; k++)//set 'k' to 0 execute while 'k' is less than 5
          {
              cout<<ages[k]<<endl;
          }
   
return 0;
}

There you have it! Are you able to see how it works? The main part of this program is the for loop.

for (k=0; k<5; k++)//set 'k' to 0 execute while 'k' is less than 5
          {
              cout<<ages[k]<<endl;
          }

When the loop first starts 'k' is 0 so when the cout statement executes it's like coding "cout<<ages[0]<<endl;" 'k' then increments by one and 'k' now equals 1 so now the cout statement looks like this "cout<<ages[1]<<endl;" we are essentially doing exactly the same thing as in the last program but only much more efficiently thanks to the for loop. One more thing to notice is the "k<5" this loop will execute so as long as k is less then 5 so it will run up to 4 and then break after that, remember because of the nature of arrays the index number will always be [N-1].
Just how for loops can be used to print our arrays they can also be very useful when inputing values through console input or better known as cin. If we allow the user of the program to assign the array values the program becomes much more flexible.

//Full working program
#include <iostream>

using namespace std;

int main()
{
    int k;//delcare a variable 'k' that holds integer values
    int ages [5];
   
        for (k=0; k<5; k++)
          {
              cout<<"Enter a value"<<endl;
              cin>>ages[k];
           }

       for (k=0; k<5; k++)//set 'k' to 0 execute while 'k' is less than 5
          {
              cout<<ages[k]<<endl;
          }
   
return 0;
}

The first for loop is exactly the same only it accepts values instead of printing them out, this allows for much less tedious coding and more flexible programs. As I mentioned before, arrays are very useful for storing large amounts of data with a single name keep this in mind when building your own programs. There are much more that arrays can do but these lessons are intended for beginners to get a good understanding of the basics of programming, for now we will keep complexity at a minumum.

Click here to see programs of array


0 comments:

Post a Comment