find smallest and largest number in a given array using pointer - C

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
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment