The magic of Array in C Programming

Array in C Programming

Here  you will learn the basic tutorial of array in c programming with examples.

 

 

What is Array in C? 

An Array is a collection of equal types of elements. It is stored in a continuous format in memory locations. The elements of array can be of int, float, char, or double data type or can be of user-defined data types like structures.
 Array in C Programming

 

 

Advantages and Disadvantages of  Array in C

Advantage of Array

Disadvantage of Array

Code Optimize : write Less code to the access the data.Array Size Fixed : Array size is static(fixed), The memory allocated to array cannot be changed(increased or decreased) after declaration. It is not flexible as link list in Data Structure.
Ease of traversing :  using the loop(for, while, do-while) we can access the elements of an array quickly and easily.
Ease of sorting : need few lines code, to sort array elements easily.
Random  Access : we can access any random element quickly using index number.

 

 

One Dimensional Array in C

A one-dimensional array or 1d array C is a linear data structure that stores a fixed-size sequential collection of elements of the same data type. It is also known as a single-dimensional array. It is declared by specifying the array type followed by the array name and size of the array enclosed in square brackets.
Syntax:
data_type array_name[size];

Example:
int arr[10];

 

Declaration of Array in C 

We can declare the  Array in c language as the following  :
Syntex : data_type array_name[array_size];

Example: int arr[7];

 

Initialization of Array in C

Initialization of the array elements in C is an easy way, by using the index of every element. Initialize every element by using the index.

arr[0]=5;
arr[1]=10;
arr[2]=15;
arr[3]=20;
arr[4]=25;
arr[5]=30;
arr[6]=35;
initialization of Array in C

Example: Initialization of Array

Output
95
79
82
73
86 

 

 

Declaration with Initialization Array in C

In c, we can initialize the array at the declaration time. Let’s see the following code.

int score[5]={10,20,30,40,50};

in this condition, no need to assign size of array, it can also be written as

int score[]={10,20,30,40,50};

 

Example: Declaration with Initialization

Output 

95
79
82
73
86

One Dimensional Array Example with code

Here, I am writing some programs, so that your all concept related to single dimension array would be clear. if still have any problem then write a comment at the last of the article. I will try to resolve with my best.

1. C Program to Sort an Array in Ascending Order

2. C Program to Find Largest Array Element

3. C program to Find Sum and Average of an Array Elements

4. C Program to Search Element in an Array

5. C Program to Insert an Element in an Array

6. C Program to Delete an Element from an Array

7. C program to Remove Duplicate Elements in Array

 

1d Array Assignment work 

Make the following programs to test your knowledge and send code to me in the comment box. If I like, I will publish your code on my coderevise.com with your reference.

1. C program to sort an array in descending order.

2. C program to find duplicates in a list.

3. C program to find second largest number in an array.

4. C program to merge elements of two arrays in third one.

 

Two Dimensional Array in C

What is Two Dimensional Array in C ?

The Two dimensional Array is defined as an array of arrays. It is similar to one-dimensional array.
The two-dimensional array is defined in Matrix format, in that rows and columns play an active role. 2D arrays are used to store the bulk amount of data at once, which can be processed in functions.
two dimensional array in c

Advantages and Disadvantages of 2D Array in C

2D Array Advantages

2D Array Disadvantages

Ease to access : element can be access easily by index number.Array Size Fixed : Array size is static(fixed), The memory allocated to array cannot be changed(increased or decreased) after declaration.
Represent matrix : Two Dimensional array is used to represent matrix.
Store multiple values : It can be used to store multiple values and work with efficiently.

 

Declaration of 2D Array in C

We can declare the Two  Dimensional Array in C as the following :

Syntax : data_type array_name[number of rows][number of columns];

Example : int num[3][3];

 

Initialization of Two 2D Array in C

2D Array initialization can be done at the declaration time of 2D array. Here are some examples of initializing a 2d array :

Method 1 

int num[3][3] = {{5,9,7},{3,9,8},{12,20,6}};

Method 2 

Using for Loop :

for(int i=0 ;i < 3 ;i++)
{
for(int j=0 ;j < 3 ;j++)
{
scanf(“%d”,&num[i][j]);
}
}

 

Two dimensional array example in C

#2. Write a C program to Add Two Matrix using Two dimensional Array.

#3. Write a C Program to Multiply Two Matrices using Two dimensional Arrays.

#4. Write a C program to Transpose a Matrix in C.

 

Difference between 1D Array and 2D Array

difference between 1d Array and 2d array

 

2d Array Assignment work

1. C program to  create a mirror image of two dimensional array

for example:-

mirror image with matrix

2. C program to arrange row elements in ascending order.

sort matrix in ascending

3. C program to check matrix is a sparse matrix or not

Check out our other C programming Examples

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top