Write functions that return the following values Assume that
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
Solution.cpp
#include <iostream>//header file for input output function
using namespace std;//it tells the compiler to link std namespace
int largestelement(int a[],int n);//function declaration
float avgofelements(int a[],int n);//function declaration
int noofpositives(int a[],int n);//function declaration
int main()
{//main function
int n;
cout<<\"enter the size of the array \"<<endl;
cin>>n;//keyboard inputting
int a[n];
cout<<\"enter the no of elements in the array :\"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];//keyboard inputting
}
largestelement(a,n);//function calling
avgofelements(a,n);//function calling
noofpositives(a,n);//function calling
return 0;
}
int largestelement(int a[],int n)
{//function definition
int temp=0;
for(int i=1;i<n;i++)
{//largest elemnts
if(a[i]>temp)
temp=a[i];
}
cout << \"Largest element = \" << temp<<endl;
}
float avgofelements(int arr[],int n)
{//function definition
float avg=0;
int sum=0;
for(int i=0;i<n;i++)
{//logic for avg of elements
sum=sum+arr[i];
}
avg=sum/n;
cout<<\"avg of elements =\"<<avg<<endl;
}
int noofpositives(int a[],int n)
{ //function defintion
int count=0;
for(int i=0;i<n;i++)
{//logic for positives
if(a[i]>0)
count++;
}
cout<<\"no of positives =\"<<count<<endl;
}
output
enter the size of the array 5
enter the no of elements in the array :
10
20
30
40
50
Largest element = 50
avg of elements =30
no of positives =5
