Solved

arrays

Posted on 2003-12-03
4
624 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:shorty225
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 9866697

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
 

Author Comment

by:shorty225
ID: 9866897
i am just asking how to pass arrays through a function and get the average not asking for the complete lab assignment
0
 
LVL 9

Accepted Solution

by:
tinchos earned 500 total points
ID: 9866970
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9867162
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now