Thread: passing 3D array to a linked list.

  1. #1
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96

    passing 3D array to a linked list.

    Hello Every1,
    I was trying to pass a 3D array to a linked list.
    My 3D array is an array of multiple different matrices
    like
    Code:
    
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       1       0
    0       0       0       0       0       0
    0       0       1       0       0       1
    0       0       0       0       1       0
    
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       0       0
    
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       0       0
    0       0       0       0       1       1
    0       0       0       1       0       0
    0       0       0       1       0       0
    but only the first matrix is read into the Linked list....
    any hints why that is happening?

    thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    35
    Post your code?

  3. #3
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    hey AntP...(I was actually trying to reach u) as u had helped me with my last LL code.
    Now I am passing all my simulated code to a LL (with which u had helped with ) but have problems as i have mentioned ahead...
    my code is
    Code:
    int ***Sim_edges( float ***array1){   // Simulation of Edges (but only for those that have nodes)
      int i,j,k;
    int ***array2;
      double prob;
      const gsl_rng_type * TT;
      gsl_rng * r;
      gsl_rng_env_setup();     // GSL environment Setup
      TT = gsl_rng_default;
      r = gsl_rng_alloc (TT);
      array2 = (int ***)malloc(sizeof(int **) * iter);
      for (i = 0 ;  i < iter; i++) {
        array2[i] = (int **)malloc(sizeof(int *) * nodes);  
        for (j = 0; j < nodes; j++) {          
          array2[i][j] = (int *)malloc(sizeof(int) * nodes);
          
        }
      }
      for (i=0; i<iter; i++){
        for (j=0; j<nodes; j++){
          for (k=0; k<nodes; k++){
    	
    	array2[i][j][k]=0;
    	prob = array1[i][j][k];
    	array2[i][j][k] = gsl_ran_bernoulli(r, prob);   // calling GSL Library
    	
    	//printf("%d\t",array2[i][k][j]);  // storing the simulated edge values
          }
          //  printf("\n");
        }
        //  printf("\n");
      }
    for (i=0; i<iter; i++){
        for (j=0; j<nodes; j++){
          for (k=0; k<nodes; k++){
    	array2[i][k][j] = array2[i][j][k]; // to make is adjacency matrix
    	printf("%d\t",array2[i][k][j]);  // storing the simulated edge values
          }
          printf("\n");
        }
        printf("\n");
      }
    
      return array2;	  this array had to be passed to the LL for BFS run  
    }
    
    struct Edge
    {
    int terminal;
    struct Edge *next;
    };
    
    struct Vertex
    {
    int visit;
    int vertex_no;
    char info;
    int path_length;
    struct Edge *Edge_Ptr;
    };
    struct Q
    {
    int info;
    struct Q *next;
    };
    
    void Table(int , int ***array2, struct Vertex vert[size]);
    struct Edge *Insert_Vertex (int , struct Edge *);
    void BFS ( int , struct Vertex vert [size]);
    
    
    struct Q *Insert_Queue(int vertex_no, struct Q *first); // Insert vertex in the queue (if any conncection found)
    struct Q *Delete_Queue(int *vertex_no, struct Q *first);// Delete vertex if it has been visited 
    
    /* Insert vertex into connectivity list */
    
    struct Edge * Insert_Vertex (int vertex_no, struct Edge
    *first) {
    struct Edge *new1, *current;
    new1 = (struct Edge *) malloc(sizeof(struct Edge));
    new1->terminal = vertex_no;
    new1->next = NULL;
    if (!first)
    return (new1);
    for (current = first; current->next; current = current->next);
    current->next = new1;
    return (first);
    }
    
    /* Insert vertices into queue */
    
    struct Q * Insert_Queue(int vertex_no, struct Q *first)
    {
      struct Q *new1, *current;  // getting the 
    new1 =(struct Q *) malloc(sizeof(struct Q));
    new1->info = vertex_no;
    new1->next = NULL;
    if (!first)
    return (new1);
    for (current = first; current->next; current = current->next);
    current->next = new1;
    return (first);
    }
    
    struct Q * Delete_Queue(int *vertex_no, struct Q *first)
    {
    struct Q *previous;
    if (!first)
    return (NULL);
    *vertex_no = first->info;
    previous = first;
    first = first->next;
    free(previous);
    return (first);
    }
    
    /* Initializing entries */
    
    void Table(int vertex_num, int ***array2,struct Vertex vert[size])
    {
      int i, j,k;
    for (i = 0; i < vertex_num; i++)
    {
    vert [i].visit = F;
    vert [i].vertex_no = i+1;
    vert [i].info = 'A'+ i;
    vert [i].path_length = 0;
    vert [i].Edge_Ptr = NULL;
    }
    for(i= 0; i <iter; i++)
    for (j =0; j < vertex_num ;j++)
    for (k =0; k < vertex_num ; k++)
    if (array2[i][j][k] > 0 )
    vert [j].Edge_Ptr = Insert_Vertex (k, vert [j].Edge_Ptr);
    }
    
    /* Computing path length */
    
    void BFS ( int index, struct Vertex vert [size])
    {
    struct Q *queue = NULL;
    struct Edge *Link;
    vert [index].visit = T;
    queue = Insert_Queue(index, queue);
    while(queue)
    {
    queue = Delete_Queue(&index, queue);
    for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next)
    {
    if (vert [Link->terminal].visit == F)
    {
    vert[Link->terminal].visit = T;
    vert[Link->terminal].path_length=vert[index].path_length+1;
    queue = Insert_Queue(Link->terminal, queue);
    }
    }
    }
    }
     
    
    int main(argc,argv)
    int argc;
    char *argv[];
    {
     float **mat;
      float ***array1;
      int ***array2;
      int  index =5;
      int i;
      int option = 2;
      nodes =  NODES();
      edges = EDGES();
      mat =  MAT(edges, nodes);
      array1 =Sim_nodes(mat);
      array2 =Sim_edges(array1);
      struct Vertex vert [size];
      struct Edge *List;
      Table(nodes, array2, vert);
      printf("\n Input the source node %d :",index);
    BFS (index, vert);
    printf("\n Input the target node %d:" ,option+1);
    
    if(vert[option].path_length > 0)
        printf("\nConnected.\n");
    else
        printf("\nNot connected.\n");
    
      
    
      
    free (pR1), (pR2),(pC1),(pC2),(pC3),(pC4);
      return 0;
    }
    I dont know If i have to make chanes in the BFS function or I am passing the array in some wrong way

  4. #4
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by satty View Post
    Code:
    .
    .
    .
    int main(argc,argv)
    int argc;
    char *argv[];
    {
     float **mat;
      float ***array1;
      int ***array2;
      int  index =5;
      int i;
      int option = 2;
    .
    .
    .
    free (pR1), (pR2),(pC1),(pC2),(pC3),(pC4);
      return 0;
    }
    1) Since you are not indenting your code in a reasonable manner, it is not worth my time to read most of it.
    2) Why are you using K&R1 style definition of main( ) instead of the prototype style from the ANSI standard (C89) and C90? int main(int argc, char *argv[]) The K&R1 style still work, but the prototype style is more readable (in my opinion).
    3) The free line is nearly equivalent to the following:

    Code:
    free (pR1);
    (pR2);
    (pC1);
    (pC2);
    (pC3);
    (pC4);
    Only the first line potentially frees memory. This does not call free with each set of parameters.
    4) I do not see any definition for pR1, pR2, pC1, pC2, pC3, or pC4 in scope for main( ) at that point. I could easily miss it because of the indenting.

  5. #5
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    I didnt put the whole of the code..... its a part of it.
    My only question is why the
    Code:
     return array2;
    from first function of Sim_edges is not read into the next function i.e.
    Code:
    void Table(int , int ***array2, struct Vertex vert[size]);
    Ya, you are right i shd have idented it more.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by satty View Post
    Code:
    int ***Sim_edges( float ***array1){   // Simulation of Edges (but only for those that have nodes)
      int i,j,k;
    int ***array2;
      double prob;
      const gsl_rng_type * TT;
      gsl_rng * r;
      gsl_rng_env_setup();     // GSL environment Setup
      TT = gsl_rng_default;
      r = gsl_rng_alloc (TT);
      array2 = (int ***)malloc(sizeof(int **) * iter);
      for (i = 0 ;  i < iter; i++) {
        array2[i] = (int **)malloc(sizeof(int *) * nodes);  
        for (j = 0; j < nodes; j++) {          
          array2[i][j] = (int *)malloc(sizeof(int) * nodes);
          
        }
      }
    ...
      return array2;	  this array had to be passed to the LL for BFS run  
    }
    In Sim_edges( ), array2 is not a three dimensional array. It is a one dimensional array of pointers to pointer to int. Those pointers are set to point to one dimensional arrays of pointer to int.

    The loop that indexes using j probably has undefined behavior since it assumes that array2 is the address of a two-dimensional array. I do not have a compiler available at the moment, but I think you need:

    for (j = 0; j < nodes; j++) {
    (*(array2[i]))[j] = malloc(sizeof(int) * nodes);

    array2 is a pointer to a one dimensional array of pointers to pointer to int. array2[i] selects an element of that one dimensional array and is thus a pointer to a one dimenstional array of pointers to int. *(array2[i]) is a one dimensional array of pointers to int. (*(array2[i]))[j] selects an element of that one dimensional array and is thus a pointer to int. You are assigning to that pointer a one dimensional array of int.

    You will have to change the next for loop in Sim_edges() in a similar way.

    In any case, remove the casts of malloc (in C) and turn up the warning level on your compiler.

    I hope that helps. If I could easily draw the diagram and upload it, I would.

  7. #7
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    thanks...
    i don't think i followed you completely but i tried changing the script as u refereed n i get a BUS ERROR.

    can i define 3D array as
    Code:
     array2 = (int ***)malloc(sizeof(int **) * iter);
      for (i = 0 ;  i < iter; i++) {
        array2[i] = (int **)malloc(sizeof(int *) * nodes);  
        for (j = 0; j < nodes; j++) {          
          (*(array2[i]))[j] = malloc(sizeof(int) * nodes);
           for (k = 0; k < nodes; k++)
          array2[i][j][k] = i * j * k;
        }

  8. #8
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by satty View Post
    thanks...
    i don't think i followed you completely but i tried changing the script as u refereed n i get a BUS ERROR.

    can i define 3D array as
    Code:
     array2 = (int ***)malloc(sizeof(int **) * iter);
      for (i = 0 ;  i < iter; i++) {
        array2[i] = (int **)malloc(sizeof(int *) * nodes);  
        for (j = 0; j < nodes; j++) {          
          (*(array2[i]))[j] = malloc(sizeof(int) * nodes);
           for (k = 0; k < nodes; k++)
          array2[i][j][k] = i * j * k;
        }
    But you did not change the next loop (k) and all your other loops in the same way. I may have not gotten the expression right because of the complicated way you are developing your data structure. You have not defined a 3 dimensional array in the normal sense. Please draw yourself a picture of what the above for loop (in its original form) does (one step at a time).

    Using the C array feature, you must have fixed size arrays (expect in C99 for variable length arrays which I have not carefully studied). You seem to need variable sized arrays.
    Are you using a compiler that claims compliance C99? If you are, you may be able to use the variable length array feature, but it has some significant restrictions. You will need someone else to help you who is familar with all those issues.

  9. #9
    C++ Beginner !!!
    Join Date
    Jul 2010
    Posts
    96
    I m not following here.....anymore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Passing an array to linked list
    By bar338 in forum C Programming
    Replies: 7
    Last Post: 04-08-2009, 09:15 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM