Tuesday, January 03, 2012

ARRAY

Introduction

Till now we have worked with few variables like addition of two numbers, factorial of a single number etc. If we are asked to add hundred numbers then we would write the program like
int a,b,c,d till 100 variables ;
or put scanf() inside a loop but in this case we will not be able to store them for further operations in a program.
To overcome this problem we will use Array. Array is used to store a group of similar values. An array can be declared as int where we cannot put floating point values or characters, same for the other ones. A character array is called a String which will be coming later on.

Declaration

An Array can be declared as follows :

float fnums[5] = {1.1,2.2,3.3,4.4,5.5};

int nums[10] = {1,2,3,4,5,6,7,8,9,10};

int nums[ ] = {1,2,3,4,5,6,7,8,9,10};

int nums[10] ;

nums is the variable name which is declared as int.
[ ] says the compiler that this is an array and will reserve space in memory location serially to store the values inside it. 
10 inside the brackets [] is the size or dimension of the array. Dimension 10 means we can store ten numbers only or [5] means can store five numbers. This dimension has to be mentioned during declaration if we initialize them later but at the time of declaration if we initialize them then it is optional like :

int nums[ ] = {1,2,3,4,5,6,7,8,9,10};

An Array declared as float, every thing will be same as above except the memory space allocated for the variables and values we put.

We can also print or manipulate those numbers stored in the array. In an Array number starts from 0 and ends at 1 less than the dimension. So to mention any particular number in the array we have to write as shown below :

int nums[10] = {1,2,3,4,5,6,7,8,9,10};

nums[0]  =  First Number ie 1

nums[4]  =  Fifth Number ie 5

nums[9]  =  Last Number ie 10

nums[0]  +  nums[9]  =  First Number + Last Number ie 1 + 10 = 11

nums[3]  -  nums[2]  =  Fourth Number - Third Number ie 4 - 3 = 1


Check Out the Output :

#include "stdio.h"
main()
{
    int nums[10] = {1,2,3,4,5,6,7,8,9,10};

    printf("After Initialization !  \n\n");

    printf("First Number of the Array is %d \n\n", nums[0] );

    printf("Fifth Number of the Array is %d \n\n", nums[4] );

    printf("Last Number of the Array is %d \n\n", nums[9] );

    printf("First Number + Last Number = %d \n\n", nums[0] + nums[9] );

    printf("Fourth Number - Third Number = %d \n\n", nums[3] - nums[2] );
}

Print Ten Numbers with the help of for loop :

#include "stdio.h"
main()
{
    int nums[10] = {1,2,3,4,5,6,7,8,9,10};

    printf("After Initialization !  \n\n");

    printf("Numbers of the Array are " );

    for( int i = 0 ; i < 10 ; i++ )

        printf("%d, ", nums[i] );
   
    printf("\n\n");
}

Here the loop starts when i 's value is 0 and ends when i 's value is 9. When the value of i is 0 the first number gets printed and so on.

Enter Ten Numbers with the help of for loop :

#include "stdio.h"
main()
{
    int nums[10], i ;  // Here mentioning the dimension is mandatory.

    for( i = 0 ; i < 10 ; i++ )
    {
        printf("%d Enter Number : ", i+1);

        scanf("%d", &nums[i] );
    }

    printf("\nAfter Initialization !  \n\n");

    printf("Numbers of the Array are " );

    for( i = 0 ; i < 10 ; i++ )

        printf("%d, ", nums[i] );

    printf("\n\nIn case of loop we can also manipulate a particular number like : \n\n");
    printf("First Number + Last Number = %d \n\n", nums[0]+nums[9] );
}

scanf("%d", &nums[i] ); When the value of i is 0 the first number of the array gets initialized and so on.

Addition of Ten Numbers :

#include "stdio.h"
main()
{
    int nums[10], i, sum = 0 ;

    for( i = 0 ; i < 10 ; i++ )
    {
        printf("%d Enter Number : ", i+1);

        scanf("%d", &nums[i] );
    }

    printf("\nAfter Initialization !  \n\n");

    printf("Numbers of the Array are " );

    for( i = 0 ; i < 10 ; i++ )
    {
        printf("%d, ", nums[i] );

        sum = sum + nums[i];
    }

    printf("\n\nAddition of Ten Numbers : %d \n\n", sum);
}

Find Out the Maximum and Minimum Number :

#include "stdio.h"
main()
{
    int nums[10], i, max = 0, min = 0 ;

    for( i = 0 ; i < 10 ; i++ )
    {
        printf("%d Enter Number : ",i+1);

        scanf("%d", &nums[i] );
    }

    min = nums[0];

    printf("\nAfter Initialization !  \n\n");

    printf("Numbers of the Array are " );

    for( i = 0 ; i < 10 ; i++ )
    {
        printf("%d, ", nums[i] );
       
        if(nums[i]>max)

            max=nums[i];

        if(nums[i]<min)

            min=nums[i];
    }

    printf("\n\nMaximum Number is : %d \n\n", max);
    printf("Minimum Number is : %d \n\n", min);
}

Binary Conversion :

#include"stdio.h"
void main()
{
    int arr[10], i=0, n ;

    printf("Enter a Number : ");
    scanf("%d",&n);
   
        while(n>0)
        {
            arr[i] = n%2;
            i++ ;
            n = n/2 ;
        }

    printf("\nBinary Form : ");

        for( i-- ; i>=0 ; i-- )
            printf("%d",arr[i]);
   
    printf("\n\n");
}

No comments:

Post a Comment

Followers