Thread: 2D array-moving a varible about problem

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    38

    2D array-moving a varible about problem

    Hi,
    have iniatlised all elements in a array to zero except for [1][1],where its value is 1.I then try to move it up,down left and right.It seems to work for down,left and right but when you press up it goes to position[0][0] instead of [0][1]

    Code:
    #include<iostream>
    #include<string>
    #include<ctime>
    #include <stdlib.h>
    #include"test.h"
    using namespace std;
    void printArray( int theGrid[][3])
    {
    	cout<<theGrid[0][0]<<" "<<theGrid[0][1]<<" "<<theGrid[0][2]<<endl;
        cout<<theGrid[1][0]<<" "<<theGrid[1][1]<<" "<<theGrid[1][2]<<endl;
        cout<<theGrid[2][0]<<" "<<theGrid[2][1]<<" "<<theGrid[2][2]<<endl;
    }
    
    
    
    
    int main(int argc,char* argv[])
    { 
    
    	int theGrid[3][3]={  {0,0,0},
    	                     {0,1,0},
    	                     {0,0,0}};
    	printArray(theGrid);
    	char playerInput;
    	cin>>playerInput;
    
    	switch(playerInput){
    		case 'w':
    			for(int row=0;row<3;row++){
    				for(int col=0;col<3;col++){
    					if(theGrid[row][col]==1){
    						cout<<"test-where 1 is  at first- [1][1] "<<row<<" "<<col<<endl;
    						theGrid[row][col]=0;//changes where 1 was to 0
    						row-=1;
    						cout<<"test-where 1 is  at after being moved-[0][1] "<<row<<" "<<col<<endl;
    						theGrid[row][col]=1;//changes new position from 0 to 1
    					}}}
    	    case 'a':
    			for(int row=0;row<3;row++){
    				for(int col=0;col<3;col++){
    					if(theGrid[row][col]==1){
    						theGrid[row][col]=0;
    						col-=1;
    						theGrid[row][col]=1;
    					}}}
    			break;
    		case 'd':
    			for(int row=0;row<3;row++){
    				for(int col=0;col<3;col++){
    					if(theGrid[row][col]==1){
    						theGrid[row][col]=0;
    						col+=1;
    						theGrid[row][col]=1;
    					}}}
    			break;
    		case 's':
    			for (int row=0;row<3;row++){
    				for(int col=0;col<3;col++){
    					if(theGrid[row][col]==1){
    						theGrid[row][col]=0;
    						row+=1;
    						theGrid[row][col]=1;
    					}}}
    			break;
    		default:		
    			break;
    	}
    			printArray(theGrid);
    	
    	return 0;
    }
    the case for down('s')works.Its the same code as for up but 1 is added to row..but if i put col+=1 in to the case for up it puts the value 1 in right place....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are missing a break in the first case-statement.

    Code:
    			for(int row=0;row<3;row++){
    				for(int col=0;col<3;col++){
    					if(theGrid[row][col]==1){
    						theGrid[row][col]=0;
    						col-=1;
    						theGrid[row][col]=1;
    					}}}
    This bit of code is essentially repeated 4 times with very minor variations.

    Like in another thread about using switch statment, perhaps you'd want to re-arrange the code so that you simply set a delta value for row and col to -1 or 1 or 0 depending on the key pressed, and then do all the looping and calculating in a single set of for-loops after the switch-statement.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    38
    Thanks mats..I understand what your getting at.Will change to code too.the first case statement was been overwritten by the 2nd.Must of read the code 20 times and dint notice the missing break!
    Last edited by Cathalo; 03-16-2009 at 08:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 8 Queens, problem with searching 2D array
    By Sentral in forum C++ Programming
    Replies: 35
    Last Post: 03-11-2009, 04:12 PM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM