w3resource logo


C Programming Exercises

C Exercises: Find the largest element using Dynamic Memory Allocation

Secondary Nav

C Pointer : Exercise-9 with Solution

Write a program in C to find the largest element using Dynamic Memory Allocation.
Test Data :
Input total number of elements(1 to 100): 5

Number 1: 5
Number 2: 7
Number 3: 2
Number 4: 9
Number 5: 8
Expected Output :

The Largest element is :  9.00 

C Code:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,n;
    float *element;
	printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n"); 
	printf("-------------------------------------------------------------------------\n"); 	
    printf(" Input total number of elements(1 to 100): ");
    scanf("%d",&n);
    element=(float*)calloc(n,sizeof(float));  // Memory is allocated for 'n' elements 
    if(element==NULL)
    {
        printf(" No memory is allocated.");
        exit(0);
    }
    printf("\n");
    for(i=0;i<n;++i)  
    {
       printf(" Number %d: ",i+1);
       scanf("%f",element+i);
    }
    for(i=1;i<n;++i)  
    {
       if(*element<*(element+i)) 
           *element=*(element+i);
    }
    printf(" The Largest element is :  %.2f \n\n",*element);
    return 0;
}

Flowchart :

Flowchart: Find the largest element using Dynamic Memory Allocation

C Practice online:


#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,n;
    float *element;
	printf("\n\n Pointer : Find the largest element using Dynamic Memory Allocation :\n"); 
	printf("-------------------------------------------------------------------------\n"); 	
    printf(" Input total number of elements(1 to 100): ");
    scanf("%d",&n);
    element=(float*)calloc(n,sizeof(float));  // Memory is allocated for 'n' elements 
    if(element==NULL)
    {
        printf(" No memory is allocated.");
        exit(0);
    }
    printf("\n");
    for(i=0;i<n;++i)  
    {
       printf(" Number %d: ",i+1);
       scanf("%f",element+i);
    }
    for(i=1;i<n;++i)  
    {
       if(*element<*(element+i)) 
           *element=*(element+i);
    }
    printf(" The Largest element is :  %.2f \n\n",*element);
    return 0;
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program