• C

arrays

i have a topic where i have to pass an array of numbers through a function using the information coming from a disk
and then take the average of the numbers

how would i do this?
shorty225Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

Hi shorty225,

This is obviously a homework assignment and the membership agreement prohibits us from doing your homework for you.  If you've got a specific problem or question we can certainly help you with that.  We can also help you correct code that is giving you trouble.  If you make an attempt to code something you'll get no shortage of answers from people willing to help.

Your question is a bit too brief to get much in the way of "general help".  If you'll post more of the assignment we'll give you some guidelines.

Good Luck,
Kent
0
shorty225Author Commented:
i am just asking how to pass arrays through a function and get the average not asking for the complete lab assignment
0
tinchosCommented:
In order to pass an array to a function then you can pass the array (of ints in the example)


float average( int *array, int numberOfElements )
{
   float average = 0;

   /*
    * Iterate through the elements of the array and calculate the average
    */
 
   return average;
}

int main()
{
   int array[] = { 1, 3, 66, 77, 4, 82, 21, 14 };

   float f = average( array, 8 );       /* 8 as there are 8 elements in the array */

  // rest of your code

   return 0;
}

I guess that this is pretty much how you would pass an array to a function.

Hope this helps

Tincho
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kent OlsenData Warehouse Architect / DBACommented:
Hi shorty,

We've also got a small "language barrier" here.  Passing an array "through a function" is a mathematical term that describes a process, such as passing data through an fft (Fast Fourier Transform).  In C, data is passed TO a function.  The function can then use the data and/or pass it to another function.  If the function passes the data to another function, then technically, the data will have been passed "through" the first function, but I doubt that is what you meant.

Parameter passing in C comes in two flavors.  You can pass a value or you can pass an address.  (Passing an address is often called passing by reference.  Some purists claim that C doesn't pass by reference, others claim that it does.  The argument is certainly beyond the scope of your question.)

When you pass a "native" variable like a char, short, int, long, float, or double you are passing a value.  The value of the variable is placed on the stack and the called function uses the value as if it belonged to it.  Actually, it does.  The variable is created on the stack specifically FOR the called function.

When you pass arrays, you pass by address.  The compiler does NOT copy the entire array to stack.  It copies only the address of the array.  This is certainly much faster than copying a potentially large amount of data, but has the drawback that the called function can actually read and write the original data, not a copy as what happens when you pass by value.

To pass an address (such as an array), the function parameter(s) simply define the parameter as an address.  You do that with the asterisk (*) or array notation ([]).  Note that you can "mix and match" parameters -- some parameters can be "by value" and other can be "by address".  But any one parameter needs to be consistent throughout your program.

myFunction(char *string)
{
}

This function declaration says that the address of a string is going to be passed.  You can call this function several different ways.

myFunction ("This is a sample string.");

char myArray[30] = {"This is also a sample string.");

myFunction (myArray);


For many usages, the asterisk and array notation are interchangable.

myFunction (char *string)

is equivalent to

myFunction (char string[])

The compiler know that the parameter being passed is the address of the first character of a string.  Within a function that is declared with either style, you can also use either the pointer notation or the array notation to access elements of the string.

myFunction (char *string)
{
  if (*string == string[1])  /*  test if the first two characters are the same  */
...
}

This is perfectly legal.  Though mixing pointer and index notation can be confusing.

Of course, you can pass arrays of other data types, too:

tableFunction (int Array[]);
matrixFunction (int Array[10][25]);

Note that if the array has only one index (a one dimensional array) you do not need to declare the size of the array.  This is because the compiler knows how to reference any element of the array.  C doesn't do "bounds checking" so there is no need to tell the function how large the array is.

But if the array has multiple indices you must tell the function the size of the array.  The concept of a two-dimensional table means nothing to C because memory is consecutive.  C needs to be able to convert your multi-dimensional structure to an offset into a block of memory and the only way that this can happen is if the function knows the size of your table.

So to wrap up, you pass an array by telling the function the starting address of the data.

Good Luck,
Kent
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.