> For the complete documentation index, see [llms.txt](https://mun.gitbook.io/c/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mun.gitbook.io/c/c/ch7/array-fundamental-concept/assignment.md).

# Assignment

## Element Assignment

```c
#include<stdio.h>
#define TOTAL_SIZE 5
int main(){
    int a[TOTAL_SIZE], current_size = 0, i = 0;
    
    // Assignment of Array Elements
    a[0] = 10;
    current_size++;
    // current_size of the Array will increase by 1 after each element insertion
    
    a[1] = 20;
    current_size++;
    
    a[2] = 30;
    current_size++;
    
    a[3] = 40;
    current_size++;
    
    a[4] = 50;
    current_size++;
    // Atmost 5 (TOTAL_SIZE) Element can be Inserted in the above Array
    
    /*
    a[5] = 50; // Invalid Assignment
    current_size++; // Current is more than the TOTAL_SIZE of the Array
    */
    
    for(i=0; i<current_size; i++){
        printf("%d ", a[i]);
    }
    printf("\n");
    
    return 0;
}
```

Elements are assigned/inserted in the array in a sequential order. The first element is assigned to index 0, the next element to index 1, and this goes on. We should not skip an index while assigning/inserting elements in the array.

### Alternative Approach

```c
#include<stdio.h>
#define TOTAL_SIZE 5
int main(){
    int a[TOTAL_SIZE], current_size = 0, i = 0;
    
    // Assignment of Array Elements
    a[current_size++] = 10;    
    a[current_size++] = 20;    
    a[current_size++] = 30;    
    a[current_size++] = 40;    
    a[current_size++] = 50;
    
    /*
    a[current_size++] = 50; // Invalid Assignment // Current is more than the TOTAL_SIZE of the Array
    */
    
    for(i=0; i<current_size; i++){
        printf("%d ", a[i]);
    }
    printf("\n");
    
    return 0;
}
```

### Alternative Approach Using Loop

```c
#include<stdio.h>
#define TOTAL_SIZE 5
int main(){
    int a[TOTAL_SIZE], current_size = 0, i = 0;
    
    // Assignment of Array Elements
    for(i=0; i<TOTAL_SIZE; i++){
       a[current_size++] = 10 * i; 
    }
    
    for(i=0; i<current_size; i++){
        printf("%d ", a[i]);
    }
    printf("\n");
    
    return 0;
}
```

## Element Update

```c
#include<stdio.h>
#define TOTAL_SIZE 5
int main(){
    int a[TOTAL_SIZE] = {10, 20, 30, 40, 50}, current_size = 5, i = 0;
    
    for(i=0; i<current_size; i++){
        printf("%d ", a[i]);
    }
    printf("\n");
    
    a[0] = 100;
    a[1] = 200;
    a[2] = 300;
    
    printf("\nAfter Array Element Update\n");
    for(i=0; i<current_size; i++){
        printf("%d ", a[i]);
    }
    printf("\n");
    
    return 0;
}
```
