C++ part 6 – Arrays (One dimensional)

UPDATED 26.8.2013 by adding
– A bubble sort algorithm (sort of)

In this post, we will be going through a subject namely arrays. Arrays, Lists, Hashmaps and similar statements are ways to store multiple values inside one variable name. Arrays basically are tables that hold values in a specific index. For example if I had a task to input 1000 names the traditional way of creating a new variable for every name is not a smart way to do it. To help us out, we create an array to hold all these names inside one array (name) that we can use to access these names and values in general.

The best way for me to explain this basically is through a picture and a code copy paste like in the below. Just meditate on the code, the output and the picture. Then proceed to the theory. The arrays in the below examples are one dimensional.

CODE:
#include <iostream>
using namespace std;
int main()
{
 
 int numberTable [10] = {5, 6, 3, 2, 9, 22, 1001, 21, 133, 42};

    
    for(int b = 0; b < 10; b++)
     {
        cout << “The numbers you entered were: ” << numberTable[b] << endl;
     }  
    
    system(“PAUSE”);
    return 0;
}

PICTURE OF OUTPUT:
arrayss

PICTURE WITH DESCRIPTION OF THE INDEX:

indexArray

where the index basically means the reference number that you use to pick up the value that this particular bit of space holds in it in your computer. Do note that arrays always start from 0 where 0 is same as 1 and so on. You can initialize your arrays to have a null value in the index of zero but that is another story. Below is a picture that I created with wrong amount of index’s but I thought it would be good for you to see how to create and handle arrays. So you see that creating an array with 9 values also has 9 index numbers but the counting starts at 0.

numtable

So basically what happened in the above code and example is that we created/declared an array (“regular order or arrangement; series: an array of figures.”) that can hold maximum of 10 values inside it be it of data type string, char, int, double, float or any other (I will show the syntax for other data types in a moment). The syntax is somewhat like I have written below

dataType dataName [numberOfvalues] = { initialization };

This array or table as we call it, has an index number meaning that it has a reference number that we can use to read, output, modify, input or delete the value that it is holding. This index numbering always start with zero(0).

You can see in the picture that the numberTable[0] holds the value 5 and numberTable[9] holds the value 42 and this is why we ended the for loop before 10 or else we would have had an array out of boundary error message. Below is the for loop code.


    for(int b = 0; b < 10; b++)
     {
        cout << “The numbers you entered were: ” << numberTable[b] << endl;
     }

and as you can see, we start the loop from the value 0 and end the for loop just before 10 and we output the value of this array in the following manner

arrayName[index_number]

where in our own code would be the following

numberTable[b]

where the b is the variable that we initialized in the for loop. We use the variable b to access the values inside this array. Instead of using a variable name, we can also use numbers for example

numberTable[1]

that would output the value 6.

—————————————————————————————————————————————-

Below is another example code where we input values inside our array and we output the values we entered.

CODE:
#include <iostream>
using namespace std;
int main()
{

int numberTable [10];

for(int i = 0; i < 10; i++)
{
cout << “Enter a whole number:” << endl;
cin >> numberTable[i];
}

for(int b = 0; b < 10; b++)
{
cout << “The numbers you entered were: ” << numberTable[b] << endl;
cout << “and the index was ” << b << endl;
}

system(“PAUSE”);
return 0;
}

OUTPUT:
array2

—————————————————————————————————————————————-

In the below example we will create an array of data type string

CODE:
#include <iostream>
using namespace std;
int main()
{
 
 string numberTable [10] = {“Tom”, “John”, “Mona”, “Sarah”, “Captain Kirk”, “Stallone”, “Arnold”, “Timmy”, “Jones”, “Jim”};

    
    for(int b = 0; b < 10; b++)
     {
        cout << “The strings you entered were: ” << numberTable[b] << endl;
     }  
    
    system(“PAUSE”);
    return 0;
}
OUTPUT:
array3

—————————————————————————————————————————————-

below is a sum program to use two arrays and sum two values.

CODE:
#include <iostream>
using namespace std;
int main()
{
 
 int numberTable [10] = {5, 6, 3, 2, 9, 22, 1001, 21, 133, 42};
 
 int numberTable2[10] = {7, 2, 8, 10, 100, 21, 33, 53, 44, 1};

    
    for(int b = 0; b < 10; b++)
     {
      int sum = numberTable[b] + numberTable2[b];
      cout << “The sum of the two arrays in the index ” << b << endl;
      cout << “are ” << sum << endl;
     }  
    
    system(“PAUSE”);
    return 0;
}

OUTPUT:
arrays4

—————————————————————————————————————————————-

Below is the code for my own bubble sort algorithm to sort the values from biggest to smallest that I wrote in few minutes. The algorithm is fairly simple, but my own version is not very efficient and there are probably more better built ones out there.

#include <iostream>
using namespace std;
int main()
{
 
 int numberTable [10] = {5, 6, 3, 2, 9, 22, 1001, 21, 133, 42};
 
 
    for(int b = 0; b < 10; b++ ) {
         for(int c = 0; c < 10; c++ ) {
          if( numberTable[b] > numberTable[c] ) {
              int a = numberTable[b];
              numberTable[b] = numberTable[c];
              numberTable[c] = a;
          }      
         }
     }  
    
    for(int o = 0; o < 10; o++) {
            cout << “Here is the sorted number: ” << numberTable[o] << endl;
    }
    
    
    system(“PAUSE”);
    return 0;
}

Below is the picture of the code and the output:

exmp

You can just reverse some code bits to output from smallest to biggest. I will add the logic behind this probably later but the basics are that you take one value one by one and compare it with the whole pack and switch places thus having values ordered in the way you want.

I will publish this now and update + add more content later.