Description

What is an Array?

An Array is the list of finite number of elements of same data types. For Example: interger, string, float etc.

An Array can be defined as the collection of the sequential memory location.

Syntax of declare the array.

datatype Array_name[size_of_array];

datatype specifies the type of the elements that will be contained in the array, such a int, char, etc. And size_of_array indicates the maximum number of elements that can be stroe in the array.

For Example:

int arr[10];

Here int is the data type and size if 10.

Types of Array.

  1. One-dimensional Array
  2. Two-dimensional Array
  3. Multidimensional Array

One Dimensional Array

An array that is represented in a only one dimension would be a one-dimension Array. One Dimensional array (linear array) is a set of finite numbers of homogenous data elements.

Declaration of one-dimensional Array

type array_name[size_of_array];

For example:

int A[20];

Two-dimensional Array

An array that is represented in two dimension that called the two dimension array.

Declaration of two-dimnesional array

Type array_name[row_size][column_size];

Tow dimensional array are defined in much same manner as one-dimensional array, except that a separate pair of square bracket is required for each subscirpt, Thus, a two-dimensional array will require two pairs of square brackets.

Two-dimensional array as stored in memory, as show

Two dimensional array representation

Multidimensional Array

C allows array of three or more dimensions. The exact limit is determined by the compiler. The general form of a mutli-dimensional array is:

Declaration of Multidimensional array

type arr_name[s1][s2][s3][s4]....

Where sis the ith dimension.

For Example: int arr[2][3][4];

Operations on arrays:

The operation performed in linear array.

  1. Create Array: To create the array.
  2. Traversal Array: Precessing each elements in the array.
  3. Search: Finding the element in an array with the given value or given key.
  4. Insertion: Add a new element in an array.
  5. Deletion: Delete the existing in ascending or descending order.
  6. Merging: Combining two array within in single array.

Initialization of one-dimensional arrays

After an array is declared,  its elements must be initialized. Otherwise, they will contain the "garbage value". An array can be initialized at either of the following stages:

  • Compile time initialization: We can initialize the elements of arrays in the same way as the ordinary variables when they are declared.
  • Runtime initialization: An array can be explicitly intialized at runtime. This approach is usually applied for initalizing large arrays.

Initialization of two-dimensional arrays

Two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed braces. For exampe: int arr[2][3]; initialized the elements of the first row to zero and the second row to one. The initialization is done row by row.

Write a program to implement an array in c programming.

#include<stdio.h>

int main(){
int arr[10];
printf(“Enter the Elements in array : “);
for(int i=0; i<10; i++){
	scanf(“%d”, &arr[i]);
}	
printf(“Elements in array:”);
for(int i=0; i<10; i++){
	printf(“%d”, arr[i]);
}
return 0;
}

Output: 

Enter the Elements in array: 3 4 1 5 6 3 8 9 10 3
Elements in array: 3 4 1 5 6 3 8 9 10 3

 

Comments

Login is mandatory to comment Please login

Recommended Posts

Break and Continue Statement in c

Break statement is usually used to terminate a case in the switch statement. In c programming language the continue statement works...

Function in C | What is Functions?

Function like main(), printf() and scanf(), we have already come through. All c Programs made up of one or more function.