• An array is a collection of data item of same type.
• Items are stored at contiguous memory location.
• It can also store the collection of derived data types such as pointers, structures etc.
• A one-dimensional array is like a list.
• A two dimensional array is like matrix.
• ARRAY ALWAYS STARTS WITH 0.
NECCESSITY OF ARRAY:
• Organized and readable.
• If you want to store marks of 100 students, creating 100 different variables makes program ridiculous or messy : solution creating array of size 100 might efficient.
SYNTAX FOR DECLARING AND INITIALIZING ARRAY
• Datatype name[size];
• Datatype name[size]= {x,y,………};
• Datatype name [rows][columns]; //for 2d array
• Initialize the array one by one by accessing it using its index
o name[0]=2;
o name[1]=4; and so on
where 0,1.... is index number
SOME BASIC EXAMPLES:
void main()
{
int marks[2]={25,50}; // intitialization
printf("marks of student 1 is %d\n",marks[0]);
printf("marks of student 2 is %d",marks[1]);
}
marks of student 1 is 25
marks of student 2 is 50
* It is clear that ; each element of array can be access with index number
#include<stdio.h>
void main()
{
int i,num[5];
//to input
printf("enter the 5 values\n ");
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
// to display
printf("content of array are \n");
for(i=0;i<5;i++)
{
printf("%d",&num[i]);
}
}
OUTPUT
enter the 5 values
1
2
3
4
5
content of array are
1
2
3
4
5
THANKS !
void main()
{
int i,num[5];
//to input
printf("enter the 5 values\n ");
for(i=0;i<5;i++)
{
scanf("%d",&num[i]);
}
// to display
printf("content of array are \n");
for(i=0;i<5;i++)
{
printf("%d",&num[i]);
}
}
OUTPUT
enter the 5 values
1
2
3
4
5
content of array are
1
2
3
4
5
THANKS !
0 comments:
Post a Comment