About Lesson
Array:
Previously we used Variables to Store the Values. To use the Variables, we have to declare the Variable and initialize the Variable i.e., assign the Value to the Variable, suppose there are 1000 Variables are present, So, it is a tedious process to Declare and Initialize each and every Variable and also to handle 1000 Variables. To Overcome this Situation, we use the concept ‘Array‘.
- In an ‘Array‘, Values of same type are Stored.
- An ‘Array‘ is a group of Memory Locations related by the fact that they all have the same Name and same Type.
- To refer to a Particular Location or Element in the ‘Array‘ we specify the Name of the ‘Array‘ and Position Number of Particular Element in the ‘Array‘.
- There are mainly three types of ‘Array‘:
- One Dimensional Array
- Two-Dimensional Array
- Multi-Dimensional Array
Types Of Array:
- One Dimensional Array: A one-dimensional array is a linear collection of elements, all of the same data type, arranged in a sequence. It is a fundamental data structure in computer programming and is often used to store and manipulate a fixed-size sequential collection of elements. Each element in the array is identified by its index or position within the array, starting from 0.
- Two-Dimensional Array: A two-dimensional array is a data structure that organizes data in a grid or matrix format, with rows and columns. Unlike a one-dimensional array, which is a linear collection of elements, a two-dimensional array allows you to represent and manipulate data in a two-dimensional space.
- Multi-Dimensional Array: A multi-dimensional array is a data structure that organizes data in two or more dimensions. In programming, arrays are used to store and manipulate collections of data of the same type. A one-dimensional array is essentially a list, while a multi-dimensional array extends this concept to two or more dimensions.
Array Implementation Rule:
1. Declare The Array:
Syntax:
datatype arrayname[size];
Example:
int a[5]; // a has 5 element
a | | | | | |index 0 1 2 3 41st element at 0th positionLast element at size-1 positionindex is used to access array element
2. Assignment:
Put value into array element using index
Syntax:
a[index]=value;
Example:
a[0]=10;a[1]=4;a[2]=7;a[3]=9;a[4]=2;a | 10 | 4 | 7 | 9 | 2 |index 0 1 2 3 4
3. Using:
printf(“n%d”,a[0]);printf(“n%d”,a[1]);printf(“n%d”,a[2]);printf(“n%d”,a[3]);printf(“n%d”,a[4]);
Complete Code:
#include<stdio.h>#include<conio.h>void main(){int a[5];clrscr();a[0]=10;a[1]=4;a[2]=7;a[3]=9;a[4]=2;printf(“n%d”,a[0]);printf(“n%d”,a[1]);printf(“n%d”,a[2]);printf(“n%d”,a[3]);printf(“n%d”,a[4]);getch();}
Output:
10
4
7
9
2