nested for loops

This is a discussion on nested for loops within the C Programming forums, part of the General Programming Boards category; Hi, i'm trying to implement an algorithm using multidimensional arrays (matrices), structs and nested for loops. i would like to ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    nested for loops

    Hi, i'm trying to implement an algorithm using multidimensional arrays (matrices), structs and nested for loops. i would like to scan the matrix continously in order to manipulate the variables assigned to the struct members. I added an outer for loop to do this but it doesn't seem to work. any suggestions on how i might do this will be appreciated. portion of code is listed below:

    /* This program creates a multidimension matrix representing a
    2D array of nodes. Each array element represents a coordinate
    in the 2D space. values of array elements signify node IDs
    with the array elements being their coordinates.value's zero of the
    array elements signify no nodes - just space.
    The program attempts to implement an algorithm that scans the matrix
    to gather node positions(coordinates)
    */

    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>

    //define structure

    struct coord {
    int x;
    int y;
    int xfirsthopneighbour;
    int xsecondhopneighbour;
    int active;
    int anchornodestatus;
    };

    //declare and initialize array matrix

    int array[4][4] = {{0,0,0,2},{0,1,0,0},{0,5,3,0},{4,0,0,0}};

    // function prototype and global variable define

    int performaveraging(struct coord *t);
    int a,b;
    int i = 20;

    int main()
    {

    struct coord node[5];
    node[0].x = 0;
    node[0].active = 0;
    node[0].xfirsthopneighbour = 5;
    node[0].xsecondhopneighbour = 3;
    node[0].anchornodestatus = 0;
    node[1].x = 3;
    node[1].active = 1;
    node[1].xfirsthopneighbour = 1;
    node[1].xsecondhopneighbour = 3;
    node[1].anchornodestatus = 1;
    node[2].x = 0;
    node[2].active = 0;
    node[2].xfirsthopneighbour = 2;
    node[2].xsecondhopneighbour = 1;
    node[2].anchornodestatus = 0;
    node[3].x = 0;
    node[3].active = 1;
    node[3].xfirsthopneighbour = 5;
    node[3].xsecondhopneighbour = 1;
    node[3].anchornodestatus = 1;
    node[4].x = 0;
    node[4].active = 0;
    node[4].xfirsthopneighbour = 4;
    node[4].xsecondhopneighbour = 1;
    node[4].anchornodestatus = 0;

    for(i=0;i<20;i++) //this loop is my concern
    {


    for(a=0;a<4;a++)
    {
    for(b=0;b<4;b++)
    {
    if((array[a][b]) != 0)
    {


    // if neighbour is anchor or active acquire and store its coordinates

    if(node[(node[array[a][b]-1].xfirsthopneighbour)-1].anchornodestatus == 1 ||
    node[(node[array[a][b]-1].xfirsthopneighbour)-1].active == 1 &&
    node[array[a][b]-1].active == 0)
    {
    node[array[a][b]-1].x = node[(node[array[a][b]-1].xfirsthopneighbour)-1].x;

    node[array[a][b]-1].active = 1;

    }

    }

    }
    return 0;
    }
    return 0;
    }

  2. #2
    Insomniac
    Join Date
    Mar 2004
    Posts
    35
    Code:
      
      	    for(a=0;a<4;a++)
      	    {
      		      for(b=0;b<4;b++)
      		      {
      			       if((array[a][b]) != 0)
      			       {
      								
      
                    // if neighbour is anchor or active acquire and store its coordinates
      				
      				       if(node[(node[array[a][b]-1].xfirsthopneighbour)-1].anchornodestatus == 1 || 
      				    node[(node[array[a][b]-1].xfirsthopneighbour)-1].active == 1 &&
      					node[array[a][b]-1].active == 0)
      				{
      					node[array[a][b]-1].x = node[(node[array[a][b]-1].xfirsthopneighbour)-1].x;
      					
      					node[array[a][b]-1].active = 1;
      
      				}
      				
      		}
      	
      	}
      	return 0;
    I believe this return will return control to the calling function. As the function is main, control is moved back to the OS.

    Just remove it and all should work.
    i386 FreeBSD -- gcc-3.2.1 -- gdb-5.3

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    thanx

    it works now, thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. nested switch issue
    By fsu_altek in forum C Programming
    Replies: 3
    Last Post: 02-15-2006, 09:29 PM
  4. Displaying Data from a Nested Structure
    By shazg2000 in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2005, 09:16 AM
  5. Nested Classes
    By manofsteel972 in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2004, 10:57 AM

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