Q. WAP to find smallest and largest number in a given array using pointer. [6]
#Assignment2b
==> Code goes like this:
#include<stdio.h>
int main()
{
int a[10],i,j,temp,num;
printf("Enter the size of array:");
scanf("%d",&num);
printf("Enter %d elements of array: ",num);
for ( i = 0; i < num; i++)
{
scanf("%d",a+i);
}
//for highest salary
for ( i = 0; i < num-1; i++)
{
for ( j = i+1; j < num; j++)
{
if(*(a+i) < *(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
printf("The highest element of array is: %d \n ",*(a+0));
//for lowest salary
for ( i = 0; i < num-1; i++)
{
for ( j = i+1; j < num; j++)
{
if(*(a+i) > *(a+j))
{
temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}
}
}
printf("The lowest element of array is: %d \n ",*(a+0));
return 0;
}
Output:
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you
Home / AssignmentSolutionC /
C Programs /
largest & smallest element /
pointer in c
/ find smallest and largest number in a given array using pointer - C
- Blogger Comment
- Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 comments:
Post a Comment