#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
#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
#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;
}