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

This is a discussion on how to write a function that takes integer dynamic array as arguements? within the C Programming forums, part of the General Programming Boards category; I m writing a program.. I m nt getting this point "For this you have to write a function gcdMulti(int[]) ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    82

    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. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    we'll be happy to help. what part is causing you trouble?

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    > 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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    82
    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. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    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 subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. Replies: 1
    Last Post: 12-07-2005, 09:23 PM
  3. Replies: 5
    Last Post: 05-06-2004, 09:14 AM
  4. Function arguements
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 12-28-2002, 10:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21