146 lines
4.1 KiB
C++
146 lines
4.1 KiB
C++
#include <iostream>
|
|
#include <ctime>
|
|
#include <cmath>
|
|
|
|
using namespace std;
|
|
|
|
void menu() {
|
|
cout << "============================" << endl;
|
|
cout << "=========== MENU ===========" << endl;
|
|
cout << "============================" << endl;
|
|
cout << "1 ... GENERATE WATER INTAKES" << endl;
|
|
cout << "2 ... PRINT WATER INTAKES" << endl;
|
|
cout << "3 ... AVERAGE" << endl;
|
|
cout << "4 ... DAYS UNDER AVERAGE" << endl;
|
|
cout << "5 ... THE MOST WATER" << endl;
|
|
cout << "6 ... IN LIMIT" << endl;
|
|
cout << "7 ... ALL THE WATER" << endl;
|
|
cout << "0 ... EXIT" << endl;
|
|
cout << "============================" << endl;
|
|
cout << "Select: ";
|
|
}
|
|
|
|
float map(float value, float start1, float stop1, float start2, float stop2) {
|
|
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
|
|
}
|
|
|
|
void fillArray(float *array, const unsigned int size) {
|
|
float rand_number;
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
rand_number = map(rand(), 0, RAND_MAX, 0.5, 3.5);
|
|
rand_number = round(10 * rand_number) / 10;
|
|
array[i] = rand_number;
|
|
}
|
|
}
|
|
|
|
void printArray(const float *array, const unsigned int size) {
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
cout << "Day: " << i << " --> " << array[i] << "L\n";
|
|
}
|
|
}
|
|
|
|
float sum(const float *array, const unsigned int size) {
|
|
float liters = 0;
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
liters += array[i];
|
|
}
|
|
return liters;
|
|
}
|
|
|
|
float average(const float *array, const unsigned int size) {
|
|
float liters = sum(array, size);
|
|
liters = liters / size;
|
|
liters = round(10 * liters) / 10;
|
|
return liters;
|
|
}
|
|
|
|
void under_average(const float *array, const unsigned int size) {
|
|
float avg = average(array, size);
|
|
unsigned int dni = 0;
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
if (avg > array[i]) {
|
|
cout << "Day: " << i << " --> " << array[i] << "L\n";
|
|
dni++;
|
|
}
|
|
}
|
|
cout << dni << " days are under average \n";
|
|
}
|
|
|
|
void max_day(const float *array, const unsigned int size) {
|
|
float max_drank = array[0];
|
|
unsigned int day;
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
if (max_drank < array[i]) {
|
|
max_drank = array[i];
|
|
day = i;
|
|
}
|
|
}
|
|
cout << "On day " << day << " you drank the most water " << max_drank << "L";
|
|
}
|
|
|
|
void in_limit(const float *array, const unsigned int size) {
|
|
unsigned int day = 0;
|
|
for (unsigned int i = 0; i < size; i++) {
|
|
if (2.0 <= array[i] && array[i] <= 2.5) {
|
|
cout << "Day: " << i << " --> " << array[i] << "L\n";
|
|
day++;
|
|
}
|
|
}
|
|
cout << day << endl;
|
|
}
|
|
|
|
int main() {
|
|
const unsigned int days = 30;
|
|
float *waterIntakes = new float[days];
|
|
float tmp;
|
|
srand(time(nullptr));
|
|
|
|
fillArray(waterIntakes, days);
|
|
|
|
bool running = true;
|
|
int selection;
|
|
|
|
do {
|
|
menu();
|
|
cin >> selection;
|
|
cout << endl;
|
|
switch (selection) {
|
|
case 1:
|
|
fillArray(waterIntakes, days);
|
|
break;
|
|
case 2:
|
|
printArray(waterIntakes, days);
|
|
break;
|
|
case 3:
|
|
tmp = average(waterIntakes, days);
|
|
cout << "On average you drank " << tmp << "L a day";
|
|
break;
|
|
case 4:
|
|
under_average(waterIntakes, days);
|
|
break;
|
|
case 5:
|
|
max_day(waterIntakes, days);
|
|
break;
|
|
case 6:
|
|
in_limit(waterIntakes, days);
|
|
break;
|
|
case 7:
|
|
tmp = sum(waterIntakes, days);
|
|
cout << "Vsa voda popita" << tmp;
|
|
break;
|
|
case 0:
|
|
running = false;
|
|
break;
|
|
default:
|
|
cout << "Wrong selection!" << endl;
|
|
break;
|
|
}
|
|
cout << endl;
|
|
} while (running);
|
|
|
|
delete[] waterIntakes;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// izpis celotne kolicine vode
|