Thread: Arrays as function arguments!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Arrays as function arguments!

    Hi all!!

    I have a problem. I have to write a program that initialises a 2 dimensional arrayof double and uses a function to copy the sub-arrays of this 2 dimensional function one-by-one in to a seperate array.

    please help. ive spent all day on this and wrote so many variations.
    I can copy a 2 dimensional array within the main function, like so...

    #include <stdio.h>
    #define SIZEA 2
    #define SIZEB 4



    Code:
    main()
    {
    	int i, j;
          double destination2[SIZEA][SIZEB];
          double source[SIZEA][SIZEB]=
    	  {
    		  {5, 3, 6, 7},
    		  {3, 15, 12, 4}
    	  };
    
    	   for(i=0; i<SIZEA; i++)
    	   {
    			for(j=0; j<SIZEB; j++)
    			{
    			destination2[i][j]= source[i][j];
    			printf("%.2f\n", destination2 [i][j]);
    			}
    			printf("\n");
    	   }
    
    }


    but this isnt what the question asks for and im really stuck.

    Thanks in advance

    Stuart

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    An easy rule to remember is copy/paste

    Code:
    void copyArray ( double destination2[SIZEA][SIZEB], double source[SIZEA][SIZEB] ) {
      // your two loops here
    }
    Which you would call with
    Code:
    copyArray( destination2, source );
    You can of course rename the formal parameters if you want to, to be more descriptive in the function body.

    You can also omit the LEFT-MOST array size as well.
    Code:
    void copyArray ( double destination2[][SIZEB], double source[][SIZEB] ) {
      // your two loops here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    thanks a bundle m8!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM