(For the following assignment, please answer in the most SIMPLEST FORM possible.
ID: 3727117 • Letter: #
Question
(For the following assignment, please answer in the most SIMPLEST FORM possible. Nothing too intricate as I am only a beginner.
Label each one my its exercise, so I know which one is which.
THANK YOU)
Exercise 5
Write a function that takes an array a of integers and its size n and returns the minimum value of the array. Propose a prototype for the function. Write its code and a tester to demonstrate its use.
Continuation of Exercise 5
Redo exercise 5 using vectors. The problem statement becomes: write a function that takes a vector of integers and returns the minimum value in the vector.
C++ LANGUAGE
Explanation / Answer
Exercise 5.
#include<iostream>
using namespace std;
int fxn( int arr[], int n )
{
int i;
int min =INT_MAX;
for( i = 0 ; i < n ; i++ )
// if the current element is smaller than min
if( arr[i] < min )
min = arr[i];
return min;
}
int main()
{
int arr[] = { 5, 3, 6, 1, -2, 5 };
cout<<"Min element : "<<fxn( arr, 6 );
return 0;
}
With Vectors
#include<iostream>
#include<vector>
using namespace std;
int fxn( vector<int> arr )
{
int i;
int min =INT_MAX;
for( i = 0 ; i < arr.size() ; i++ )
// if the current element is smaller than min
if( arr[i] < min )
min = arr[i];
return min;
}
int main()
{
vector<int> arr;
arr.push_back(6);
arr.push_back(4);
arr.push_back(7);
arr.push_back(1);
arr.push_back(-2);
arr.push_back(4);
cout<<"Min element : "<<fxn( arr );
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.