Hello friends today we are going to start new topic of c language name is ARRAY
Today, in this post i will tell describe you that what is array and where we use this? So now start this topic and friends please ask your doubts in comment if you have any kind of problem.
Array:
Array is a collection of similer type of elements stored in continuous memory allocation.
Mean we can store only same type value in array after declare that. If we declare that or integer we can only store integer number in it,if we declare for float we can store any float number.
But we know float can store any integer value but int can't store any float value so after declare for int we will store only integer value.
If we store any digit in array it store continuously in memory so we can easily find address of any array element. When we declare any variable it store any where in memory but when we use array for declare many variable it allocate continuously in memory.
Type of array:
There are two type of array-
- One dimensional array
- Multi dimensional array
Many people say array are three type and some say array have two type both are correct because who say three type array they say one dimensional ,two dimensional and then multi dimensional but who say two they take two dimensional and multidimensional together.
One dimensional array:
One dimensional array only on one row. In this we use only one loop for take input.
when dimensional increase loop counts will also increase this is easy way to learn.
Declaration of one-d array:
syntax:
data type array name [size off array];
for ex:
int arr[100];
int sum[50];
The size of array should be know during compile time.The size of the array should be an integer value.
Initialization:
int arr[3]={1,2,3};
int arr2[ ]={3,3,4,5,2,6};
int arr3[ ]= {3,6,8};
Initialization of array we can do by using curly bracket.we have to write in equal than give every elements in bracket.
Programs(array):
write a program to read and display n numbers using an array :
#include<stdio.h>
main()
{
int i=0,n;
int arr[50];
printf("\n Enter the numbers of elements:");
scanf("%d",&n);
printf("\n Enter the elements:");
for(i=0;i<n;i++)
{
printf("\n Arr[%d]=",i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are:");
for(i=0;i<n;i++)
{
printf("Arr[%d]=%d\n",i,arr[i]);
}
}
Find largest and smallest numbers in an array:
#include<stdio.h>
main()
{
int i,n,arr[20],small,temp;
int small,small_pos;
int large,large_pos;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
small=arr[0];
small_pos=0;
large=arr[0];
large_pos=0;
for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small=arr[i];
small_pos=i;
}
if(arr[i]>large)
{
large=arr[i];
large_pos=i;
}
}
printf("\n The smallest number is %d",small):
printf("\and position of small number is %d",small_pos);
printf("\n The largest number is %d",large);
printf("and position of largest number is %d",large_pos);
}
Thanks for reading.....................
Keep supporting
Comments
Post a Comment