Tuesday, 22 April 2014

C Program to Find Largest Number Using Dynamic Memory Allocation

 Dynamic Memory Allocation

In this program, calloc() function is used to allocate the memory dynamically. Depending upon the number of elements, the required size is allocated which prevents the wastage of memory. If no memory is allocated, error is displayed and the program is terminated.

Source Code to Find Largest Element Using Dynamic Memory Allocation

#include <stdio.h>
#include <stdlib.h>
int main(){
    int i,n;
    float *data;
    printf("Enter total number of elements(1 to 100): ");
    data=(float*)calloc(n,sizeof(float));  /* Allocates the memory for 'n' elements */
        printf("Error!!! memory not allocated.");
    for(i=0;i<n;++i)  /* Stores number entered by user. */
       printf("Enter Number %d: ",i+1);
    for(i=1;i<n;++i)  /* Loop to store largest number at address data */
       if(*data<*(data+i)) /* Change < to > if you want to find smallest number */
    printf("Largest element = %.2f",*data);
    return 0;


Largest element of an array output
Post a Comment


Comment Box is loading comments...