assign value of boolean equal to validity of statement
May 19, 2019 at 10:56pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main () {
ifstream fin ("measurement.in" );
ofstream fout ("measurement.out" );
int z = 0;
int n;
fin>>n;
int day [100];
string name [100];
int change [100];
for (int i=1; i<=n; i++) {
fin>>day[i]>>name[i]>>change[i];
}
int bessiemilk, mildredmilk, elsiemilk;
bessiemilk = mildredmilk = elsiemilk = 7;
bool bessie_milk, mildred_milk, elsie_milk;
bessie_milk = mildred_milk = elsie_milk = true ;
for (int currday=1; currday<=100; currday++) {
for (int i=1; i<=n; i++) {
if (currday==day[i]) {
if (name[i]==("bessie" )) {
bessiemilk = bessiemilk + change [i];
}
if (name[i]==("mildred" )) {
mildredmilk = mildredmilk + change [i];
}
if (name[i]==("elsie" )) {
elsiemilk = elsiemilk + change [i];
}
}
}
int maxmilk = max(bessiemilk, max(mildredmilk, elsiemilk));
bool bessie_milk2 = (bessiemilk == maxmilk);
bool mildred_milk2 = (mildredmilk == maxmilk);
bool elsie_milk2 = (elsiemilk == maxmilk);
if (bessie_milk!=bessie_milk2 || mildred_milk != mildred_milk2|| elsie_milk!=elsie_milk2) {
z++;
}
bessie_milk=bessie_milk2;
mildred_milk=mildred_milk2;
elsie_milk=elsie_milk2;
}
fout<<z;
}
for these:
bool bessie_milk2 = (bessiemilk == maxmilk);
bool mildred_milk2 = (mildredmilk == maxmilk);
bool elsie_milk2 = (elsiemilk == maxmilk);
how do I make the value of the booleans on the left equal to the validity of the statement (true/false) of the statement on the right? Is this correct already?
Last edited on May 19, 2019 at 11:03pm UTC
May 20, 2019 at 2:42am UTC
bool bessie_milk2 = (bessiemilk == maxmilk);
sure
but please differentiate your variable names
by the way, valid array indices start at 0 and end at size-1
May 20, 2019 at 3:05pm UTC
Is this correct already?
YES.
you can also treat boolean expression results as the integer 0 and 1 which can be very useful (as an array index or the sum of true results, etc).
Topic archived. No new replies allowed.