Thread: Array Funtions

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    24

    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!
    Thank you in advance for your time!

    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

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    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

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    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

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    int array[100] = {0}; //initialize array to all zero's
    gg

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    amazing... so adding those little brackets around the "0" fixed all that... go figure!! Thanks again!

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    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?

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    ID is an array, so you have to say:
    Code:
    if (ID[<index value>] < 1000 || ID[<index value>] > 9999)
    Same with bal.

    gg

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    24
    oh duh... I cant believe I left out that very VITAL piece of info... thanks again Codeplug

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM