Easy Tutorial: Computer Programming for DUMMIES -- beginner friendly!

Hey guys! I just want to let you know, yet again, this tutorial can be applied to many programming languages. I will be programming in C++, so the syntax for other languages will be a little bit different. This tutorial is about arrays. Arrays are in many languages. Learning them in one language will help you easily translate your knowledge of arrays to others. this is tutorial number six. If you want to catch up on my other tutorials, here they are:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Side note: When becoming a computer programmer, you want to be able to do things efficiently and quickly. One helpful thing to know is keyboard shortcuts. Here are four basic keyboard shortcuts I use all of the time. They make things a little bit easier!

  • Ctrl + C - copy
  • Ctrl + V - paste
  • Ctrl + X - cut
  • Ctrl + A - select all

What is an Array?

An array is a list of elements. Here are a few examples:

{1, 2, 3, 4, 5}
{a, b, c, d}
{dog, cat, mouse, giraffe}

Pretty simple! In programming, we can store an array in a variable.

Declaring an Array

You declare an array just like any other variable, except for two differences.

  1. You follow the variable name with brackets, and place a number in the brackets. The number declares the size of the array. You do not have to place a number in the brackets if you set the array equal to a set of values when you declare it (but you can as long as the number is greater than or equal to the number of values you declared).
  2. You cannot set the array equal to a single value by itself. You must surround the set by brackets and place commas between each value.

Here are some examples:

int arr[5];

This declares an integer array called 'arr' with five elements.

int arr[] = {1, 2, 3, 4, 5};

This declares an integer array called 'arr' with the values 1-5. The program will know to make the array size five since the size was not declared and there are five values.

int arr[50] =  {1, 2, 3, 4, 5};

This declares an integer array called 'arr' with the values 1-5 in the first five array addresses. There are 45 more array addresses that are not declared yet because 'arr' was declared with a size of 50.

Referencing and Setting Values to an Array

Say we have this array:

int arr[5];

To access a value in the array, type in the name of the array followed by brackets. Place the address of the value in the brackets. Array addresses start at 0 and count up by 1. This may be a bit confusing at first (and you probably will make the mistake of starting at 1 several times). Let's set the first address of 'arr' equal to 10. This is how to do that:

arr[0] = 10;

We can set the rest of the values now.

arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Notice how I stopped at the address 4. This is the last address since the size of the array is only 5. You can use the different array addresses like variables. Here is what I mean:

cout << arr[0] + arr[4];

arr[0] = 10 // arr[4] = 50 // 10 + 50 = 60 // the screen prints 60

Printing Arrays

To print the contents of an array, you cannot simply write:

cout << arr;

You will get something like this:

0x7fff3ed5de50

What is that? This is actually the address of the variable 'arr.' This is where the computer stores 'arr.' One way to print the content is to do something like this:

cout << arr[0] << " ";
cout << arr[1] << " ";
cout << arr[2] << " ";
cout << arr[3] << " ";
cout << arr[4] << " ";

What if your array has 1000 elements?? There has to be an easier way! ... Well there is! This is why loops are so useful. The following code is equivalent to the code above:

for(int x = 0; x < (sizeof(arr1) / sizeof(arr1[0])); x++)
    {
        cout << arr[x] << " "; 
    }

Look at '(sizeof(arr1) / sizeof(arr1[0])).' The 'sizeof' function returns the size of the argument in bytes. The number of bytes in the entire array divided by the number of bytes in one value in the array will equal however many values are in the array. In our case, that is equal to 5. I just wanted to point out that we don't even have to know how many values are in the array to print the array elements. The following code is equivalent to the code above:

for(int x = 0; x < 5; x++)
    {
        cout << arr[x] << " "; 
    }

This loop executes its body five times. 'x' starts at 0, counting up all the way to 4 before it ends. 0-4 are all of the addresses of 'arr,' so all of the values are printed in order.

Strings

Do you remember that data type I talked about in my tutorial about variables called 'string'? The reason why you need to include the string class to use 'string' as a data type is because a string is actually an array of characters. Languages such as C do not have a 'string' type. Check this code out:

char charArr[7] = {'s','t','r','i','n','g','\0'};
string s = "string";
if(charArr == s)
        cout << "They are equivalent.\n";

Output:

They are equivalent.

Note that the last character of a string MUST be the NULL character. This is simply '\0' (backslash zero). If you declare a string, this character will automatically be placed at the end. This is how your program knows where the end of a string is.

Here is a program I wrote to help you understand arrays a little better:

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int arr1[5] = {1,2,3,4,5};
    cout << "Address of arr1: " << arr1 << endl;

    cout << "Size of arr1: " << sizeof(arr1) / sizeof(arr1[0]) << endl;

    cout << "Contents of arr1: ";
    for(int x = 0; x < (sizeof(arr1) / sizeof(arr1[0])); x++)
    {
        cout << arr1[x] << " "; 
    }
    cout << endl << endl;

    double arr2[5];
    arr2[0] = 10.5;
    arr2[1] = 20.567;
    arr2[2] = 30.45;
    arr2[3] = 40.001;
    arr2[4] = 50.000030005;
    double arrSum = arr2[0] + arr2[1] + arr2[2] + arr2[3]+ arr2[4];
    cout << "Sum of the contents of arr2: " << arrSum << endl << endl;

    string sArr[4] = {"cow","chicken","horse","platypus"};
    cout << "Four different farm animals: " << endl;
    for(int x = 0; x < (sizeof(sArr) / sizeof(sArr[0])); x++)
    {
        cout << sArr[x] << endl;
    }
    cout << endl;

    return 0;
}

Here is the output:

[cmw4026@omega test]$ g++ array.cpp
[cmw4026@omega test]$ ./a.out
Address of arr1: 0x7fff9ea43be0
Size of arr1: 5
Contents of arr1: 1 2 3 4 5

Sum of the contents of arr2: 151.518

Four different farm animals:
cow
chicken
horse
platypus

[cmw4026@omega test]$

I hope this helped out! Leave any questions in the comments. There is no stupid question!

Sort:  

Nice Tutorial. Would like to see OOP for dummies! :)

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 62857.40
ETH 3441.23
USDT 1.00
SBD 2.51