Friday, January 06, 2012

ARRAY AND FUNCTION

Introduction

Like other variables, values of an array can also be passed to a function as parameter.

Call by Value

We can pass a individual array element or total numbers of elements to a function.
For example :

#include "stdio.h"
void func(int);
main ()
{
    int arr[5] = { 1, 2, 3, 4, 5 } ;

    printf("Passing the Third element to the Function !  \n" );

    func( arr[2] );
}

void func( int a )
{
    printf("Inside Function ! \nThird element of the Array is %d  \n", a );
}

Display values of an array from another function :

#include "stdio.h"
void show(int);
main ()
{
    int arr1[5], i ;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number for the Array : ", i+1);
   
            scanf("%d", &arr1[i] );
        }

    printf("\nPassing all the elements to the Function ! \n\n");

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

            show( arr1[i] ); // When i 's value is 0 first element is passed and so on.

    printf("\n");
}

void show(int a)   //  Each numbers are received at integer a.
{
    printf("Values of the Array are : %d \n",a );
}

Addition of 5 Number :

#include "stdio.h"
int sum(int);
main ()
{
    int arr1[5], i,c ;

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

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

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

            c = sum( arr1[i] );

    printf("\nAddition of 5 Numbers : %d \n\n", c);
}

int sum(int a)
{
    static int sum = 0 ;   //  A static variable is initialized once.

    sum = sum + a;

    return sum;
}

Call by Reference

This is a smarter way to manipulate a array element from another function. Just know the Dimension and send the Base Address of the Array to the desired function.
For example :

#include "stdio.h"
void func( int * );
main ()
{
    int arr[5] = { 1, 2, 3, 4, 5 } ;

    printf("Passing the Address of Third element to the Function !  \n" );

    func( &arr[2] );
}

void func( int *a )
{
    printf("Inside Function ! \nThird element of the Array is %d  \n", *a );
}

Display values of an array from another function :

#include "stdio.h"
void show(int *, int);
main ()
{
    int arr1[5], i ;

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

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

    printf("\nPassing the Base Address and Dimension to the Function ! \n\n");

            show( &arr1[0], 5 );

    printf("\n\n");
}

void show(int *a, int b)
{
    for ( int j=0 ; j < b ; j++ )  //  b is the dimension of the array.
    {
        printf("Values of the Array are : %d \n",*a);

        a++;  //  Switching over to the next memory location.
    }
}

Addition of 5 Number :

#include "stdio.h"
int sum(int *);
main ()
{
    int arr1[5],i,c;

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

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

    c = sum(&arr1[0]);  //  First or Base Address is passed.

    printf("\nAddition of 5 Numbers : %d \n\n", c);
}

int sum(int *a)
{
    int sum=0, j ;

    for( j = 0 ; j < 5 ; j++ )
    {
        sum = sum + *a;
        a++;
    }
    return sum;
}

Or it can be done in the following way too :

#include "stdio.h"
void sum(int *, int *);
main ()
{
    int arr1[5], i, sm = 0;

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("%d Enter Number : ", i+1);
   
            scanf("%d", &arr1[i] );
        }

    sum(&arr1[0], &sm);

    printf("\nAddition of 5 Numbers : %d \n\n", sm);
}

void sum(int *arr, int *s)
{
    for( int j = 0 ; j < 5 ; j++ )
    {
        *s = *s + *arr;
        arr++;
    }
}

Copy of Array :

#include "stdio.h"
void copy(int *);
main ()
{
    int arr1[5], i ;

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

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

    printf("\nCopy the Numbers ! \n\n");

    copy(&arr1[0]);
   
    printf("\n");
}

void copy(int *ranty)
{
    int arr2[5], j ;

    for( j = 0 ; j < 5 ; j++ )
    {
        arr2[j] = *ranty ;

        ranty++;
    }

    for( j = 0 ; j < 5 ; j++ )

        printf("Value of Second Array : %d \n", arr2[j]);
}

Thursday, January 05, 2012

ARRAY AND POINTER

Introduction

Array itself uses pointer internally. Like other variables, address of an array can also be stored in a pointer variable or a Array of Pointers can be made. Address of the first element of the array is called the Base Address.

Method 1

    int a[5]={1,2,3,4,5};

This array contains five elements, to print the address of each one we can write :

    for( i = 0 ; i < 5 ; i++ )
        printf("Address of %d is %u  \n\n",a[i], &a[i] );

Method 2

Here each pointer variable is initialized with the address of array elements.

    int *p0 = &a[0];
    int *p1 = &a[1];


    printf("Address of %d is %d \n\n", *p0, p0 );

Method 3

Array of Pointers

We can make an array of pointer where addresses of other array elements or variables can be stored like :

int *p[5]={a,a+1,a+2,a+3,a+4};

Here a means the base address or the address of the first value, a+1 means the next address that is the address of the second value, a+2 means the third address and so on. To print them we can write :

    for( i = 0 ; i < 5 ; i++ )
        printf("Address of %d is %d  \n\n",*p[i], p[i]  );

Method 4

Simply know the dimension of the array and take the base address into a pointer variable
like :

