chore(CPlusPlus): add max and min average (#631)

Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>
pull/675/head
manekasingh05 2022-01-18 00:07:19 +05:30 committed by GitHub
parent 676d1257e5
commit 564b5db025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 0 deletions

View File

@ -0,0 +1,84 @@
/* Finding maximum,minimum and average of an array
SAMPLE INPUT: 4
5
6
7
8
SAMPLE OUTPUT: 8
5
6
*/
//Time complexity= O(n)
//Space complexity= O(n)
//DESCRIPTION
//Problem related to find the maximum, minimum and average from the given array.
#include<iostream>
using namespace std;
//function to find max element in an array
int max(int *arr, int k, int size)
{
int i;
for(i=1; i<size; i++)
{
if(arr[i]>k)
{
k=arr[i];
}
}
return k;
}
//function to find min element in an array
int min(int *arr, int k, int size)
{
int i;
for(i=1; i<size; i++)
{
if(arr[i]<k)
{
k=arr[i];
}
}
return k;
}
//function to find average of an array
int avg(int *arr, int k, int size)
{
int sum=0,avg;
for(int i=0; i<size; i++)
{
sum=sum+arr[i]; //finding sum of every element in an array
}
avg=sum/size;
return avg;
}
int main()
{
int size;
int k=0;
cout<<"Enter the size of an array\n";
cin>>size;
int *arr=new int[size]; //dynamic memory allocation
cout<<"Enter elements in an array\n";
for (int i = 0; i < size; i++)
{
cin>>arr[i];
}
k=arr[0];
cout<<"Maximum element is:\n"<<max(arr,k,size)<<"\n";
cout<<"Minimum element is:\n"<<min(arr,k,size)<<"\n";
cout<<"Average will be:\n"<<avg(arr,k,size)<<"\n";
return 0;
}

View File

@ -29,6 +29,7 @@
- [Merge two sorted array without using extraspace](Arrays/merge-two-sorted-array.cpp)
- [All unique triplet that sum up to given value](Arrays/three-sum.cpp)
- [Next permutation](Arrays/next-permutation.cpp)
- [Maximum Minimum Average of numbers](Arrays/max-min-avg.cpp)
## Dynamic-Programming