I need help implementing this part of the code. What needs to be done can be fou
ID: 3565900 • Letter: I
Question
I need help implementing this part of the code. What needs to be done can be found in the .h file seen below. ################# #include "part1.h" int vector_max (vector<int> &theList) { return -1; } int vector_min (vector<int> &theList) { return -1; } int vector_count_values (vector<int> &theList, int value) { return -1; } int list_max (list<int> &theList) { return -1; } int list_min (list<int> &theList) { return -1; } int list_count_values (list<int> &theList, int value) { return -1; } #################################### part1.h #################################### #ifndef CSC116_A3_PART1_H_ #define CSC116_A3_PART1_H_ #include <list> #include <vector> using namespace std; /* * return the maximum value in theList * * Pre-conditions: * * theList.size() >= 1 * * Returns: * the maximum value in the list */ int vector_max (vector<int> &theList); /* * return the minimum value in theList * * Pre-conditions: * * theList.size() >= 1 * * Returns: * the minimum value in the list */ int vector_min (vector<int> &theList); /* * return the number of times value occurs in theList * * Pre-conditions: * * theList.size() >= 1 */ int vector_count_values (vector<int> &theList, int value); /* * return the maximum value in theList * * Pre-conditions: * * theList.size() >= 1 * * Returns: * the maximum value in the list */ int list_max (list<int> &theList); /* * return the minimum value in theList * * Pre-conditions: * * theList.size() >= 1 * * Returns: * the minimum value in the list */ int list_min (list<int> &theList); /* * return the number of times value occurs in theList * * Pre-conditions: * * theList.size() >= 1 */ int list_count_values (list<int> &theList, int value); #endif
Explanation / Answer
Here are the function implementations:
#include <list>
#include <vector>
using namespace std;
int vector_max (vector<int> &theList)
{
int i, len = theList.size(), max = theList[0];
for(i=1; i < len; i++){
if(theList[i] > max){
max = theList[i];
}
}
return max;
}
int vector_min (vector<int> &theList)
{
int i, len = theList.size(), min = theList[0];
for(i=1; i < len; i++){
if(theList[i] < min){
min = theList[i];
}
}
return min;
}
int vector_count_values (vector<int> &theList, int value)
{
int i, len = theList.size(), count = 0;
for(i=0; i < len; i++){
if(theList[i] == value){
count++;
}
}
return count;
}
int list_max (list<int> &theList)
{
list<int>::iterator it = theList.begin();
int max = *it;
while(it != theList.end()){
if(*it > max){
max = *it;
}
it++;
}
return max;
}
int list_min (list<int> &theList)
{
list<int>::iterator it = theList.begin();
int min = *it;
while(it != theList.end()){
if(*it < min){
min = *it;
}
it++;
}
return min;
}
int list_count_values (list<int> &theList, int value)
{
list<int>::iterator it = theList.begin();
int count = 0;
while(it != theList.end()){
if(*it == value){
count++;
}
it++;
}
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.