# Thread: what does this code calculate?

1. ## 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. 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

3. 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. 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 );```

5. 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