Jump to content

[C++] Find minimum and maximum number of array using function


Azain
 Share

Recommended Posts

#include<iostream>
using namespace std;

//function to find minimum
int minimum(int a[], int n)
{
	int result=a[0];
	for(int i=1; i<n; i++)
	{
		if(a[i]<result)
		result=a[i];
	}
	return result;
}

//function to find maximum
int maximum(int a[], int n)
{
	int result=a[0];
	for(int i=1; i<n; i++)
	{
		if(a[i]>result)
		result=a[i];
	}
	return result;
}

//function to find sum of array
int sum(int a[], int n){
	int result=0;
	for(int i=0; i<n; i++)
	{
		result+=a[i];
	}
	return result;
}

int main()
{
	int n=0, Sum=0, avg=0;
	cout<<"Enter the array size: ";
	cin>>n;
	int arr[n];
	cout<<"Enter the values for arrays elements: \n";
	for(int i=0; i<n; i++){
		cout<<"Element no. "<<i+1<<" : ";
		cin>>arr[i];
	}
	cout<<endl;
	
	cout<<"The integer array is : \n";
	for(int i=0; i<n; i++)
	{
		cout<<arr[i]<<"   ";
	}
	
	//output minimum
	cout<<"\n\nMinimum number of this array is "<<minimum(arr,n);
	
	//output maximum
	cout<<"\nMaximum number of this array is "<<maximum(arr,n);
	
	cout<<"\nSum of this array is "<<sum(arr,n);
	
	//to calculate average
	Sum=sum(arr,n);
	avg=Sum/n;
	
	//output average
	cout<<"\nAverage of this array is "<<avg;
}

This program takes array size and elements values from user and find the minimum and maximum value, also calculates sum and average of these values using function.

 

Edited by A Z A I N
Link to comment
Share on other sites

@A Z A I N,

 

you add the values in vector starting by 0, but you start the sum from 0, so you are jumping over the first elementor from vector. That means both sum and average are displayed wrong.

Edited by shanker'
Link to comment
Share on other sites

38 minutes ago, A Z A I N said:

int main()
{
	int n=5, arr[n], Sum=0, avg=0;
	[...]
}

 

arr[n] doesn't exist. If you want to have a specific size of array, you can initialize it at the maximum size, and then you can give to n any value, smaller than the size of array. For example:

int arr[100000];
int main()
{
	int n, Sum=0, avg=0;
	cin>>n;
	cout<<"Enter the values for arrays elements: \n";
	for(int i=0; i<n; i++){
		cout<<"Element no. "<<i+1<<" : ";
		cin>>arr[i];
	}
	cout<<endl;
	[...]
}

And the most important thing: add 'return value' at the final of function, except for 'void'. You forgot to put 'return' in "main()".

Link to comment
Share on other sites

1 hour ago, shanker' said:

@A Z A I N,

 

you add the values in vector starting by 0, but you start the sum from 0, so you are jumping over the first elementor from vector. That means both sum and average are displayed wrong.

Thanks for identifying the logic error, i have edited it. It happened because i copy the above function of minimum and forgot to edit value 1 in for loop

 

51 minutes ago, Clanin3 said:

arr[n] doesn't exist. If you want to have a specific size of array, you can initialize it at the maximum size, and then you can give to n any value, smaller than the size of array. For example:


int arr[100000];
int main()
{
	int n, Sum=0, avg=0;
	cin>>n;
	cout<<"Enter the values for arrays elements: \n";
	for(int i=0; i<n; i++){
		cout<<"Element no. "<<i+1<<" : ";
		cin>>arr[i];
	}
	cout<<endl;
	[...]
}

And the most important thing: add 'return value' at the final of function, except for 'void'. You forgot to put 'return' in "main()".

I edited it, now user will enter the array size.

Link to comment
Share on other sites

Hi, here is a much simpler and cleaner code. Have a look:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    int N;
    std::cout << "Enter the array size: "; std::cin >> N;
    
    std::vector<int> input(N); // <- HERE IS INITIALIZED A VECTOR OF SIZE N (DYNAMICALLY ALLOCATED)
    
    // YOU DON'T NEED A WORKAROUND FOR THIS LIKE THE NORMAL ARRAY WHERE YOU MUST GIVE A BIGGER SIZE
    // AND THEN HAVE N SMALLER
    
    std::cout << "Enter the elements of the array: ";
    
    for (auto& element : input) { // <- C++'s foreach
        std::cin >> element; // <- READ THE ARRAY ELEMENTS
    }
    
    std::cout << "The maximum element is: " << *std::max_element(input.begin(), input.end()) << "\n";
    
    std::cout << "The minimum element is: " << *std::min_element(input.begin(), input.end()) << "\n";
    
    int sum = std::accumulate(input.begin(), input.end(), 0);
    std::cout << "The sum of the array is: " << sum << "\n";
    
    std::cout << "The average of the array is: " << 1.0 * sum / N << "\n";
    
    return 0;
}

You don't need to reinvent the wheel. Use the functions that already exist inside the language.

If you have any questions about the code, feel free to ask. Gaining knowledge means gaining power ;)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.