INTRODUCTION TO ARRAY

WHAT IS ARRAY ?

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:


  • To aware about memory allocation and storing data according to index:#include<stdio.h>
    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]);

    }

    OUTPUT
    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 


  • To input the values in array and display them:

  • #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 !



    SHARE

    Milan Tomic

    Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

    • Image
    • Image
    • Image
    • Image
    • Image
      Blogger Comment
      Facebook Comment

    0 comments:

    Post a Comment