Understanding Arrays
Tags: c, codenewbie, beginners
Published: 2022-02-15 08:15:55

##Arrays
**Array** is a linear data structure that stores several elements of the same data type.
It can also be defined as a collection of data of the same data types.

###Syntax

`data_type array_name[SIZE]` e.g `int data[10]`

The location that is created is contiguous, i.e they are next to one another as shown below.
![Array](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kqne566j851k12imu3g.gif)
Its size is usually constant, i.e it can't be adjusted once declared.
One must specify the size of the array before use.

###Methods of Declaring Array

```
#define SIZE 10
int data[SIZE];
```
OR

```
const int SIZE = 10;
int data[SIZE];
```
An array can also be initialized **statically** or **dynamically**. In static initialization, the array is assigned values when it is declared, i.e at the compilation time while in dynamic initialization, they are assigned values when the program is running, i.e at the runtime.

An array can also be of one dimension, two dimension e.t.c e.g `int data[10]`, `int data[10][20]`, `int data[5][2][7]` etc.

---

##Single Dimention Array

###Initialization of array
`int a[] = {45,10,25,24,78};` or `int a[5] = {45,10,25,24,78};`  or
```
int a[5];
a[0] = 45;
a[1] = 10;
a[2] = 25;
a[3] = 24;
a[4] = 78;
```
The above codes are examples of  **Static array** while the one below where the user input values at runtime is **dynamic**.
###Printing of array elements
```
#include<stdio.h>
int main()
{
    int i, a[5] = {45,10,25,24,78};
    for(i=0;i<5;i++)
    {
        printf("Value at %d is %d\n",i, a[i]);
    }
}
```
**output**
![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20tqls6a3z6elmnbce7p.png)

###User input into an array

```
#include<stdio.h>
int main()
{
    int i, a[5];
    for(i=0;i<5;i++)
    {
        printf("[+]Enter value at position %d >>> ",i);
        scanf("%d",&a[i]);
    }
    printf("\n*****OUTPUT*****\n");
    for(i=0;i<5;i++)
    {
        printf("Value at %d is %d\n",i, a[i]);
    }
}
```
**output**
![User input](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ouuwli6y2685j0p7qc9v.png)

###Operations Associated with Array

- Traverse
- Delete
- Insert
- Search
- Sort
- isEmpty
- isFull etc

**Traverse Operations:** Enables one to go through the entire array either outputting, counting the elements, etc. The maximum amount of time that a traverse operation can take is in the order of 1, **O(1)**.
**Deletion Operations:** This is an expensive operation in an array because elements must be shifted backward. The maximum amount of time taken is in the order of n, O(n) and the minimum in the order of 1, O(1).
**Insertion Operations:** Just like deletion it is also expensive because elements are to be shifted while inserting elements except when the elements are not in a given order.
**Search Operation:** Enables one to search for an element in an array and begins at the first location. The maximum time taken is in order of n and the minimum in the `order of n` in case of `linear search`and `order of log n` in the case of `binary search`. Searching can either be `linear` or `binary`.
**Sorting Operation:** This enables one to arrange elements in either ascending or descending orders. There are various ways of sorting out elements in an array e.g, merge sort, quick sort, bubble sort, shell sort, selection sort and insertion sort.
**isEmpty Operation** returns true when the array is empty and false otherwise.
**isFull Operation** returns true when the array is full and return falls otherwise.

**Order of 1,**`O(n)` - Means time taken is constant irrespective of the size of the array or any other type of data structure.
**Order of n,**`O(n)` - Mean time taken is dependent of the size of the array ,i.e the larger the size the longer the time taken and verse-verse.

###Format of Operations implementation

