Friday, September 5, 2014

display all elements of an array, use pointers to find maximum and minimum element in an array.


Problem

Write three user define functions:
  1. void display(int *p, int size);
    The first parameter is the base address of an array and the second parameter is the size of array
    (No of elements in an array). The function displays all the elements in the array. Call this
    function from main function.
  2. int max_value (int *p, size);
    this function returns the maximum element of the array pointed by the pointer p. second
    parameter specify the size of array.
  3. int min_value (int *p, size);
    this function returns the minimum element of the array pointed by the pointer p. second
    parameter specify the size of array.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int *,int);
int max_value (int *,int);
int min_value (int *,int);
main()
{
 int max,min,size,i,x,sum=0,avg;
 int num[5];
 size = 5;
 display(num,size);
 max = max_value (num,size);
 printf ("Maximum element : %d\n",max);
 min = min_value (num,size);
 printf ("Minimum element : %d\n",min);
 system("pause");
}
void display(int *p,int size)
{
 int i,num[size],sum=0,avg;
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  sum = sum + num [i];
 }
 avg =(sum/5);
 printf ("sum = %d\n",sum);
 printf ("average = %d\n",avg);
}
int max_value (int *p,int size)
{
 int num[size];
 int i,max=num[0];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=0;i<5;i++)
 {
  if (max < num[i])
  max=num[i];
 }
 return max;
}
int min_value (int *p,int size)
{
 int num[size];
 int i,min=num[4];
 for (i=0;i<5;i++)
 {
  printf("Enter numbers: ");
  scanf("%d",&num[i]);
 }
 for (i=5;i>=0;i--)
 {
  if (min > num[i])
  min=num[i];
 }
 return min;
}

Sample Output

find maximum and minimum element c language

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact