debug-multidimensional array

This is a discussion on debug-multidimensional array within the C++ Programming forums, part of the General Programming Boards category; I set out to see if I could code a tic tac toe game in less then an hour with ...

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    debug-multidimensional array

    I set out to see if I could code a tic tac toe game in less then an hour with a good AI. I failed XD
    This is the start of the program, and it messes up a little bit.

    Code:
    #include <iostream>
    
    using namespace std;
    
    void printarray (char arg[10][4])
    {
    	system("CLS");
    	for (int n2 = 0; n2 < 5; n2++)
    	{
    		for (int n = 0; n < 11; n++)
    			cout << arg[n][n2];
    		cout << "\n";
    	}
    }
    
    int main()
    {
    	int var;
    	char array[10][4];
    	// making the initial board...
    	array[0][0] = ' ';
    	array[1][0] = ' ';
    	array[2][0] = ' ';
    	array[3][0] = '*';
    	array[4][0] = ' ';
    	array[5][0] = ' ';
    	array[6][0] = ' ';
    	array[7][0] = '*';
    	array[8][0] = ' ';
    	array[9][0] = ' ';
    	array[10][0] = ' ';
    	var = 0;
    	while (var < 11)
    	{
    		array[var][1] = '*';
    		var++;
    	}
    	array[0][2] = ' ';
    	array[1][2] = ' ';
    	array[2][2] = ' ';
    	array[3][2] = '*';
    	array[4][2] = ' ';
    	array[5][2] = ' ';
    	array[6][2] = ' ';
    	array[7][2] = '*';
    	array[8][2] = ' ';
    	array[9][2] = ' ';
    	array[10][2] = ' ';
    	var = 0;
    	while (var < 11)
    	{
    		array[var][3] = '*';
    		var++;
    	}
    	array[0][4] = ' ';
    	array[1][4] = ' ';
    	array[2][4] = ' ';
    	array[3][4] = '*';
    	array[4][4] = ' ';
    	array[5][4] = ' ';
    	array[6][4] = ' ';
    	array[7][4] = '*';
    	array[8][4] = ' ';
    	array[9][4] = ' ';
    	array[10][4] = ' ';
    	//print board for the first time
    	printarray(array);
    	return 0;
    }
    The output should be
    (_ = a space)
    ___*___*___
    ***********
    ___*___*___
    ***********
    ___*___*___
    but instead the output is

    ____*___*__
    **********
    ___*___*___
    ***********
    ___*___*___

    Note: I am aware using system("CLS") is bad.'

    Can someone help?
    Thanks

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Since you are using indices from 0..10 and 0..4, the array should be dimensioned

    Code:
    char arg[11][5];
    ..
    ..
    char array[11][5];
    Regards,

    Dave
    Last edited by Dave Evans; 01-13-2005 at 07:40 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    you're accessing elements outside the bounds of your array.

    Code:
    void printarray (char arg[10][4])
    {
    	system("CLS");
    	for (int n2 = 0; n2 < 5; n2++) // 0 - 4 == 5 elements - you only have 4!
    	{
    	 for (int n = 0; n < 11; n++) // 0 - 10 == 11 elements - you only have 10!
    
    		cout << arg[n][n2];
    		cout << "\n";
    	}
    }

    [edit]
    beat me to it!
    [/edit]



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  2. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  3. Copy multidimensional array to single array?
    By seepox in forum C Programming
    Replies: 9
    Last Post: 05-08-2006, 11:19 AM
  4. Multidimensional Array in a Structure
    By Night_Blade in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 08:14 PM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM

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