Thread: beginning array question

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    I really don't understand what you want completely, but I get an figure now, please try the bottom program, and tell me what you want to do:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int myArr[3][3];
    	int i, j;
    
    	//initialise the array, all values to 0
    	for (i=0; i<3; i++)
    		for (j=0; j<3; j++)
    			myArr[i][j] = 0;
    
    	//loop and print the position
    		for (i=0; i<3; i++) {
    			for (j=0; j<3; j++)
    				printf("You are at myArr[%d][%d]!\n", i, j);
    			putchar('\n');
    		}
    	
    	return 0;
    }
    The output of this program in my machine:
    Code:
    You are at myArr[0][0]!
    You are at myArr[0][1]!
    You are at myArr[0][2]!
    
    You are at myArr[1][0]!
    You are at myArr[1][1]!
    You are at myArr[1][2]!
    
    You are at myArr[2][0]!
    You are at myArr[2][1]!
    You are at myArr[2][2]!
    
    Press any key to continue

  2. #17
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    Lets say I was trying to make a chessboard: the long code below will output the position of the chess pieces for the 8th rank, as I can set a8=1 and there is a pawn on a8; or h8=9 and there is a queen on h8; or e8=0 and nothing is on e8

    I was hoping to use an array[8][8] to do something like this...but in order to do that I would need to set array[4][3]=6; and then check to see whether array[4][3]=6; as 6 represents a chesspiece.

    But apparently Im not understanding something about arrays, I thought an array[8][8] would have 64 different slots to which I could assign 64 different values, but Im not sure how to set those values and then see what they are within an if statement.

    Code:
    void board()
    {
    if (a8==-1)
    {
    textcolor(BROWN);
    cprintf("            _");
    }
    if (a8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf("            n");
    }
    if (a8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf("            n");
    }
    if (a8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf("            r");
    }
    if (a8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf("            q");
    }
    if (a8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf("            k");
    }
    if (a8==10)
    {
    textcolor(LIGHTRED);
    cprintf("            K");
    }
    if (a8==13)
    {
    textcolor(LIGHTRED);
    cprintf("            N");
    }
    if (a8==14)
    {
    textcolor(LIGHTRED);
    cprintf("            B");
    }
    if (a8==16)
    {
    textcolor(LIGHTRED);
    cprintf("            R");
    }
    if (a8==19)
    {
    textcolor(LIGHTRED);
    cprintf("            Q");
    }
    if (b8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (b8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (b8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (b8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (b8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (b8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (b8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (b8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (b8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (b8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (b8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (c8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (c8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (c8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (c8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (c8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (c8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (c8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (c8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (c8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (c8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (c8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (d8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (d8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (d8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (d8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (d8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (d8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (d8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (d8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (d8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (d8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (d8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (e8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (e8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (e8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (e8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (e8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (e8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (e8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (e8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (e8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (e8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (e8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (f8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (f8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (f8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (f8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (f8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (f8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (f8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (f8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (f8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (f8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (f8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (g8==-1)
    {
    textcolor(BROWN);
    cprintf(" _");
    }
    if (g8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n");
    }
    if (g8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b");
    }
    if (g8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r");
    }
    if (g8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q");
    }
    if (g8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k");
    }
    if (g8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K");
    }
    if (g8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N");
    }
    if (g8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B");
    }
    if (g8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R");
    }
    if (g8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q");
    }
    if (h8==-1)
    {
    textcolor(BROWN);
    cprintf(" _\r\n");
    }
    if (h8==3)
    {
    textcolor(LIGHTBLUE);
    cprintf(" n\r\n");
    }
    if (h8==4)
    {
    textcolor(LIGHTBLUE);
    cprintf(" b\r\n");
    }
    if (h8==6)
    {
    textcolor(LIGHTBLUE);
    cprintf(" r\r\n");
    }
    if (h8==9)
    {
    textcolor(LIGHTBLUE);
    cprintf(" q\r\n");
    }
    if (h8==0)
    {
    textcolor(LIGHTBLUE);
    cprintf(" k\r\n");
    }
    if (h8==10)
    {
    textcolor(LIGHTRED);
    cprintf(" K\r\n");
    }
    if (h8==13)
    {
    textcolor(LIGHTRED);
    cprintf(" N\r\n");
    }
    if (h8==14)
    {
    textcolor(LIGHTRED);
    cprintf(" B\r\n");
    }
    if (h8==16)
    {
    textcolor(LIGHTRED);
    cprintf(" R\r\n");
    }
    if (h8==19)
    {
    textcolor(LIGHTRED);
    cprintf(" Q\r\n");
    }
    AIM: MarderIII

  3. #18
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    int maploc[3][3]={0};
    maploc[0][0]=1;

    I assume the above set element[0][0] to a value of 1. Now I want to write an if statement that says

    if (maploc[0][0]=1)
    AIM: MarderIII

  4. #19
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    I think I just answered my question!
    AIM: MarderIII

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Hah good luck with your program

  6. #21
    1.b4 e5 2.a3 d5 3.d4 exd
    Join Date
    Jan 2003
    Posts
    167
    This code outputs a 3x3 map, in which 2 areas have been marked for some reason or other, one area marked blue, and other is red.


    Code:
    void mapdisplay()
    {
    int x,y;
    int maploc[4][4]={0};
    maploc[0][2]=2;
    maploc[2][1]=1;
    
    for (x=0; x<4; x++)
    {
    y=3;
    maploc[x][y]=-1;
    }
    
    for (y=0; y<4; y++)
    {
    x=3;
    maploc[x][y]=-2;
    }
    
    y=0;
    for (x=0; x<4; x++)
    {
    for (y=0; y<4; y++)
    {
    if (maploc[x][y]==0)
    {
    cout<< " *";
    }
    if (maploc[x][y]==1)
    {
    textcolor(LIGHTBLUE);
    cprintf(" *");
    }
    if (maploc[x][y]==2)
    {
    textcolor(LIGHTRED);
    cprintf(" *");
    }
    if (maploc[x][y]==-1)
    {
    cout<<"\n";
    }
    if (maploc[x][y]==-2)
    {
    cout<<" ";
    }
    }
    }
    }
    AIM: MarderIII

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Your code looks like C++, you're using cout... hmm and don't forget you're in a C board, and not C++ board.

  8. #23
    Waltp
    Guest
    Let's assume you have the following 'map' which is 3 by 3

    map=
    2 4 9
    5 3 6
    1 7 8

    map[0][0] contains 2
    map[1][1] contains 3
    map[2][0] contains 1
    etc.

    map[0] is an array containing 2,4,9

    Therefore, when you use 2 index values (map[1][2]) you are looking at a specific value in the matrix, row 1, col 2 (value 6).

    Using one index specifies a full row.


    Another definition, map is 2 by 4:
    3 5 7 2
    4 1 8 6

    map[0][0] is 3
    map[0][3] is 2
    map[1][2] is 8
    and so on...

    Does that help?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM