Write a C program Write functions that return the following

Write a C program

Write functions that return the following values. (Assume that a and n are parameters, where a is an array of int values and n is the length of the array.) (a) The largest element in a. (b) The average of all elements in a. (c) The number of positive elements in a.

Solution

#include <stdio.h>
#include <stdlib.h>
int largest(int* ,int );
float avg(int*,int);
int positive(int*,int);
int main()
{
int n,i=0,lar,count;
float average;
printf(\"Enter the number of elements in the array\ \");
scanf(\"%d\",&n);
int a[n];
for(i=0;i<n;i++)
    {
      printf(\"\ Enter the element %d of the array\ \",i+1);
      scanf(\"%d\",&a[i]);
    }
lar=largest(a,n);
average=avg(a,n);
count=positive(a,n);
printf(\"\ Largest Value in array = %d\ \",lar);
printf(\"\ Average of the array = %f\ \",average);
printf(\"\ Total Positive elements = %d\ \", count);
return 0;
}
int largest(int *a,int n)
{
int max=-32768 ;
for(int i=0;i<n;i++)
    {
      if(max < a[i])
   {
      max=a[i];
   }
    }
return max;
}
float avg(int *a,int n)
{
float average=0;
int sum=0;
for(int i=0;i<n;i++)
    {
      sum+=a[i];
    }
average=(float)sum/n;
return average;
}
int positive(int *a,int n)
{
int count=0;
for(int i=0;i<n;i++)
    {
      if(a[i]>0)
   {
      count+=1;
   }
    }
return count;
}

Write a C program Write functions that return the following values. (Assume that a and n are parameters, where a is an array of int values and n is the length o
Write a C program Write functions that return the following values. (Assume that a and n are parameters, where a is an array of int values and n is the length o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site