help! 3 dimensional array

This is a discussion on help! 3 dimensional array within the C Programming forums, part of the General Programming Boards category; can you help me with my assignment.. a complete program to store the following value in a 3-dimensional array and ...

  1. #1
    nio
    nio is offline
    Registered User
    Join Date
    Jul 2007
    Posts
    2

    help! 3 dimensional array

    can you help me with my assignment..
    a complete program to store the following value in a 3-dimensional array
    and display the outputs of the data in the array
    1 2 3 4
    2 4 6 8
    3 6 9 12
    4 8 12 16

    2 4 6 8
    4 8 12 16
    6 12 18 24
    8 16 24 32

    3 6 9 12
    6 12 18 24
    9 18 27 36
    12 24 36 48

    thanks in advance
    Last edited by nio; 07-24-2007 at 02:09 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Code:
    int three_dee[3][4][4] = {
      { /* First block */
        { 1, 2, 3, 4 }, /* First row */
      },
    };
    Extend to suit.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    The following code does what you want...If you get in any trouble let me know.
    Hope i helped.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //Functions.
    
    int ***Create3dArray(int D, int R, int C)
    {
        int i,j;
        int ***ptrArray = NULL;
        ptrArray = (int ***)calloc(D, sizeof(int **));
        if(ptrArray)
        {
                    for(i = 0; i < D && (ptrArray[i] = (int **)calloc(R, sizeof(int*))) != NULL; i++);
                    if(i == D)
                    {
                         for(i = 0; i < D; i++)
                               for(j = 0; j < R; j++)
                                     ptrArray[i][j] = (int *)calloc(C, sizeof(int));
                         if(i == D && j == R)
                              return ptrArray;
                         else
                             return NULL;
                    }
                    else
                        return NULL;
        }
        else
            return NULL;
    }
    void Print3dArray(int ***ptrArray, int D, int R, int C)
    {
         int i, j , k;
         for(i = 0; i < D; i++)
         {
               for(j = 0; j < R; j++)
               { 
                     for(k = 0; k < C; k++)
                     {
                           printf("Array[%d][%d][%d] = %d\n", i,j,k, ptrArray[i][j][k]);
                     }
                     printf("\n");
               }
               printf("\n\n");
         }
    }
    void InitArray(int ***Array, int D, int R, int C, int Start)
    {
         if(Array)
         {
                  int i , j , k;
                  int CStart = Start;
                  int r = 1;
                  for(i = 0; i < D; i++)
                  {
                        for(j = 0; j < R; j++)
                        {
                              for(k = 0; k < C; k++)
                              {
                                    Array[i][j][k] = CStart * r;
                                    r += 1;
                              }
                              r = 1;
                              CStart += Start;
                        }
                        CStart = ++Start;
                        r = 1;
                  }
                        
         }
         else
             printf("Array is NULL.\n");
    }
                                               
    int main(int argc, char *argv[])
    {
        int ***Array = Create3dArray(3,4,4);
        InitArray(Array, 3,4,4,1);
        Print3dArray(Array, 3, 4, 4);
        printf("Hit any key to continue...\n");
        getch();
      return 0;
    }

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Shouldn't you free the memory allocated by calloc(...) at the end of your program ?

    I think that allocating memory dynamically is a bit "overcomplex" for this situation, where the stack could have handle it pretty fine.

    It could have looks like this
    Code:
    #define D 3
    #define R 4	// Would be nice to describe what all those constant mean by the way heh
    #define C 4
    
    int main()
    {
    	int array[C][R][D];
    
    	//...
    
    	return 0;
    }
    and you don't have to worry about freeing the memory since it's automatic this way.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Yes i agree, but should i did it all???
    I used dynamic allocation because you can use it for any R,C,D.
    If nio likes and surely understands my code, will expand it by adding the function which will free the memory. if not i can do it by myself by editing my message.

  6. #6
    nio
    nio is offline
    Registered User
    Join Date
    Jul 2007
    Posts
    2
    Quote Originally Posted by Bokarinho View Post
    The following code does what you want...If you get in any trouble let me know.
    Hope i helped.

    Code:
    int ***Create3dArray(int D, int R, int C)
    -Error: Declaration syntax error

  7. #7
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    -Error: Declaration syntax error
    Hmm...looks like you'll have to think of a solution, not copy one.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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