Ek array same data type ke elements ka fixed-size sequenced collection hota hai. Ye ek simple grouping of like-type data hota hai. Ek array ka use kisi numbers ya names ki list ko represent karne ke liye hota hai. Some examples jaha par array use hota hai:
- List of temperatures recorded every hour in a day, or a month, or a year.
- List of employees in an organization.
- List of products and their cost sold by a store.
- Test scores of a class of students.
- List of customers and their telephone numbers.
- TAble of daily rainfall data.
and so on.
So ek array hame data represent karna ka convenient structure provide karta hai, ye ek data-structure bhi hota hai. Jaise ki hamne pehle baat ki, ek array related data items ka sequenced collection hota hai jo ki ek common name share karte hai. For example ham array name salary ko use kar sakte hai to represent set of salaries of a group. Ham individual salary ko represent kar sakte hai by writing a number called index or subscript in brackets after the array name. For example, salary [10] ye represent karta hai 10th employee ki salary. Complete values ke set ko array, aur individual values ko elements kehte hai.
Why We Should Use Array
Array ka use karke ham efficient programs likh sakte hai. For example hame 100 students ke name store karna hai to ham kis tarah karenge ? kiya ham 100 variables create karenge like stu_1, stu_2, ..., stu_100. Obviously nhi kioki ye bahut hi complex hai aur time-consuming hoga, instead of this ham ek array banayenge of size 100. Har element par ham ek value store karenge.
Example:
string student[100];
student[0] = "Aman";
student[1] = "Nitin";
student[2] = "Sam";
.......... ... .........
.......... ... .........
student[99] = "Beyond Man";
Note: Array ki indexing 0th se start hoti hai aur last element nth-1 hota hai. (n is the size of array).
Types of Array :
- One Dimensional Array
- Two Dimensional Array
- Multi Dimensional Array
ONE-DIMENSIONAL ARRAYS
One Dimensional array ek variable hota hai jo list of items ko represent karta hai using the index ya single-subscript. For example : Agar ham 5 number ke set ko represent karna chahte hai eg. {10, 20, 30, 40, 50} to ham ek array bana sakte of size 5.
Declaration of 1-D Array
1-D Array ko declare karne ke liye ham ' data_type array_name[size]; ' ka use karte hai yaha par koi sa bhi data type ho sakta hai like Derived, Fundamental, and User-Defined. Array name ko bhi valid identifier ho sakta hai. Size totally hamari requirement par depend karta hai.
Syntex: int employee[30].
Example:
#include<stdio.h>
int main(){
int marks[5] = {89, 83, 93, 78, 77};
printf("Marks
");
for(int i = 0; i < 5; i++)
{
printf("%d ", marks[i]);
}
printf("
");
return 0;
}
Output:
Marks
89, 83, 93, 78, 77
Explanation :
- Pehle hamne ek integer type ka array banaya jiska size hai '5' aur use compile time par hi initialize kar diya with values of {89, 83, 93, 78, 77}.
- Then hamne ek for loop likhi jo 0 to less then 5 matlab 4 tak.
- for loop ki body mein har baar marks ki each value ko print kar diya using the marks[i]. Hamne yaha par i ko as a subscript use kari hai jo har baar increment hokar next element ko access karne mein help karegi.
TWO DIMENSIONAL ARRAYS
Two Dimensional Array mein ek variable hota hai jo list of items ko represent karta hai using the two index ya (double subscript). 2D Array mein data row and column ke format mein store hota hai. For example 4x4, isme 4 rows and 4 columns hai.
[
[0][0], [0][1], [0][2], [0][3],
[1][0], [1][1], [1][2], [1][3],
[2][0], [2][1], [2][2], [2][3],
[3][0], [3][1], [3][2], [3][3]
]. Ham 2-D array mein data ko matrix ki form mein visualize karte hai, lekin actual computer memory mein data linearly hi store hota.
Declaration of 2-D Array
2-D Array ko declare karne ke liye ham row and column dono ke size ko define karte hai. data_type array_name[row][column];
Syntex: int matrix[4][4];
Example:
#include <stdio.h>
int main()
{
int matrix[4][4] = {{2, 4, 6, 8},
{1, 3, 5, 7},
{8, 9, 4, 5},
{3, 4, 5, 6}};
printf("Matrix
");
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("%d ", matrix[i][j]);
}
printf("
");
}
return 0;
}
Output
Matrix
2 4 6 8
1 3 5 7
8 9 4 5
3 4 5 6
Explanation :
- Pehle hamne 2-D array ko declare aur initialilze kara, yaha hamne two bracket ka use kara hai kioki first bracket for row and second bracket for column, aur phir curly braces ka use karke '{}' initialize kar diya.
- Then yaha hamne two for loops ka use kara hai first i row par iterate karega aur j column par iterate karega.
- for loop ki body mein ham matrix[i][j] ki value ko print kar rahe hai.
MULTI DIMENSIONAL ARRAYS
Multi Dimensional Array ek variable hota hai jo list of items ko represent karta hai using the more than two index or (multi-script). Multi-Dimensional Array ki general form data_type array_name[size][size][size]....;
Syntex: int cube[2][2][2], here cube is multi-dimensional array.
Comments