Array Funtions

• 04-10-2003
GrlNewB
Array Funtions
Hi... I am trying to write a function that, in short, uses info from an ID array and info from a Balance array and computes the average balance for for the Balance array. That average will be returned back to the main program. I am kinda lost right now but I will post what I have so far. Please make any suggestions you may have because I need all the help I can get! :)

Code:

``` //function to determine average balance double ave_bal(int ID [], int num_bal) {                      int subtotal = 0;         int total;         for (int j = 0; j< num_bal; j++0) {         subtotal += ID [j]; }         total = subtotal / num_bal; return total;```
Okay... I know its bad... but I really need help! I cant get the average and I know its a problem with my function code.
Thanks for any help! :)

GrlNewB
• 04-10-2003
Codeplug
Remove the "0" after the "j++" ,change the type of total to double, cast subtotal to double so the compiler will perform floating point division and not integer division.
Remember that in integer division, 5/3 is 1.

gg
• 04-10-2003
GrlNewB
ah, thank you! I am currently working on the rest of the program so I am not able to test it at this time... but I will ASAP and post back whether or not it fixed my problems(s).
Thanks again :)
• 04-10-2003
GrlNewB
Ok, this is my variable declaration at the very beginning of my program. When I try to compile it, it tells me:

: error C2440: 'initializing' : cannot convert from 'const int' to 'int [100]'

I havent declared them as a const, so why is it giving me this error? It shows this error all over the program each time I refer back to either of the two arrays. Any ideas as to why it is doing this?

Code:

```int main () {         int n, j;         int ID [100] = 0;         int bal [100] = 0;         double ave;         double sum_bal;```
The program goes on... but this is where it keeps referring back to when I get the errors. Any help would be much appreciated :)

GrlNewB
• 04-10-2003
Codeplug
Code:

`int array[100] = {0}; //initialize array to all zero's`
gg
• 04-10-2003
GrlNewB
amazing... so adding those little brackets around the "0" fixed all that... go figure!! Thanks again!
• 04-10-2003
GrlNewB
lol... you are going to get tired of me by the time I actually get this program to work :)

This error:

error C2040: '<' : 'int [100]' differs in levels of indirection from 'const int'

keeps pointing to this code:

Code:

``` if (ID < 1000 || ID > 9999)         {                        cout<<"Invalid ID, please reenter: "<<endl;         }                 if (bal > 10000)         {                 cout<<"Invalid Balance, please reenter: "<<endl;         }```
Could you point me to where my error is or what it is concerning?
• 04-10-2003
Codeplug
ID is an array, so you have to say:
Code:

`if (ID[<index value>] < 1000 || ID[<index value>] > 9999)`
Same with bal.

gg
• 04-10-2003
GrlNewB
oh duh... I cant believe I left out that very VITAL piece of info... thanks again Codeplug :)