Find Largest Number Using Dynamic Memory Allocation.(Problem 1.1)

Question 1.1 # : Write a program to find Largest Number Using Dynamic Memory Allocation from a list of elements. You have to input total size of elements and depending upon the number of elements, the required size has to be allocated using DMA. Example: 

Input size of array: 5 

Array element: 1 2 3 4 5 

Expected Output: 5


Solution: 


#include<stdio.h>
int main()
{

    int n,*p;
    printf("Enter the number of elemant: ");
    scanf("%d",&n);

    p = (int*)malloc(n*sizeof(int));

    if(p==NULL){
        printf("Program Error");
    }
    else{

        for(int i=0;i<n;i++){
            printf("Enter the elemant: ");
            scanf("%d",p+i);
        }
        int max=0;
        for(int i=0;i<n;i++){
            if(max<*(p+i)){
                max = *(p+i);
            }

        }
        printf("Largest Number is: %d",max);
    }
}






After Run:

Enter the number of elemant: 4
Enter the elemant: 3
Enter the elemant: 2
Enter the elemant: 8
Enter the elemant: 5
Largest Number is: 8
Process returned 0 (0x0)   execution time : 15.531 s
Press any key to continue.







Previous Post Next Post