I can't figure out what my prof is talking about.

Pages: 12
In validtemperatures, remember that you can test a bool without a comparison; that's one of the two reasons for using bool instead of int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool validtemperature(int x,int y,int z){

     bool validx, validy, validz;

     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);
    
     if (validx==true&&validy==true&&validz==true){
          return true;
     }
     else { 
          return false;
     }
}


What does this mean?

&&&&&&&&&&

You have too many tests in whatseason; you have not used the final version of the cascaded if/else as your example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//tells you what season it is according to the temperature
void whatseason(double x){
     outfile<<"The average is "<<x<<endl;
       
     if(x>=100)
          outfile<<"It is roasting season"<<endl<<endl;
     else if(x>=80&&x<100)
               outfile<<"It is summer"<<endl<<endl;
          else if(x>=60&&x<80)
               outfile<<"It is spring"<<endl<<endl;
               else if(x>=40&&x<60)
                         outfile<<"It is fall"<<endl<<endl;
                    else if(x>=0&&x<40)
                              outfile<<"It is winter"<<endl<<endl;
       else if(x<0)
                 outfile<<"It is freezin' season"<<endl<<endl;
return;
}


I honestly have no idea what she means. And shes not one to tell me exactly what to do, instead, she would just give hints here and there. Im really stuck and dont know what to do. Can someone please help me. It would be gratefully appreciated.
remember that you can test a bool without a comparison
What she means is that any non zero value evaluates to true, and a value of 0 evaluates to false. So... you can replace
if (validx==true&&validy==true&&validz==true){
with
if (validx && validy && validz)
In other words
1
2
if(1)
  cout << "Hello";

would print Hello and
1
2
if(0)
  cout << "Hello";

would not print Hello.

Also, you can replace else if(x<0) with just else
For formatting purposed, you shouldn't keep indenting each else if statement because it becomes hard to read. Try it like this:
1
2
3
4
5
6
7
8
9
10
if()
..
else if()
..
else if()
..
else if()
..
else
..


Also, you don't need that return; statement in whatseason. Void functions are not required to have any return statements in fact.
Last edited on
your teacher lady wrote:
You have too many tests in whatseason; you have not used the final version of the cascaded if/else as your example.


You carry out some redundant tests in your else if statements. Look at the order of execution logically - if line 7 is reached, that would mean that line 5 would be false, which means that x < 100. So it's already known at line 7 that x < 100, but you test for it regardless.
Thanks maese. That helped a lot. But my prof likes to see a return statement even if its not returning anything...Weird huh?


So branflakes would that mean that for line 7 for example, I should write x>=80 and take out the <100?
Yes, I didn't see that at first glance so credit branflakes for that answer:). Make sure to do that for all the if else statements. Also, like I said previously , replace else if(x < 0) with just else for the same reasons that you are replacing the other ones.
Last edited on
Better code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){

     bool validx, validy, validz;

     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);
    
     if (validx&&validy&&validz)
          return true;
     else 
          return false;
 }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//tells you what season it is according to the temperature
void whatseason(double x){
     outfile<<"The average is "<<x<<endl;
       
     if(x>=100)
          outfile<<"It is roasting season"<<endl<<endl;
     else if(x>=80)
               outfile<<"It is summer"<<endl<<endl;
     else if(x>=60)
               outfile<<"It is spring"<<endl<<endl;
     else if(x>=40)
               outfile<<"It is fall"<<endl<<endl;
     else if(x>=0)
               outfile<<"It is winter"<<endl<<endl;
     else
          outfile<<"It is freezin' season"<<endl<<endl;
return;
}
You can condense validtemperature to one line without reducing readability.
what do you mean by that?
Better code?
If your teacher doesn't give you an A for this, then she is not a nice teacher.
Sorry i meant is this better code than what I originally posted? haha
I mean you can replace the code in validtemperature with one line that does the same thing. When I say "without reducing readability" I mean usually when you can reduce a bunch of operations down to one line it typically becomes an obfuscated piece of code. Not in this case.

Here's a hint:

1
2
3
4
if (X)
    return true;
else
    return false;

is equivalent to:

return X;
OO shacktar, I didn't think about that. Your teacher would be empressed cosimo. :p
I'm probably wrong but this is what i whipped up.

1
2
3
4
if (validx=isvalid&&validy=isvalid&&validz=isvalid)
          return true;
     else 
          return false;
Hint 2: you do not need to use the variables validx, validy, and validz.
1
2
3
4
if (isvalid)
          return true;
     else 
          return false;


hmm...this?
Dang shacktar, I thought you meant
return (validx && validy && validz);

Even better lol..
@Cosimo: isvalid takes a parameter, right?

this

1
2
3
4
if (isvalid)
    return true;
else
    return false;


will always return true because the expression isvalid evaluates to the address of the function, which is non-zero, which is true. Check out the code that Maese909 posted above but try doing that without using the three variables.
Last edited on
so was i right?
Ok so can do I leave this:

1
2
3
4
5
6
7
8
9
//Check to see if temperature>=-10 and <=105
bool validtemperature(int x,int y,int z){

     bool validx, validy, validz;

     validx=isvalid(x);
     validy=isvalid(y);
     validz=isvalid(z);
 


Then delete this

1
2
3
4
5
  if (validx&&validy&&validz)
          return true;
     else 
          return false;
 }


Then insert this and fix it?
 
return (validx && validy && validz);
Not quite, like Shacktar stated, you want the only line of code in your function to look like this:
return (X);
Here's another hint, use the three variables declared in your parameter list (x, y, z), call the function isvalid for each, and totally disregard the other variables you have declared.

Keep in mind that (true && true && true) will return a true
Last edited on
Pages: 12