int *ranty = a;

Now to print each values with address we can write :

    for( i = 0 ; i < 5 ; i++ )
    {
        printf("Address of %d is %d \n\n", *ranty, ranty );
        ranty++;
    }

When value of i is 0, value at ranty that is value and address of a[0] gets printed.
ranty++ means switching over to the next memory location that is value and address of a[1] gets printed. It would be carried on till the loop gets ended, whose condition is 1 less than the dimension of the array.

A Program Demonstrating the above points :

#include"stdio.h"
void printline(void);
void main()
{
    int a[5]={1,2,3,4,5} , i;

    printf("\nValues of the Array are : ");

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

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

    printline();

    printf("\nPRINT VALUES WITH ADDRESSES ! \n\n");

    printline();

    printf("\tMETHOD 1 \n\n");
   
        for( i = 0 ; i < 5 ; i++ )

            printf("Address of %d is %u \n\n",a[i], &a[i] );

    printline();
   
    printf("\tMETHOD 2 \n\n");

    int *p0 = &a[0]; // A pointer variable storing the address of first element.
    int *p1 = &a[1];
    int *p2 = &a[2];
    int *p3 = &a[3];
    int *p4 = &a[4];

    printf("Address of %d is %d \n\n", *p0, p0 );
    printf("Address of %d is %d \n\n", *p1, p1 );
    printf("Address of %d is %d \n\n", *p2, p2 );
    printf("Address of %d is %d \n\n", *p3, p3 );
    printf("Address of %d is %d \n\n", *p4, p4 );

    printline();

    printf("\tMETHOD 3 \n\n");
   
    int *p[5]={a,a+1,a+2,a+3,a+4};  // Array of Pointer

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

            printf("Address of %d is %d \n\n",*p[i],p[i] );

    printline();

    printf("\tMETHOD 4 \n\n");

        int *ranty = a;  // Taking the Base Address or the First Address

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("Address of %d is %d \n\n",*ranty,ranty );

            ranty++;
        }

    printline();
}

void printline()
{  
    int i;

    for( i=0 ; i<60 ; i++ )
        printf("_");

    printf("\n\n");
}

Another Example for Array of Pointers :

#include"stdio.h"
void main()
{
    int a = 5, b = 10, c = 15, i;

    int *pt[3] = {&a, &b, &c};

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

         printf("Values of the Array are %d  \n", pt[i] );

    printf("a + b = %d  \n", *pt[0] + *pt[1] );
    printf("c - b = %d  \n", *pt[2] - *pt[1] );
}

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");
}

Sunday, January 01, 2012

RECURSION

Introduction

A function calling itself is called Recursion. In some programs to fulfill a certain condition we use Recursion that is a function will call itself unit we get our desired result. A very simple example of it will be :

Addition of two numbers but the result should be an even number (Just add one with the second number if the result is an odd number) :

#include"stdio.h"
void cal (int, int);
main()
{
    int a, b;

    printf("Enter value for A : ");
    scanf("%d",&a);

    printf("Enter value for B : ");
    scanf("%d",&b);

    cal(a,b);
}

void cal (int a, int b)
{
    int c;
        if((a+b)%2==0)
        {
            c=a+b;
            printf("A + B = %d  \n",c);
        }
        else
        {
            printf("(Added 1 with the Second Number)  \n");

            cal(a,b+1);  // Called Itself
        }
}

It will Count Its Own Call :

#include"stdio.h"
#include"stdlib.h"  //  exit() is defined under stdlib.h.
void fun (int);
void main()
{
    int counter=0;

    fun(counter);
}

void fun (int counter)
{
    if (counter==100)
    {
        exit(0);  // This function will take an exit from the total program.
    }
        counter++;

        printf("%d Called itself 100 Times \n",counter);

        fun(counter);
}

Binary Conversion :

#include"stdio.h"
void bin(int);
void main()
{
    int n;
    printf("Enter a Number : ");
    scanf("%d",&n);

    printf("\nNumber = %d \t Binary = ",n);

    bin(n);

    printf("\n\n");
}

void bin(int n)
{   
    if(n>1)
        bin(n/2);

    printf("%d",n%2);
}

Binary Conversion ( 0 - 100 ) :

#include"stdio.h"
void bin(int);
void main()
{
    printf("Decimal \t Binary\n");
    printf("__________________________\n");

    for(int i=0;i<=100;i++)
    {
        printf("\n%d -------------- ",i);
        bin(i);
        printf("\n");
    }
    printf("\n\n");
}

void bin(int i)
{   
    if(i>1)
        bin(i/2);

    printf("%d",i%2);
}

Will Work Like an Odd Loop :

#include"stdio.h"
void neg(int);
main()
{
    int i=0;
    neg(i);
}

void neg(int i)
{
    static int f=0;

    if(i>=0)
    {
        printf("To Exit from this Function a Negative Number has to be Entered : ");
        scanf("%d",&i);

        if(i>=1)
            f=f+i;

        neg(i);
    }
    else
    {   
        printf("Sum of all Positive Numbers You Entered is %d \n",f);
    }
}

Followers