# Thread: how to write a function that takes integer dynamic array as arguements?

1. ## how to write a function that takes integer dynamic array as arguements?

I m writing a program.. I m nt getting this point

"For this you have to write a function gcdMulti(int[]) that would take the integer array as argument, the array size and print the GCD of the consecutive elements."

I know functions.. I know how to send any integer in a function but nt getting hw to send an integer dynamic array in a function
will it like

Basically the question is this

#1: Write a program that calculates the gcd’s (greatest common divisors) of each of
the two consecutive elements of an array of size n and prints them as shown in the example

below. For this you have to write a function gcdMulti(int[]) that would take the integer array as
argument, the array size and print the GCD of the consecutive elements. Furthermore,
gcdMulti() must call gcd() which returns the GCD of two integers. Identify the complete
signatures of each of these functions and implement them.
I will be grateful if any one can help me plz....

2. we'll be happy to help. what part is causing you trouble?

3. > gcdMulti(int[])
This doesn't take the size... you'd need an int in the prototype too.

After that just run a loop and replace the (n+1)th element with the GCD of nth and (n+1)th element. Return the last element when done.
For each calculation use Euclid's Algorithm.. or some of its optimized versions.

4. Code:
```gcdMulti(int arr[], int size){
int *ptr,i;

ptr=(int *) malloc(size * sizeof(int));

for(i = 0; i < size; i++){
scanf("%d",(ptr+i));
}
}```

now how i use this function in another function?? thats gcd?? and then use this function in main??? if you can plz give me an example by making another program like in the problem mentioned above (having function,pointers,dyamic arrays,and any funtion like sorting etc. or any small program)i will be thankful to you.

5. in another function, you would allocate an array of ints and pass the array pointer and its size to your gcdMulti function. you don't need to allocate a new array inside gcdMulti. you use the one provided as a parameter. also, the array should already contain the data. you don't use gcdMulti to fill the array with data.

Popular pages Recent additions