```
/*
OPEN DOCUMENTATION
Author: *************
Description: ************
Date: ***********
Version: ***
Contact: **********
*/
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define SIZE 10
int data[SIZE];

void traverse();
void delete_begin();
void delete_at_point(int);
void delete_end();
void insert_begin(int);
void insert_at_point(int, int);
void insert_end(int);
void search(int);
void sort_asc();
void sort_dec();
bool isFull();
bool isEmpty();

int main(){
    //menu
    int option,item,pos;
    printf("\n\n******MENU******\n-----------------------+");
    printf("\n1. Traverse Operations");
    printf("\n2. Delete Operations");
    printf("\n3. Insert Operations");
    printf("\n4. Search Operations");
    printf("\n5. Sort Operations");
    printf("\n6. IsFull Operation");
    printf("\n7. IsEmpty Operation");
    printf("\n\n");
    printf("[+]Select an option >>> ");
    scanf("%d",&option);
    if(option == 1){
        printf("\n******Traverse Operation******\n");
        traverse();
    }
    else if(option == 2){
        printf("\n******Delete Operation******\n");
        printf("\n1. Delete 1st Element");
        printf("\n2. Delete Element at a point");
        printf("\n3. Delete last Element");
        printf("\n\n[+]Select an option >>> ");
        scanf("%d",&option);
        if(option == 1){
            delete_begin();
        }
        else if(option == 2){
            printf("\n[+]Enter position >>> ");
            scanf("%d",&pos);
            delete_at_point(pos);
        }
        else if(option == 3){
            delete_end();
        }
        else{
            printf("Invalid Choice, try options 1 to 3");
        }
    }
    else if(option == 3){
        printf("\n******insert Operation******\n");
        printf("\n1. Insert 1st Element");
        printf("\n2. Insert Element at a point");
        printf("\n3. Insert last Element");
        printf("\n\n[+]Select an option >>> ");
        scanf("%d",&option);
        if(option == 1){
            printf("[+]Enter value to insert >>> ");
            scanf("%d",&item);
            insert_begin(item);
        }
        else if(option == 2){
            printf("[+]Enter value to insert >>> ");
            scanf("%d",&item);
            printf("\n[+]Enter position >>> ");
            scanf("%d",&pos);
            insert_at_point(item,pos);
        }
        else if(option == 3){
            printf("[+]Enter value to insert >>> ");
            scanf("%d",&item);
            insert_end(item);
        }
        else{
            printf("Invalid Choice, try options 1 to 3");
        }
    }
    else if(option == 4){
        printf("\n******Search Operation******\n");
        printf("[+] Enter element to search for >>> ");
        scanf("%d",&item);
        search(item);
    }
    else if(option == 5){
        printf("\n******Sort Operation******\n");
        printf("\n1. Sort in Ascending Order");
        printf("\n2. Sort in Descending order");
        printf("\n\n[+]Enter Option >>> ");
        scanf("%d",&option);
        if(option == 1){
            sort_asc();
        }
        else if(option == 2){
            sort_dec();
        }
        else{
            printf("\n\nInvalid Option!! Try with option 1 or 2!\n\n");
        }
    }
    else if(option == 6){
        printf("\n******isFull Operation******\n");
        isFull();
    }
    else if(option == 7){
        printf("\n******isEmpty Operation******\n");
        isEmpty();
    }
    else{
        printf("\n\nInvalid Options, Try a number between 1 and 7\n\n");
    }

    printf("~BY PROFF JEFF~\n");
    return 0;
}
void traverse(){
//body
}
void delete_begin(){
//body
}
void delete_at_point(int){
//body
}
void delete_end(){
//body
}
void insert_begin(int){
//body
}
void insert_at_point(int, int){
//body
}
void insert_end(int){
//body
}
void search(int){
//body
}
void sort_asc(){
//body
}
void sort_dec(){
//body
}
bool isFull(){
//body
}
bool isEmpty(){
//body
}
```

---

##Double or Two Dimension Array
A two-dimensional array is similar to a one-dimensional array, but it can be visualised as a grid (or table) with rows and columns.
###Syntax
```
#include<stdio.h>
int main()
{
    int a[3][3];
}
```
Here `a` is the name of the array
`int` is the data type of the array
`size` of the array is `3*3` meaning we can store a maximum of `9` items or values in the array.

###Initialization of array

```
int a[3][3] = {
        {40,50,60},
        {10,20,30},
        {70,80,90}
    };
```
###Displaying the values

```
#include<stdio.h>
int main(){
    int i,j, a[3][3] = {
        {40,50,60},
        {10,20,30},
        {70,80,90}
    };
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
}
```
**Output**
![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1ivsaftxu8zvw4eo9uu.png)

###Taking input from User

```
#include<stdio.h>
int main(){
    int i,j, a[3][3];
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("[+]Enter value at [%d][%d] >>> ",i,j);
            scanf("%d",&a[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
}
```
**Output**
![Output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/um41iu5p563jbu7uaw4k.png)

After Array we will also check on other linear data structures such as the stack, queue, linked-list and there implementation.