Search This Blog

Thursday, October 13, 2011

Write a Program to find max element in an Array.

   1:  #include<conio.h>
   2:  #include<stdio.h>
   3:  float max_array(int n,float x[])
   4:  {
   5:  int i;
   6:  float max=-1.0E38;
   7:  for(i=0;i<n;i++)
   8:  if(x[i]>max)max=x[i];
   9:  return(max);
  10:  }
  11:  void main()
  12:  {
  13:  int n,i;
  14:  char ch;
  15:  float x[50];
  16:  printf("\nEnter the array:\n");
  17:  for(i=0;i<50;i++)
  18:  {
  19:  scanf("%f%c",&x[i],&ch);
  20:  if(ch=='\n')
  21:  break;
  22:  }
  23:  n=i+1;
  24:  printf("The array is:\n");
  25:  for(i=0;i<n;i++)
  26:  printf("%7.2f",x[i]);
  27:  printf("\nmax num is: %7.2f\n",max_array(n,x));
  28:  getch();
  29:  }

No comments:

Post a Comment