Find Smallest element in array Using Dynamic Memory Allocation

-- Leave a Comment
Hello Here is a C Program to Find the Smallest Element among Array element. Here we use Dynamic Memory Allocation to Set the size of Array. And all other are as usual.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<stdlib.h>
void main()
{
    int *p,n,i,min;
    printf("Enter the total number of elements:- ");
    scanf("%d",&n);
    p = (int *) malloc(sizeof(int) * n);
    printf("Enter the Elements of the Array:- \n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(p+i));
    }
    min=*p;
    for(i=0;i<n;i++)
    {
        if(min>*(p+i))
        {
            min=*(p+i);
        }
    }
    printf("The Minimum ammong the Elements is %d",min);
}

p = (int *) malloc(sizeof(int) * n);
Here memory are allocated and the first memory is assigned to the first element.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University, NCIT. Open Source Enthusiastic.

0 comments:

Post a Comment