1
0

maximum via for itr

This commit is contained in:
Captain ALM 2022-10-28 10:30:27 +01:00
parent f67603bc36
commit 34044faf99
3 changed files with 18 additions and 11 deletions

View File

@ -1,5 +1,6 @@
#include <iostream>
#include <list>
#include <vector>
#include <iomanip>
using string = std::string;
@ -20,22 +21,20 @@ int prompti(string,bool*);
double promptd(string,bool*);
double avgitr(std::list<double>*);
double avgfor(std::list<double>*);
void scale(std::list<double>&,double);
int maximum(std::vector<int>&);
int maximum(std::list<int>&);
int main() {
printlm("Scale:");
printlm("Largest:");
bool s {};
std::list<double> lst {};
std::list<int> lst {};
do {
auto v = promptd("Enter a double value: ", &s);
auto v = prompti("Enter a integer value: ", &s);
if (s) lst.push_back(v);
} while (s);
auto scalef = promptd("Enter double scalar: ", &s);
if (!s) return -1;
printlm("Scaled:");
scale(lst, scalef);
for (const auto c : lst) printlm(c);
printm("Maximum: ");
printlm(maximum(lst));
return 0;
}
@ -77,6 +76,14 @@ double avgfor(std::list<double> *values) {
return sum / values->size();
}
void scale(std::list<double> &values, double sf) {
for (auto &c : values) c *= sf;
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;
}

BIN
main.o

Binary file not shown.

BIN
t

Binary file not shown.