consolidate all repos to one for archive
This commit is contained in:
19
semester_2/programiranje_2/naloga0102/README.md
Normal file
19
semester_2/programiranje_2/naloga0102/README.md
Normal file
@@ -0,0 +1,19 @@
|
||||
1.Preučite in uporabite zgled.(zgled.cpp)
|
||||
|
||||
2.Spremenite program tako, da se v polje vnese 30 naključnih na eno decimalno mesto natančnih števil med 0,5 in 3,5. Vrednosti predstavljajo količino popite vode (v litrih) na dan.
|
||||
|
||||
3.Dopolnite program z izpisom povprečne količine popite vode.
|
||||
|
||||
4.Dopolnite program z izpisom števila dni, ko je bila količina popite vode pod povprečjem.
|
||||
|
||||
5.Dopolnite program z izpisom največje popite količine vode.
|
||||
|
||||
6.Izpišite število dni, ko je bila popita količina vode v mejah priporočenih količin, in sicer med 2 in 2,5 litra.
|
||||
|
||||
|
||||
Končana naloga mora imeti v meniju 6 izbir + izbira za izhod iz programa.
|
||||
|
||||
Pomoč pri generiranju naključnih števil:
|
||||
|
||||
https://en.cppreference.com/w/cpp/numeric/random/rand
|
||||
https://en.cppreference.com/w/cpp/numeric/random/random_device
|
146
semester_2/programiranje_2/naloga0102/naloga0102.cpp
Normal file
146
semester_2/programiranje_2/naloga0102/naloga0102.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#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
|
61
semester_2/programiranje_2/naloga0102/zgled.cpp
Normal file
61
semester_2/programiranje_2/naloga0102/zgled.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
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 << "0 ... EXIT" << endl;
|
||||
cout << "============================" << endl;
|
||||
cout << "Select: ";
|
||||
}
|
||||
|
||||
void fillArray(float* array, const unsigned int size) {
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
array[i] = 0.5f + i * 0.3f;
|
||||
}
|
||||
}
|
||||
|
||||
void printArray(const float* array, const unsigned int size) {
|
||||
for (unsigned int i = 0; i < size; i++) {
|
||||
cout << ((i > 0) ? ", " : "") << array[i];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
const unsigned int days = 10;
|
||||
float* waterIntakes = new float[days];
|
||||
|
||||
srand(time(nullptr));
|
||||
|
||||
bool running = true;
|
||||
int selection;
|
||||
|
||||
do {
|
||||
menu();
|
||||
cin >> selection;
|
||||
switch (selection) {
|
||||
case 1:
|
||||
fillArray(waterIntakes, days);
|
||||
break;
|
||||
case 2:
|
||||
printArray(waterIntakes, days);
|
||||
break;
|
||||
case 0:
|
||||
running = false;
|
||||
break;
|
||||
default:
|
||||
cout << "Wrong selection!" << endl;
|
||||
break;
|
||||
}
|
||||
cout << endl;
|
||||
} while (running);
|
||||
|
||||
delete[] waterIntakes;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user