Array: Part 1

An array is a collection of data items, in which all of the data items have the same datatype, accessed using a common name. A 1-D array is like a list. A 2-D array is like a table. An array is used to store a lot of data's at a time.

LOGO

Why Array ?


Let it understand by an example:-

Consider their is a school in which there are 20 sections and in each section there are 50 students. They have to collect the marks of each and every student (1000 Students) in the school.

For that we have to make a program which has 1000 different variables to store the marks of each and every students. Also we have to remember the variable name that we have assigned. This is a very difficult task to perform, and this is just a small scale problem. Consider if we have to store the marks of 10,000 students, that becomes almost impossible.

To overcomes these type of problems, idea of Array is introduced.
Array can store as much as data you want to store in a single variable. 
For example: int a[ 5 ]; //here 'a' can store five values.

Syntax: 1-D Array- datatype array_name[ size of the array ] ;
             Let the array be-
             int arr[ 5 ];
Infrastructure of Array

⧫ In an array the index starts with 0 i.e, if the number of element in an array is N, then the indexes starts with 0 and ends in N-1.
⧫ "arr" always stores the base address of the block which the array is pointing i.e, let the address of arr[ 0 ] is "2034e" then arr stores "2034e".
Base Address
⧫ An array is stored in a sequence/continuous way i.e, it needs a continuous free memory.
⧫ If the continuous memory is not available then the array will return NULL.

Declaration of an Array:

An array can be declared by two ways-

  1. Local Declaration:
    If we declare the array locally then-
    a[5]; //then the default value it stores is garbage value.
    Local Declaration

    Here we are giving values using scanf( ).
    a[5] ={1,2,3} //then the default value it stores is ' 0 '.
    Local Declaration 2

    Here we are directly taking the values.
  2. Global Declaration:
    If we declare the array globally then-
    a[5]; //then the default value is ' 0 '.
    Global Declaration
How to enter the elements in an array ?

🔺 1-D Array:
⭆To enter the element in an 1-D array we use for loop.

@ Write a program to enter the five elements in an array and print it ?
Example 1
Example 1

Here we are using for loop to enter the elements in an array
OUTPUT
OUTPUT

⭆To enter the elements in an array by directly giving the values

       int a[5] = {10, 20, 30, 40, 50};

    We can also write it like this:-
       int a[ ] = {10, 20, 30, 40, 50};

Things To Remember

  • All elements in an array should be of same datatype.
  • The size of an array should be specified on the time of declaration otherwise the compiler will show error.
  • The indexes in an array starts with '0' and ends in "(no. of elements)-1".

To read our post on Array (Part-2) please click on Arr2.

To get the codes of different programs related to Arrays, please click on Array_Programs.

To read our post on Function, please click on FUN( ).


For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog. Thank you for your support and time...

Post a Comment

0 Comments