# Introduction to Arrays

## Arrays

An array is a collection of similar types of data items and each data item is called an element of the array. The data type of element may be float, int, char, float, etc. All the elements of the array share the same variable name. But each element has a different index known as a subscript. It is one of the most common data structures used.

Arrays can be single-dimensional or multidimensional. Two-dimensional arrays are known as matrices. So the number of subscripts defined will determine the dimensions of the array.

A simple program to explain the use of the array can be to find the average marks obtained by a class of 30 students in the test. So in this, if you can solve the problem by declaring 30 variables or by creating an array of 30 students. Which is much simpler than creating 30 variables. below is the c code for the program

``````#include<stdio.h>
int main(){
int avg,sum=0;
int i;
int marks[30];//array declaration
for( i=0;i<30;i++){
printf("enter the marks");
scanf("%d",&marks);
}
for(i=0;i<30;i++){
sum=sum+marks[i];
}
avg=sum/30;
printf("average marks is =%d\n",avg);
return 0;
}``````

### Accessing elements of array

Once an array is declared an individual element in it is referred to using marks[0], marks[1], marks[2], etc. The numbers used [] in specifies the position of an element in the array. Array elements are counted starting from 0,1,2. so marks[2] is the third element not the second.

### Array Intialization

``````int num[6]={1,2,3,4,5,6};
int n[]={233,67a};
float gdp[2]={1.1,1.5};``````

This shows arrays can be initialized while declaring them.

The speed of an array data structure makes it perfectly suited for ‬more complex data types, such as hash tables.‭ ‬The predictability of the memory addresses of the elements also can be used to implement very fast array algorithms that can move data quickly.‭ ‬This is particularly useful for sorting operations such as bubble sorts‭ ‬that are perfectly suited for use with arrays.

We will be happy to hear your thoughts
Hello!👋
Can we help you?