4

I'm doing the first project euler problem and I just did this

#include <iostream>
using namespace std;

int main(){

int threes =0;
int fives = 0;
int both = 0;
for (int i = 0; i < 10; i++){
      
       if(i%3==0){
   threes += i;
   }
  
       if(i%5==0){
   fives += i;
   }
   
      if ( i % 5 == 0 && i % 3 == 0){
      both += i;
  }

}

cout << "threes = " << threes << endl;
cout << "fives = " << fives << endl;
cout << "both = " << both << endl;

cout << " threes + fives - both = " << endl;
int result = (threes + fives) - both;
cout << result<< endl;

return 0;
}

My professor recently corrected me for doing this in a different problem saying something about else statements, but I don't understand WHY i have to add else in front of the next if. for what its worth I have another version with else if(i%5){ fives += .... } and they both work and get me the right answer.

My question is whats inherently wrong with this way of thinking, is it stylistic or am I not thinking logically about something?

If it works, why use switch statements ever?

1
  • Based on the question: I bet that you are not supposed to increment both if you also increment three and five. i.e. each time through the loop only one variable needs to be incremented. Jun 5, 2011 at 15:21

5 Answers 5

5

The only thing that I see wrong with your implementation is that in the case where the number is both a multiple of 3 and a multiple of 5 not only is the both variable incremented but the fives and threes variables are also incremented. Based on what the professor described I believe he wants you to use an else-if so that the both variable is the only one that is incremented when you pass in a number that is both a multiple of 3 and a multiple of 5.

The reason you get the correct answer both ways is because you are only going to 10 in the for loop, if you increase it to i <= 15 you will get fives and threes being 1 higher than I think he intended.

For example:

for( int i = 0; i < 10; i++ )
{
   if( ( ( i % 3 ) == 0 ) && ( ( i % 5 ) == 0 ) )
   {
      both++;
   }
   else if( ( i % 3 ) == 0 )
   {
      threes++;
   }
   else if( ( i % 5 ) == 0 )
   {
      fives++;
   }
}
1
  • the professor comment I made was for a different problem. I actually don't need the both variable at all. My final implementation is if ( i % 3 == 0 ){ threes += i ; } else if ( i % 5 == 0 ) { fives += i; } I don't need both because a number like 15 will be true for the 3 part and thus skip the 5 statement, so all numbers are only being counted once. its weird cause I've had three ideas/implementations and they've all given me the correct answer...
    – Dave2081
    Jun 5, 2011 at 8:36
3

The else branch in an if-else statement is only executed if the if branch is false. If you just have two if statements in a row, they'll both be executed, which can be a waste. In the below code, the else prevents the second computation from running if the first is satisfied.

if (expensive_computation1()) {
  ...
}
else if (expensive_computation2()) {
  ...
}

Additionally, it's clearer to humans reading the code whether both if statements should be allowed to run or whether only one should.

0
2

In this case, maybe you really want this:

if (i % 5 == 0 && i % 3 == 0) {
    both += i;
} else if (i % 3 == 0) {
    threes += i;  
} else if (i % 5 == 0) {
    fives += i;
}

(why you do += i instead of ++ I don't know but you didn't explain, so I just copied it)

In your code, threes and fives were incremented even if it would also increase both, which depending on your problem may not be what you want. If you do the if/else way I've just presented, only one of the three variables is increased.

1
  • the original question was, "Find the sum of all the multiples of 3 or 5 below 1000". I want to add the numbers up not keep track of how many there are, which is why did += . I actually got rid of the both idea. didn't need it. thanks though, everyones answer has helped
    – Dave2081
    Jun 5, 2011 at 8:40
1

Why use if-else instead of multiple if's?
if-else & if would achieve the same results but if-else achieves them in a performance enhanced way. with multiple if's every if condition will have to be checked. With if-else only one conditional check will have to be performed and the rest of the conditions just don't need to be checked at all.

This would not affect a small program like the one you have but it will sure have some impact in potentially expensive function being called repeatedly over and over again.

If it works, why use switch statements ever?
With nested if-else conditions the code is hard to read and understand. The switch-case construct helps to represent the conditions in a much easier to read & understand format.

0

To me it looks like stylistics. You can use some autoreformating tool that follows certain established stylistic look (K&R, ANSI, GNU, etc.)

For example astyle is such tool, http://astyle.sourceforge.net/ - just reformat your code with it, and you might have a happy professor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.