What is the difference between a one-dimensional and two-dimensional matrix?

3

In high school we usually study about the concept of Matrix, which consists of a row table and columns forming a set of numbers or elements.

In mathematics, programming and other areas we use Matrices so I would like to know the difference between one-dimensional and two-dimensional matrix?

How to declare a one-dimensional array and a two-dimensional array?

  

I would like examples in some languages, eg java , php , c ++ if   possible.

    
asked by anonymous 04.07.2016 / 03:48

3 answers

7

One-dimensional array has only one dimension. Also called vector. In pseudolanguage:

var vetor = inteiro[10];

In Java:

int[] vetor = new int[10];

In C ++:

int vetor[10];

Two-dimensional array has two dimensions. In pseudolanguage:

var matriz = inteiro[10, 10];

In Java:

int[][] vetor = new int[10][10];

In C ++:

int vetor[10][10];
    
04.07.2016 / 03:53
4

One-dimensional array, also treated as a "vector", stores data sequentially, and each data is stored and retrieved by an integer representing its "position" in this "row". By representing a "straight", you can say that the data is stored in a single dimension.

The statement will depend on the language, usually:

Dim Vetor(15)

In this example, we created a vector (named "Vector") that contains 16 elements (distinct data), since every vector starts from 0 (zero).

The two-dimensional matrix stores data by means of two integers, representing the "position" of each die in the matrix, as in a Cartesian axis where, for example, points can be considered as integers and positive numbers ( including zeros). Thus, each pair of values indicates a different die. Since they represent two axes, which form a "plane" (x and y in geometry), they represent values in two dimensions.

Usually the statement would be:

Dim Matriz(5; 4)

In this example we created an array (named "Matrix") that contains 30 elements (distinct data), because as 0 (zero) values must be considered, the amount of items this array can store is calculated by: 6 X 5 = 30.

    
04.07.2016 / 04:14
2

Well the answers are above, I just wanted to put a drawing I think will contribute to the understanding.

Imagine I want to store people's data.

One-dimensional array:

João | Vitor | Pedro | Adriana

As you can see above would be complicated to store the age, sex, etc. of the same person, because I only have one line in the matrix. The two-dimensional array solves this as well, since each row in the array now has one person's data.

Two-dimensional array:

Then it will look like this:

João | 18 | Masculino | Desenvolvedor

Vitor | 22 | Masculino | Analista

Pedro | 35 | Masculino | Arquiteto

Adriana | 24 | Feminino | Web Design

The difference is that the one-dimensional array has only one dimension, just one line.

    
04.07.2016 / 15:26