In c programming user can create a set of similar types of data. That is call Array. Arrays is a set where uses can add data, delete data and fetch data when it is needed. There is three types of Array,
Single-Dimensional Array
Two-Dimensional Array
Multi-Dimensional Array
Here we are going to talk about Single-Dimensional Array
Let’s write a program to understand Array
Now once we declared the elements it is stored inside the array with an index. In array index of first element is start with “0” then it will increase by “1” for next element. As we can see in this example that “a[0]” holds the value “23” which is the first element declared in that array.
Now let’s see the output
Let’s see the output
Single-Dimensional Array
Two-Dimensional Array
Multi-Dimensional Array
Here we are going to talk about Single-Dimensional Array
Let’s write a program to understand Array
#include<stdio.h>
void main()
{
int a[5]={23,5,4,33,56};
printf("%d\n",a[0]);
printf("%d\n",a[1]);
printf("%d\n",a[2]);
printf("%d\n",a[3]);
printf("%d\n",a[4]);
}
In above program we execute a simple example of array. Here the syntaxvoid main()
{
int a[5]={23,5,4,33,56};
printf("%d\n",a[0]);
printf("%d\n",a[1]);
printf("%d\n",a[2]);
printf("%d\n",a[3]);
printf("%d\n",a[4]);
}
int a[5]={23,5,4,33,56};
Says it is a declaration of an Array. Writing the data type before “a[]” says what type of data it is. An Array can be “int”, “float”, “char” type. Here Array of character is called as “String”. When we write “a[5]” the compiler understand it is an Array by “[]”. The number inside this array declare that how many elements we are going to put in this array. Data types of all that element is same as data types of array. We can’t put integer type and float type data together in a array.Now once we declared the elements it is stored inside the array with an index. In array index of first element is start with “0” then it will increase by “1” for next element. As we can see in this example that “a[0]” holds the value “23” which is the first element declared in that array.
Now let’s see the output
23
5
4
33
56
In first example we write an simple program where element of array is already written. There as a way that we can get value of elements dynamically with the help of Loop Structure. Let’s write a program for that5
4
33
56
#include<stdio.h>
void main()
{
int i,a[5];
printf("Enter the value inside array \n");
for(i=0;i<5;i++)
{
printf("enter the value ");
scanf("%d",&a[i]);
}
printf("The output of array\n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
}
In this program we take value form user and show the output. Using for loop we put value inside array using “i” as index. We can enter as many element as we want in a Array.void main()
{
int i,a[5];
printf("Enter the value inside array \n");
for(i=0;i<5;i++)
{
printf("enter the value ");
scanf("%d",&a[i]);
}
printf("The output of array\n");
for(i=0;i<5;i++)
{
printf("%d\n",a[i]);
}
}
Let’s see the output
Enter the value inside array
enter the value 25
enter the value 12
enter the value 65
enter the value 36
enter the value 17
The output of array
25
12
65
36
17
enter the value 25
enter the value 12
enter the value 65
enter the value 36
enter the value 17
The output of array
25
12
65
36
17


0 comments:
Post a Comment