2

this is probably a dumb question but I just can't figure it out. It has to do with the differences between n++ and ++n (which I thought I understood but apparently not).

#include <stdio.h>
#include <math.h>

long algorithmA(int n);
long algorithmB(int n);

int main(){
    long A, B;
    A = B = 0;
    int n = 1;
    while(A >= B){
        A = algorithmA(n);
        B = algorithmB(n);
        n++;
    }
    printf("At n = %d, Algorithm A performs in %ld seconds & "
           "Algorithm B performs in %ld seconds.", n, A, B);

}

long algorithmA(int n){
    return pow(n,4) * 86400 * 4;
}

long algorithmB(int n){
    return pow(3,n);
}

Here you can probably tell I'm trying to see at what point Algorithm A outperforms Algorithm B. The functions and units of time were given to me in a homework problem.

Anyways, I always thought that the order of "++" would not matter at the end of a while loop. But if I put ++n instead of n++, I get the wrong answer. Can somebody explain why?

Edit: Well it WAS showing 24 with ++n and 25 with n++, but it must have been for another reason. Because I just checked now and there is no difference. Thanks for your patience and time guys, I just wish I knew what I did!

Steady
  • 33
  • 1
  • 1
  • 6
  • 1
    Not sure if a direct duplicate (different language) but I bet the problem is the same. [Is there a difference between x++ and ++x](http://stackoverflow.com/questions/1094872/is-there-a-difference-between-x-and-x-in-java?rq=1). – takendarkk Sep 06 '14 at 21:40
  • @mlwn I think you got those backwards. – takendarkk Sep 06 '14 at 21:41
  • @Takendarkk lol.. just deleted it.. :) mis typing.. – mlwn Sep 06 '14 at 21:41
  • 1
    When the operation stands alone, and its "result" is not used, there is no difference. Only if the result is used (as in `myArray[n++]`) does it make a difference, and that difference is whether you effectively fetch the array element before incrementing or after. – Hot Licks Sep 06 '14 at 21:43
  • 1
    There shouldn't be any difference between these, since the increment isn't used in an expression. Do you have output that shows otherwise? – hobbs Sep 06 '14 at 21:44
  • (It should not make a difference in the above code.) – Hot Licks Sep 06 '14 at 21:44
  • Hmm really weird, it must have been something else. I checked again and there was no difference. Thanks guys. – Steady Sep 06 '14 at 21:49

3 Answers3

9

If you increment without assignment, no difference. However, in the following circumstances, there is:

int n = 1;
int x = n++; // x will be 1 and n will be 2

In this example, the statement gets executed prior to the increment.

int n = 1;
int x = ++n; // both x and n will be 2

However, in this example, increment occurs prior to the execution of the statement.

Operator precedence can help you out.

B.K.
  • 9,982
  • 10
  • 73
  • 105
5

The only difference between n++ and ++n is that n++ yields the original value of n, and ++n yields the value of n after it's been incremented. Both have the side effect of modifying the value of n by incrementing it.

If the result is discarded, as it is in your code, there is no effective difference.

If your program is behaving differently depending on whether you write

n++;

or

++n;

it must be for some other reason.

In fact, when I compile and execute your program on my system, I get exactly the same output in both cases. Adding newlines to the output format, I get:

At n = 25, Algorithm A performs in 114661785600 seconds &
Algorithm B performs in 282429536481 seconds.

You haven't told us what output you're getting. Please update your question to show the output in both cases.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

The prefix versions (++n) alter the variable and then pass along its value. The postfix version (n++) pass along the current value and then alter the variable.

Zain
  • 43
  • 1
  • 1
  • 8