Let Us C (Chapter 8 : Arrays) : Program-7
(g) Find the smallest number in an array using pointers.
// Let Us C (Chapter 8 : Arrays) : Program-7
/* Find the smallest number in an array using pointers */
#include<stdio.h>
#define ARRAY_SIZE 10
long int arr[ARRAY_SIZE],smallest,*ptr;
unsigned int i,N;
void main()
{
clrscr();
printf("Program to find smallest number in an array using pointers");
printf("Enter no. of elements: ");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("Element %d: ",i);
scanf("%ld",&arr[i]);
}
ptr = arr; // passing array address to pointer
smallest = arr[0];
for(i=0;i<N;i++)
{
if(smallest > *(ptr+i)) // comparing using pointer
{
smallest = *(ptr+i);
}
}
printf("Smallest number in array is %ld",smallest);
}
OUTPUT
Comments
Post a Comment