In this article array in c we give the information about an array is a fixed-size indexed collection of elements of the same data type. There are three types of array.

Introduction:

So far we have used only basic data types, such as char, int, float, double and variations of int and double. Although these types are very useful, they are limited due to the fact that variables of this type can store only one value at a time. Therefore, they can be used to handle a limited amount of data.

However, in many applications, we have to handle a large amount of data; We need a powerful data type that facilitates efficient storage, access and handling of data items. C supports a derived data type known as an array that can be used for such applications.

Definition:

“Array is a fixed-size indexed collection of elements of the same data type”.

Some examples where the concept of array can be used:

  1. List of employees in the organization.
  2. List of products sold by the store and their price.
  3. Student class test marks
  4. List of customers and their telephone numbers.

Types of Arrays in c:
There are three types of arrays such as follows:

One-dimensional arrays

Two-dimensional arrays

Multidimensional arrays

Advantages of C array

1) Code Optimization: Less code to access data.

2) Easy to traverse data: Using for loop, we can easily retrieve array elements.

3) Easy to sort data: To sort the elements of an array, we only need a few lines of code.

4) Random Access: We can access any element randomly using array.

Disadvantages of C array

1) Fixed size: Whatever the size, we define when declaring an array, we cannot exceed the limit. Therefore, it does not dynamically resize like the linked list which we will learn later.

One-dimensional array:-

Definition:

“A list of objects can be named a variable using only one subscript, and such a variable is called a single-subscribed variable or one-dimensional array.”

Declaring a one-dimensional array:

Like any other variable, arrays must be declared before they can be used so that the compiler can give them space in memory.

Syntax: Return_Type variable-name [size];

The type specifies the type of element that will be contained in the array, such as int, float or char and the size indicates the maximum number of elements that can be stored inside the array.

For example,

float height [50];

Declares height as an array with 50 actual elements. Any membership from 0 to 49 is valid.

Initiation of a Dimensional Array:

After declaring an array, its components must be started. Otherwise, they will guess the “garbage”. An array can be started in one of the following steps:

  • At compile time
  • At run time

Compile Time Initialization:

     We can start array elements as defined by common variables.

Syntax:

data-type array-name [size] = {List of Values};

The values ​​in the list are separated by commas. For example, the statements
int number[3]={0,0,0};

Variable number 3 will declare the size as an array and assign zero to each element. If the number of values ​​in the list is less than the number of elements, as many elements as possible will be initiated. The remaining elements will be automatically set to zero.

For example

Float tot [5] = 1.1,2.3,3.5

The first three elements will start at 1.1, 2.3 and 3.5 and the remaining two elements at zero.

Run time Initiation:

An array can be explicitly at run time. This approach is usually applied for initializing large arrays.

Program:
#include<stdio.h>
int main()
{
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf(“%d \t”,marks[i]);
}//end of for loop
return 0;
}

Out/Put:- 

80            60         70             85           75

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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