Thread: Multidimensional arrays and functions.

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    10

    Question Multidimensional arrays and functions.

    I've been lurking around here for a while, but this is my first post. I've been having trouble passing a pointer of a multidimensional array to a function. I think I got it, but i'd like to post it here to see if it's right. Here's the problem we were given:

    Write a complete program and function for the following. You must use a #define for the size of the array. The program will prompt and let the user fill up a 4 x 4 array of integers. Then call a function called TWOES that will be passed a pointer to the array. Using this pointer you must traverse all memory locations in the array and count the element that are not evenly divisible by 2. The function will return this number of elements that not are divisible by 2. Back in main, the program will print out the value that is returned from the function.

    note that the prototype for the function will look like this:

    int TWOES( int * );



    This is what I got:

    Code:
    #include <stdio.h>
    
    #define ROW 4
    #define COL 4
    
    
    int numbers[ROW][COL]; /* Initialize the array */
    
    int TWOES(int *);  /* function prototype */
    
    
    int main()
    {
      int rownum, colnum;
      int count;
    
      for (rownum = 0; rownum < ROW; rownum++)
        {
          for (colnum = 0; colnum < COL; colnum++)
            {
              printf("Please enter an integer:");
              scanf ("%d", &numbers[rownum][colnum]); /* user input into array */
            }
        }
      int *ptr;
      ptr = &numbers[0][0];
    
    count = TWOES(&ptr[0]);      /*call the function */
    
     printf("%d of your numbers are NOT divisible by 2\n",count);      /* print value obtained from function */
    
      return 0;
    
    }
    
    int TWOES(int *tempptr)    /* function */
    {
    
      int r,c;
      int count = 0;
      for(r=0;r<ROW;r++)
        {
          for(c=0;c<COL;c++)
            {
              if(*tempptr % 2 != 0) /* running through the array and testing values */
                  {
                  count++;
                   }
              tempptr++;  /* Increasing by 4 bytes in memory through loop */
            }
        }
      return (count);           /* returning calculation to main() */
        }
    It works. I tested the values passed with printf functions, and they are passing the correct values, but is this right?

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    int *ptr should appear at the top of a scope block, i.e. after int count
    In the TWOES function, you don't need two for loops. You're only incrementing the pointer, anyway, right? So one for loop with the total size of the array will suffice.
    Your indenting needs work.
    Also, you should initialise *all* your variables.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some notes on array flattening.
    http://c-faq.com/aryptr/ary2dfunc2.html
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by QuantumPete View Post
    int *ptr should appear at the top of a scope block, i.e. after int count
    Otherwise it's C99-specific (or C++) code.
    Also, you should initialise *all* your variables.
    There's no need to, as long as values are assigned to them before they are used.

    Also, function names are typically all lowercase or capitalized, like twoes() or Twoes(). All uppercase is an unusual name for a function. It might be mistaken for a macro, which are all uppercase by convention.

    And twoes() isn't a particularly good name to begin with. The plural of "two" is "twos", not "twoes". But even "twos" isn't very good. Something like count_odd_numbers() would be better.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    And twoes() isn't a particularly good name to begin with. The plural of "two" is "twos", not "twoes". But even "twos" isn't very good. Something like count_odd_numbers() would be better.
    I agree with the function name, but the Prof. picked it, so i have to use it. Thanks for all of your input, it realy helps a lot. I'm going to remove the 2nd "for" loop, and move the pointer initialization below "count" (don't know why I put it there ) . Thanks again!

    About the two "for" loops in the function. Is it wrong to do it that way, or just bad programming practice?
    Last edited by miniwhip; 09-12-2007 at 12:57 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    There's nothing wrong with either method. I guess there are two ways of looking at it.

    1. Since the array represents a 2D array, albeit a flattened one, one should use two for loops.
    2. The array is a 1D array, so one should use one for loop.


    I'd probably use one for loop myself, but it's just a matter of preference.

    Neither one is "bad programming practice", though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    10
    Edited program:




    Code:
    #include <stdio.h>
    #define ROW 4
    #define COL 4
    
    int numbers[ROW][COL]; /* Initialize the array */
    
    int TWOES(int *);  /* function prototype */
    
    int main()
    {
      int rownum, colnum;
      int count;
      int *ptr;
      ptr = &numbers[0][0];
    
      for (rownum = 0; rownum < ROW; rownum++)
        {
          for (colnum = 0; colnum < COL; colnum++)
            {
              printf("Please enter an integer:");
              scanf ("%d", &numbers[rownum][colnum]); /* user input into array */
            }
        }
    
    count = TWOES(&ptr[0]);      /*call the function */
    
     printf("%d of your numbers are NOT divisible by 2\n",count);      /* print value obtained from function */
    
      return 0;
      }
    
    int TWOES(int *tempptr)    /* function */
             {
      int i = 0;
      int j =(ROW * COL);
      int count = 0;
      for(i=0; i<j; i++)
        {
          if(*tempptr % 2 != 0) /* running through the array and testing values */
             count++;
             tempptr++;  /* Increasing by 4 bytes in memory through loop */
         }
    
       return (count);           /* returning calculation to main() */
             }
    I just need to go through it and make the indent a little better.

    This site is great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. functions using arrays
    By trprince in forum C Programming
    Replies: 30
    Last Post: 11-17-2007, 06:10 PM
  3. Arrays and Functions
    By KunoNoOni in forum Game Programming
    Replies: 12
    Last Post: 10-04-2005, 09:41 PM
  4. Passing multidimensional arrays to functions
    By maxthecat in forum C Programming
    Replies: 3
    Last Post: 12-22-2001, 03:58 PM
  5. passing multidimensional arrays to functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 03:27 AM