Canna

Tuesday, 25 September 2018

LARGE ARRAY

  • Problem Description

    Read in "n" integer numbers into an array and find the average of two largest given numbers without sorting the array. The program should output the given the average
  • Test Case 1

    Input (stdin)
    4
    
    80 23 79 58
    
    
    Expected Output
    First Largest=80
    
    Second Largest=79
    
    Average=79.5
  • Test Case 2

    Input (stdin)
    5
    
    180 23 179 58 43
    
    
    Expected Output
    First Largest=180
    
    Second Largest=179
    
    Average=179.5
  • Program
  • #include <stdio.h>
    #include <limits.h>
    
    int main()
    {
      int a[10], n;
      int l1,l2,i;
      float c;
      scanf("%d",&n);
      for(i=0;i<n;i++)
        scanf("%d",&a[i]);
      l1=l2= INT_MIN;
      for(i=0;i<n;i++)
      {
        if(a[i]>l1)
        {
          l2=l1;
          l1=a[i];
      }
          else if(a[i]>l2 && a[i]<l1)
          l2=a[i];
      }
      
      printf("First Largest=%d",l1);
      printf("\nSecond Largest=%d",l2);
      c=(float)(l1+l2)/2;
      printf("\nAverage=%.1f",c);
      return 0;
    }

No comments:

Post a Comment

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