Thread: arrays

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    18

    arrays

    using
    Code:
    int ExecuteCommand(int com[], int max)
    {
    int Tfloor[FLOORSIZE][FLOORSIZE];
    
    int i, move, penup, pendown, n, s, e=1, w;
    for(i=0;i< max;i++)
    {
    switch (com[i]) {
     case 1:
          cout << "penup\n";
          penup=1;
     break;
    
     case 2:
          cout << "pendown\n";
          pendown =1;
     break;
    
     case 3:
          cout << "right\n";
    w=1;
     break;
    
     case 4:
          cout << "left\n";
     e=1;
    break;
    
     case 5:
    // case 5 .. uses
    //com[i + 1] to get the value of how many spaces to move..
    // assuming i know wat direction i wanna go..
    // i put an if statement.. and then how do i fill an array.. 
    // i have to convert pendown to 1s and pen up to 0..
    // do i make sense....
     Tfloor[FLOORSIZE][FLOORSIZE]=; // how do i fill an array..
          DisplayFloor(Tfloor, FLOORSIZE)
     break;
    }
    
    }
    }
    how do i convert the above to something like this and put it into
    int Tfloor[FLOORSIZE][FLOORSIZE] = {1, 1, 1, 1, 1,
    0, 0, 0, 0, 0};
    until the end of the loop..

    its similar to turtle graphics


    ta..

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    i dont quite understand. does tFloor[][] represent a geometric plan, a 2D space, with 2 dimensions? what is the purpose of this routine? you should be more specific.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    tFloor is a 2d array..
    when u combine the pen down and movement of the turtle.. for eg..

    6x5 array grid.. the ones represent the movement of the turtle.
    011110
    010010
    010010
    010010
    011110

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    i think im beginning to understand. you have a 2 dimensional area represented by a 2d array. you have a number of variables which tell you the current status of movement, drawing, etc.

    if im right, your routine reads in a list of commands from com[], and based on those commands records movement in the form of ones and zeros.

    assuming that's correct, my personal opinion would be to leave it in the command format, since that will tell you the complete set of movements, whereas your array wont, because it cant tell how many times the 'turtle' has moved onto a particular square.

    but if you're really fussed, ill give it a shot

    ill post it when im done
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    actually, i realized while making the program that i had no idea of what i was trying to make really. give us a hand here!
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    lol @ chizzy.

    Your not at Uni are you? Doing PP2 by any chance?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    fry - yes
    benny..
    umm..
    all im having trouble with is filling the array..
    lets say one command is for the turtle to move 15 spaces across..

    using an if statement i will be able to determine whther the pen is up or down..(whether a 1 or 0 is needed) the only trouble i have is filling the 2d array...

    thanks

  8. #8
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    You have to keep track of the x and y coordinates of the turtle.
    And you use these to change the array values.

    For example (this is simplified to be more general):
    Code:
    if(pen_down && need_to_move){
        Tfloor[current_x][current_y] = 1;
    }
    It really depends on how you have implemented the code so far. Another option would be to store in the current x and y, as well as the destination. Then you use a loop such as this:


    Code:
    for(int offset=0; offset < spaces_to_move; offset++){
        if(move_left)
            Tfloor[current_x - offset][current_y] = 1;
        if(move_right)
            Tfloor[current_x + offset][current_y] = 1;
        if(move_up)
            Tfloor[current_x][current_y - offset] = 1;
        if(move_down)
            Tfloor[current_x][current_y + space] = 1;
    }
    There are several other ways to do this as well, i have just shown two, and you should be able to see how it works. Not too difficult. (Btw, i haven't actually started the assignment yet, but feel free to send a PM if you have any more questions )
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    18
    thanx. im goin try that out ...

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. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM