C++ programming problem find.min.cpp: Q1) Given find min.cpp, write the correspo
ID: 3719193 • Letter: C
Question
C++ programming problem
find.min.cpp:
Q1) Given find min.cpp, write the corresponding lib files. find.min.cpp prints the minimum integer in a vector by calling two separate functions. The functions do achieve the same result by two different means-one by pointer, one by reference. The functions' prototypes are shown below and should be placed in a library file named libmin.h. The respective definitions should be written in libmin.cpp void find.min.ret void find,nin-ptr (vector«int> vector array, int &min;); array, int *min); Note: Compile your code with the command gt+ -std-ct+11 find.min.cpp libmin.cpp.Explanation / Answer
#include <iostream>
#include <vector>
using namespace std;
void find_min_ref(vector<int> v, int &min) {
min = v[0];
for(int i=0;i<v.size();i++){
if(min>v[i]){
min = v[i];
}
}
}
void find_min_ptr(vector<int> v, int *min) {
*min = v[0];
for(int i=0;i<v.size();i++){
if(*min>v[i]){
*min = v[i];
}
}
}
int main()
{
int r, min, len;
vector<int> array;
cout<<"Enter length of array: "<<endl;
cin >> len;
cout<<"Enter "<<len<<" integers (i.e 1 2 -5 2): "<<endl;
for(int i=0;i<len;i++) {
cin>> r;
array.push_back(r);
}
find_min_ref(array, min);
cout<<"Min found by reference: "<<min<<endl;
find_min_ptr(array, &min);
cout<<"Min found by pointer: "<<min<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.