#001 DS&A - Operators and Arrays
Tags: algorithms, datastructure
Published: 2020-08-23 10:16:29

# Introduction
I put a schedule to start GO , Algorithms and DS also advanced one with job preparation (and maybe competitive not sure yet) , Clean Coding Series , Linux (first I will start with Unix Design book ).
And the DevOps journey that I already start will be every 2 days new tutorial.
In Case your wondering who is this man in the banner it's the Khwarizmi the inventor of algorithms concept. 
Make sure to follow because I will start more advanced series in future.

****
## Operators in C

![ops](https://dev-to-uploads.s3.amazonaws.com/i/z9khcd7lbdm06yljvoda.png)

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first. -- [source](https://www.tutorialspoint.com/cprogramming/c_operators.htm)

## Arrays

#### One Dimensional Array

in Arrays you can random access the array by an index A[i] , in Linked Lists you can't

![2D1](https://dev-to-uploads.s3.amazonaws.com/i/s1begy8brfm590ybj18i.png)

in order to calculate the adress of A[i] we need to know 2 informations 1 is the size of our type and the base adress of the array , those are the compiler job not us but they are good to know.

address of A[i] = i * sizeof(element) + base_address // in our example base adress is 100

address of A[i] =(i-1)* sizeof(element) + base_address // i-1 if it's start from 1 not 0

sizeof(element) depends on the type in case of integers it's 4 byte (4*8 = 32 bit)

#### Two Dimensional Arrays

if we understand how we can store two dimensional arrays in memory we can write better programs which they are faster
let's take this example of an 2D array with 3 rows and 4 columns
 ```c
  A[3][4] === A: 3x4
        0  1  2  3
     0 [00,01,02,03]
     1 [10,11,12,13]
     2 [20,21,22,23]
 ```
it gonna be complex if the compiler need to use a Linked List to link the rows with columns
so the compiler is going to transform it to one dimensional array , we have 2 methods to do so.

1 : Row major order
after transformation the array will look like this
{00,01,02,03,10,11,12,13,20,21,22,23}

if I need to reach an element I know the address of it by
```c
address of A[i][j] = (((i-start)*N)+(j-start))*size+B 
    
/*
this in case of M rows and N columns
N	 : number of columns
i    : row
j	 : column
start: where array start (usually 0)
size : sizeof(element)
B	 : Base address
*/
```

2 : Column major

{00,10,20,01,11,21,02,12,22,03,13,23}

```c
address of A[i][j] = (((j-start)*M)+(i-start))*size+B 
/*
this in case of M rows and N columns
M	 : number of rows
i    : row
j	 : column
start: where array start (usually 0)
size : sizeof(element)
B	 : Base address
*/
```

We can also do an Binary addressing of 2D Array , wich is super complicated:
assume it's start from 0 and Row major order
{00,01,02,03,10,11,12,13,20,21,22,23}
```c
  address of A[i][j] = (i*2^K+2^L)*2^X+B // Base we also convert it to binary
  N = 2^K   ,   0 <= i <= 2^L - 1 // L is number of binary occupied by N
  M = 2^L	,	0 <= j <= 2^K - 1 // K is number of binary occupied by M
  i*2^K -> i will be shifted by K bits
```
it will occupy in memory L+K+X bits

![2D2](https://dev-to-uploads.s3.amazonaws.com/i/za2vh1yl0iwxegh1apq5.png)

> Note : 2^X * 2^Y + 2^Z = 2^(x+Y)+2^Z

> Note : if the organization is row we implement row , if it's column we implement column. Because it will give us performance.