Thread: /= doesn't do what you think it does?

  1. #46
    Registered User
    Join Date
    Sep 2008
    Posts
    1
    To see what was going on to the example, I added this wide line:

    cout << i << " i | " << namesmap[i] << " namesmap[" << i << "] | " << kFmap[k][namesmap[i]] << " kFmap[" << k << "][" << nam\
    esmap[i] << "] | ";

    This way, I can see what is going on to what where. The output becomes this:

    k = 0
    1 i | one namesmap[1] | 5 kFmap[0][one] | 5 kFmap | 2 | 0.2 |
    2 i | two namesmap[2] | 10 kFmap[0][two] | 10 kFmap | 1 | 0.1 |
    3 i | three namesmap[3] | 5 kFmap[0][three] | 5 kFmap | 0.2 | 0.02 |
    4 i | four namesmap[4] | 20 kFmap[0][four] | 20 kFmap | 1 | 0.1 |

    k = 1
    1 i | one namesmap[1] | 5 kFmap[1][one] | 5 kFmap | 3 | 0.3 |
    2 i | two namesmap[2] | 5 kFmap[1][two] | 5 kFmap | 0.3 | 0.03 |
    3 i | three namesmap[3] | 5 kFmap[1][three] | 5 kFmap | 0.03 | 0.003 |
    4 i | four namesmap[4] | 20 kFmap[1][four] | 20 kFmap | 1.1 | 0.11 |


    For k=1, we can now see that he's accessing F[5] three times, producing the behaviour he observes, as it redivides the quotient it just calculated. As pointed out, he accesses F[20] which is uninitialised (hence why he got 1.1).

    Cheers.

  2. #47
    Registered User
    Join Date
    Jun 2008
    Posts
    106
    Quote Originally Posted by pedestrian879 View Post
    To see what was going on to the example, I added this wide line:

    cout << i << " i | " << namesmap[i] << " namesmap[" << i << "] | " << kFmap[k][namesmap[i]] << " kFmap[" << k << "][" << nam\
    esmap[i] << "] | ";

    This way, I can see what is going on to what where. The output becomes this:

    k = 0
    1 i | one namesmap[1] | 5 kFmap[0][one] | 5 kFmap | 2 | 0.2 |
    2 i | two namesmap[2] | 10 kFmap[0][two] | 10 kFmap | 1 | 0.1 |
    3 i | three namesmap[3] | 5 kFmap[0][three] | 5 kFmap | 0.2 | 0.02 |
    4 i | four namesmap[4] | 20 kFmap[0][four] | 20 kFmap | 1 | 0.1 |

    k = 1
    1 i | one namesmap[1] | 5 kFmap[1][one] | 5 kFmap | 3 | 0.3 |
    2 i | two namesmap[2] | 5 kFmap[1][two] | 5 kFmap | 0.3 | 0.03 |
    3 i | three namesmap[3] | 5 kFmap[1][three] | 5 kFmap | 0.03 | 0.003 |
    4 i | four namesmap[4] | 20 kFmap[1][four] | 20 kFmap | 1.1 | 0.11 |


    For k=1, we can now see that he's accessing F[5] three times, producing the behaviour he observes, as it redivides the quotient it just calculated. As pointed out, he accesses F[20] which is uninitialised (hence why he got 1.1).

    Cheers.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  2. This seemed impossible!Can anyone help??
    By nesir in forum C Programming
    Replies: 23
    Last Post: 08-22-2006, 01:23 AM
  3. Replies: 29
    Last Post: 10-25-2005, 09:46 PM
  4. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM
  5. Replies: 11
    Last Post: 03-25-2003, 05:13 PM