Pages

Thursday, March 27, 2014

C Program to demonstrate example of Dynamic Memory Allocation

/*array as function argument and dynamic memory allocation*/
#include<stdio.h>
#include<stdlib.h>
int max(int*a,int n);
int main()
{
    int *a,n,i;
    printf("How many numbers? ");
    scanf("%d",&n);
    a=(int*)malloc(n*sizeof(int));
    printf("Enter %d numbers: ",n);
    for(i=0;i<n;i++)
        scanf("%d",a+i);
    printf("\n lagrest number=%d", max(a,n));
    system ("pause");
    return 0;
}
int max(int*a,int n)
{
    int i,large;
    large=*a;
    for(i=1;i<n;i++)
    {
        if(large<*(a+i))
            large=*(a+i);
    }
    return large;
}

No comments:

Post a Comment