Easy Tutorial: Computer Programming for DUMMIES -- very easy!

Hey guys! This is a continuation of my programming tutorials in C++. If you need to catch up, here are links to the others:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

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

Part 5: Loops

Part 6: Arrays <-- Definitely check that one out before this tutorial

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

This tutorial is about 2D arrays. As you know, an array is a list or set of elements. A 2D array is like a list of lists (or a set of sets). Here are a few different ways you can declare a 2D array:

int arr[10][5];
int arr1[][4] = { {1,2,3,4},
                  {5,6,7,8},
                  {9,0,1,2} };
int arr2[3][4] = { {1,2,3,4},
                   {5,6,7,8},
                   {9,0,1,2} };
  • arr1 and arr2 are equivalent.
  • If you equate the array to values on the same line as you declare it, then the first set of brackets does not need a number, meaning its size being declared (although, the second set of brackets does).

Here is an example of how a 2D array is called:

cout << arr1[1][2];

This will print the value 7 (1 represents the second set; 2 represents the third value of the set)

More examples:

  • arr1[0][0] --> 1
  • arr1[0][2] --> 3
  • arr1[2][3] --> 2
  • arr1[2][0] --> 9
  • arr1[3][3] --> 2

An integer array is pretty much just a matrix.

... not that kind of matrix! Like this one:

That is the basics of a 2D array. Here is a little program I wrote implementing 2D arrays:

#include<iostream>
using namespace std;

int main()
{
    int arr1[][4] = { {1,2,3,4},
                      {5,6,7,8},
                      {9,0,1,2} };

    for(int x = 0; x < 3; x++)
    {
        for(int y = 0; y < 4; y++)
            cout << arr1[x][y] << " ";
        cout << endl;
    }
    
    char str[][10] = { {'T','h','i','s','\0'},
                       {'i','s','\0'},
                       {'a','\0'},
                       {'s','t','r','i','n','g','\0'},
                       {'a','r','r','a','y','\0'} };

    for(int x = 0; x < 5; x++)
        cout << str[x] << " ";
    cout << endl;

    return 0;
}

Output:

[cmw4026@omega test]$ g++ 2d.c
[cmw4026@omega test]$ ./a.out
1 2 3 4
5 6 7 8
9 0 1 2
This is a string array
[cmw4026@omega test]$

Notice how I could print str[x] as a string. A string is just a character array... So a 2D character array is really just an array of strings!

I hope this helped! Leave suggestions in the comments!

Sort:  

This post has been linked to from another place on Steem.

Learn more about linkback bot v0.4. Upvote if you want the bot to continue posting linkbacks for your posts. Flag if otherwise.

Built by @ontofractal

Charlie if you are not following me you should. Not because you need to follow me, but because the things I post on are complimented by your posts. I am not interested in teaching the basics of programming. I'm deep diving into game development and I assume the people that will be doing coding on my tutorials know at least the basics of coding. I am however, currently making an intermediary theoretical post to accompany my Turing Test post and I am pointing them to ALL of your tutorials if they need to follow the basics.

My tutorials are for gamedev in Unity as such I am using C#. That means pointers and such are not particularly useful, but pretty much everything else in C++ carries over. You can use pointers C# if you set your code to unsafe, but I am not going to teach that. (BTW - I miss my pointers... I could do a lot of cool stuff with pointers faster than I can without them)

Hey, that is really great! Thanks a lot! You got a follow! I'll be sure to give you some shout outs back in case the basics bore anybody on my posts to death, haha! Just note that I am not doing tutorials about EVERYTHING, just a lot of the useful basics. For example, I never really explained comments. There are plenty of websites that will teach every single little thing about C++. I'm just trying to cover a lot of things that I think are useful and make it available on Steemit. If you have any suggestions, I'm open. I've been doing C++ because it is super easy for beginners. I can do more C, or anything you think would help people before they get to your tutorials. Just let me know and we can kind of collab.

I personally am language agnostic. I've programmed in so many of them over the years. Yet any of my game development tutorials will be in C#. That is because, currently I choose to use Unity and the choices are C#, a modified version of Javascript (called Unity Script), and a variant of python called Boo. C# has a lot more functionality than the others. I also tend to come from a C, C++ environment so C# other than not getting to use pointers is pretty similar.

I did not want to do computer programming basic tutorials so I am glad you chose to go that route. :) I actually linked all of your tutorials in one of my posts.

Coin Marketplace

STEEM 0.20
TRX 0.12
JST 0.028
BTC 66086.15
ETH 3548.75
USDT 1.00
SBD 2.58