1
0
LearningCPP/main.cpp
2022-10-28 10:30:27 +01:00

90 lines
1.6 KiB
C++

#include <iostream>
#include <list>
#include <vector>
#include <iomanip>
using string = std::string;
//Place in header!
template <typename T>
void printm(T m){
std::cout << m;
}
template <typename T>
void printlm(T m){
printm(m);
printm("\n");
}
int prompti(string,bool*);
double promptd(string,bool*);
double avgitr(std::list<double>*);
double avgfor(std::list<double>*);
int maximum(std::vector<int>&);
int maximum(std::list<int>&);
int main() {
printlm("Largest:");
bool s {};
std::list<int> lst {};
do {
auto v = prompti("Enter a integer value: ", &s);
if (s) lst.push_back(v);
} while (s);
printm("Maximum: ");
printlm(maximum(lst));
return 0;
}
int prompti(string message, bool *status) {
int tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0;
}
*status = true;
return tr;
}
double promptd(string message, bool *status) {
double tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0.0;
}
*status = true;
return tr;
}
double avgitr(std::list<double> *values) {
double sum {};
for (auto itr = values->cbegin(); itr != values->cend(); ++itr) sum += *itr;
return sum / values->size();
}
double avgfor(std::list<double> *values) {
double sum {};
for (const auto c : *values) sum += c;
return sum / values->size();
}
int maximum(std::vector<int> &values) {
int m {};
for (const auto c : values) if (c > m) m = c;
return m;
}
int maximum(std::list<int> &values) {
int m {};
for (const auto c : values) if (c > m) m = c;
return m;
}