
Patrick B. answered 02/28/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <vector>
#include <stdlib.h>
//UNCOMMENT THE COMPILER SWITCH to run which of the 3 main programs to run
#define FILTER
//#define NORMALIZE
//#define SORT
vector<int> GetUserValues(vector<int>& userValues, int numValues)
{
for (int iLoop=0; iLoop<numValues; iLoop++)
{
int iNum;
cout << "input integer value :>";
cin >> iNum;
userValues.push_back(iNum);
}
return(userValues);
}
void OutputIntsLessThanOrEqualToThreshold(vector<int> userValues, int upperThreshold)
{
int N = userValues.size();
for (int iLoop=0; iLoop<N; iLoop++)
{
int iNum = userValues[iLoop];
if (iNum<upperThreshold)
{
cout << iNum << " ";
}
}
}
int GetMinimumInt ( vector<int> listInts)
{
int N = listInts.size();
int minInt = 32767;
for (int iLoop=0; iLoop<N; iLoop++)
{
int iNum = listInts[iLoop];
if (iNum<minInt)
{
minInt = iNum;
}
}
return(minInt);
}
vector<int> Normalize( vector<int>& intVector, int minVal)
{
int N = intVector.size();
for (int iLoop=0; iLoop<N; iLoop++)
{
int iNum = intVector[iLoop];
iNum -= minVal;
intVector[iLoop]=iNum;
}
return(intVector);
}
void IntVectorDump ( vector<int> intVector)
{
int N = intVector.size();
for (int iLoop=0; iLoop<N; iLoop++)
{
int iNum = intVector[iLoop];
cout << iNum << " ";
}
}
int CompareInt( const void * rec1, const void * rec2)
{
int * intPtr1 = (int*)rec1;
int * intPtr2 = (int*)rec2;
int intVal1 = * intPtr1;
int intVal2 = * intPtr2;
return( intVal1 - intVal2);
}
void SortVector(vector<int>& myVec)
{
int N = myVec.size();
int * A = (int*)malloc(N * sizeof(int));
for (int iLoop=0; iLoop<N; iLoop++)
{
A[iLoop]=myVec[iLoop];
}
qsort(A,N,sizeof(int),CompareInt);
for (int iLoop=0; iLoop<N; iLoop++)
{
myVec[iLoop]=A[iLoop];
}
free(A);
}
#if defined(FILTER)
/* which ints are LESS than the specified max value */
int main()
{
vector<int> intVector;
int N;
cout << "How many??? :>";
cin >> N;
GetUserValues( intVector,N);
//debug dumps:
//IntVectorDump(intVector);
int maxIntAllowed;
cout << "please input max int threshold :>";
cin >> maxIntAllowed;
OutputIntsLessThanOrEqualToThreshold(intVector,maxIntAllowed);
}
#elif defined(NORMALIZE)
int main()
{
vector<int> intVector;
int N;
cout << "How many??? :>";
cin >> N;
GetUserValues( intVector,N);
int minVal=GetMinimumInt(intVector);
Normalize(intVector,minVal);
IntVectorDump(intVector);
}
#else //sort
int main()
{
vector<int> intVector;
int N;
cout << "How many??? :>";
cin >> N;
GetUserValues( intVector,N);
SortVector(intVector);
IntVectorDump(intVector);
}
#endif