Thread: what does this code calculate?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    what does this code calculate?

    can anyone tell me what this code calculates?

    Code:
    double calculateAge ( double totalAge[ ], int size, int arrayCounter )
    {
       double totalO$$$es;
       double totalNumber;
    
       for ( int i = 0; i < arrayCounter; i++ )
       {
          if ( totalAge[ i ] <= 21 )
          {
             totalO$$$es += totalAge[ i ];
             totalNumber++;
          }
       }
       return ( totalO$$$es / totalNumber );
    }

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    it doesn't calculate anything as both totalO$$$es and totalNumber are not initialized to any value. The compiler will throw an arbitrary value there, making the whole process have no sense...so what you probably want to do is set both of those variables equal to zero as you initialize them.

    Code:
    double totalO$$$es = 0;
    double totalNumber = 0;
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Originally posted by axon
    it doesn't calculate anything as both totalO$$$es and totalNumber are not initialized to any value. The compiler will throw an arbitrary value there, making the whole process have no sense...so what you probably want to do is set both of those variables equal to zero as you initialize them.

    Code:
    double totalO$$$es = 0;
    double totalNumber = 0;
    axon
    sorry, i forgot to mention that this is a function that is being called. all the other stuff is declared in main.

    as far as i understand, this function is supposed to calculte the average of person's under the age of 21.

    i just dont understand this code -

    totalO$$$es += totalAge[ i ];
    totalNumber++;

    so if you could explain that to me, then that would be helpful.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    it looks like its SUPPOSED to return the average age of everyone 21 or under.

    as was stated before, though,

    double totalO$$$es;
    double totalNumber;

    neither of those are initialized, so it wont calculate correctly. those variables arent defined in main because they are defined in this function. if they were defined as, say, a global, then youd get a redefinition error. set them equal to 0 to get the function to work. this function cant see the values of variables in another function (hint, main is another function).

    also, it looks like the function accepts two parameters of for the same thing, and one of them isnt used. size, i would guess, would be the size of the array, but arraycount apparently holds that same value. size isnt even used in the function so you should probably get rid of it.

    heres how you work through it:

    Code:
    // goes through each element in the totalAge array once.
    for ( int i = 0; i < arrayCounter; i++ )
       {
    // if the age of this element is less than or equal to 21
          if ( totalAge[ i ] <= 21 )
          {
    // add that age to the total age
             totalO$$$es += totalAge[ i ];
    // incriment the total number of people         
             totalNumber++;
          }
       }
    // return the sum of all of the ages divided by the number of people
    // hence, the average age of those people
       return ( totalO$$$es / totalNumber );
    Last edited by ...; 09-17-2003 at 06:45 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    2
    If you are asking specifically what these statements do:

    totalO$$$es += totalAge[ i ];
    ^ This statement is the same as saying:
    totalO$$$es = totalO$$$es + totalAge[ i ];

    totalNumber++;
    ^ This statement is the same as saying:
    totalNumber = totalNumber + 1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM