Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Simple allocate and loop-through bug,

Simple allocate and loop-through bug,

Scheduled Pinned Locked Moved Clever Code
c++comarchitecturehelpquestion
5 Posts 4 Posters 11 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Maunder
    wrote on last edited by
    #1

    Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)

    1 #include
    2 #define FIRST_YEAR 1901
    3 #define LAST_YEAR 2006
    4 #define NSTATIONS 100
    5 extern double
    6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
    7
    8 void print_temperatures()
    9 {
    10 double total;
    11
    12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
    13 {
    14 total = 0.0;
    15 for( int j = 0; j < NSTATIONS; j++ )
    16 total += temperatures[i-FIRST_YEAR][j];
    17 printf( "Ave. temp. for year %d is %g\n",
    18 i, total / NSTATIONS );
    19 }
    20 }

    cheers, Chris Maunder

    CodeProject.com : C++ MVP

    J R 2 Replies Last reply
    0
    • C Chris Maunder

      Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)

      1 #include
      2 #define FIRST_YEAR 1901
      3 #define LAST_YEAR 2006
      4 #define NSTATIONS 100
      5 extern double
      6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
      7
      8 void print_temperatures()
      9 {
      10 double total;
      11
      12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
      13 {
      14 total = 0.0;
      15 for( int j = 0; j < NSTATIONS; j++ )
      16 total += temperatures[i-FIRST_YEAR][j];
      17 printf( "Ave. temp. for year %d is %g\n",
      18 i, total / NSTATIONS );
      19 }
      20 }

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #2

      <= -> >= (You'll be looping through every year that's not in the interval LAST_YEAR ... FIRST_YEAR, which is probably not what you want) Nevermind, it'll never loop. :-O

      -- A Stern Warning of Things to Come

      1 Reply Last reply
      0
      • C Chris Maunder

        Anyone care to take a crack at this one? It's from a well-known company but no mentions of who till it's solved (in order to stop Googling)

        1 #include
        2 #define FIRST_YEAR 1901
        3 #define LAST_YEAR 2006
        4 #define NSTATIONS 100
        5 extern double
        6 temperatures[LAST_YEAR+1-FIRST_YEAR][NSTATIONS];
        7
        8 void print_temperatures()
        9 {
        10 double total;
        11
        12 for( int i = LAST_YEAR; i <= FIRST_YEAR; i-- )
        13 {
        14 total = 0.0;
        15 for( int j = 0; j < NSTATIONS; j++ )
        16 total += temperatures[i-FIRST_YEAR][j];
        17 printf( "Ave. temp. for year %d is %g\n",
        18 i, total / NSTATIONS );
        19 }
        20 }

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        R Offline
        R Offline
        Rama Krishna Vavilala
        wrote on last edited by
        #3

        i >= FIRST_YEAR but may be a typo.


        Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

        C 1 Reply Last reply
        0
        • R Rama Krishna Vavilala

          i >= FIRST_YEAR but may be a typo.


          Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -Brian Kernighan

          C Offline
          C Offline
          Chris Maunder
          wrote on last edited by
          #4

          It took me a disturbingly long time to pick that one.

          cheers, Chris Maunder

          CodeProject.com : C++ MVP

          R 1 Reply Last reply
          0
          • C Chris Maunder

            It took me a disturbingly long time to pick that one.

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            R Offline
            R Offline
            Russell Morris
            wrote on last edited by
            #5

            Chris Maunder wrote:

            It took me a disturbingly long time to pick that one.

            I spent the whole time looking for a fence-post problem. It wasn't until I googled "[LAST_YEAR+1-FIRST_YEAR][NSTATIONS]" that I found the answer :-O

            -- Russell Morris Morbo: "WINDMILLS DO NOT WORK THAT WAY!"

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups