Arrays

Introduction

Array:  An array is a collection of data items, all of the same type, accessed using a common name

A one-dimensional array is like a list;  A two dimensional array is like a table;  The C language places no limits on the number of dimensions in an array, though specific implementations may.

Declaring Arrays :  

  1. Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array
  2. Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within the square brackets.
  3. Dimensions used when declaring arrays in C must be positive integral constants or constant expressions.
format: data_type arr_name[size]
 Example : int arr[10];
           float arr1[10];
           char arr2[10];

 

Initializing Arrays

  1. Arrays may be initialized when they are declared, just as any other variables.
  2. Place the initialization data in curly {} braces following the equals sign.  Note the use of commas in the examples below.
  3. An array may be partially initialized, by providing fewer data items than the size of the array.  The remaining array elements will be automatically initialized to zero.
  4. If an array is to be completely initialized, the dimension of the array is not required.  The compiler will automatically size the array to fit the initialized data

Example :  

 

int A[10] ={1, 2, 3, 4, 5};

Note: // remaining elements which are not intialize are default zero (from index 5 to 9 in above array)

 

Array types

1-D Array Implementation :

#include<stdio.h>
int main() {
  int n;
  scanf("%d",&n); // size of the array
  int arr[n];
  // taking input 
  for(int i=0;i<n;i++) {
    scanf("%d",&arr[i]);
  }
  //printing arr elements 
  for(int i=0;i<n;i++) {
      printf("%d",arr[i]);
  }

 return 0;
  
}