Program to move box in a jar - what is wrong?

This is a discussion on Program to move box in a jar - what is wrong? within the C++ Programming forums, part of the General Programming Boards category; I am writing this program to move a box in a jar(a larger array). First, I overlap the box into ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Program to move box in a jar - what is wrong?

    I am writing this program to move a box in a jar(a larger array). First, I overlap the box into the array, then I print the array and attempt to move it. But for some reason, the moving part of the code only works when the box is set entirely to true and not for the set up I have now. Can anyone see what I am doing wrong? - the code compiles, so this should help visualizing what the code is about.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    #define MAX_HEIGHT 20
    #define MAX_WIDTH 10
    #define MAX_SIZE 4
    
    void main()
    {
    	int k =0;
    	int l =0;
    	int firstRow = 0; //these will come as parameters
    	int firstCol = 3;
    
    
    //------------------------For testing-------------------//
    	bool jar[MAX_HEIGHT][MAX_WIDTH];
    	for(int i = 0; i < MAX_HEIGHT; i++)
    	{
    		for(int j = 0; j < MAX_WIDTH; j++)
    		{
    			jar[i][j] = false;
    		}
    	} 
    
    	bool box[MAX_SIZE][MAX_SIZE];
    
    	for(int i = 0; i < MAX_SIZE; i++)
    		for(int j = 0; j < MAX_SIZE; j++)
    	{
    		box[i][j] = false;
    
    	} 
    		box[1][2] = true;
    		box[1][3] = true;
    		box[2][1] = true;
    		box[2][2] = true;
    
    	//print box
    	for(int row =0; row < MAX_SIZE; row++)
    	{
    		for (int col = 0; col < MAX_SIZE; col ++)
    			cout << box[row][col];
    		cout << endl;
    	}
    //-------------------------------------------------------------//
    		for(k = 0; k < MAX_SIZE; k++) //assigns the box to that location of the jar
    			for(l = 0; l < MAX_SIZE; l++)
    				for(int n = firstRow; n < firstRow + 4; n++)
    					for(int m = firstCol; m < firstCol + 4; m++)
    					{
    						if(jar [k][l+3] == false && box [k][l] == true)
    						{jar [k][l+3] = true;
    						}
    						if(jar [k][l+3] == true && box [k][l] == false)
    						{jar [k][l+3] = false;
    						}
    						
    					}
    //prints the original array for testing
    
    
    	for (int i = 0; i < MAX_HEIGHT; i++)
    		{
    			for(int j = 0; j < MAX_WIDTH; j++) 
    			{
    				if(jar[i][j] == true)
    				{
    					cout << "H";
    				}
    				else
    				{
    					cout << ".";
    				}
    			}
    			cout << endl;
    		}
    
    //Main function: After key is input, move array on jar.
    	char direction;
    	cin >> direction;
    
    	if(direction == 's')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y+1][x] = jar[y][x];
    			}
    			for(int z = firstCol; z < firstCol + 4; z++)
    				jar[firstRow][z] = false;
    	}
    
    	else if(direction == 'd')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y][x+1] = jar[y][x];
    			}
    			for(int z = firstRow; z < firstRow + 4; z++)
    				jar[z][firstCol] = false;
    
    	}
    	else if(direction == 'a')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y][x-1] = jar[y][x];
    			}
    			for(int z = firstRow; z < firstRow + 4; z++)
    				jar[z][firstCol + 3] = false;
    		
    	}
    
    //---------------Prints array again for testing----------------------//
    	for (int i = 0; i < MAX_HEIGHT; i++)
    		{
    			for(int j = 0; j < MAX_WIDTH; j++) 
    			{
    				if(jar[i][j] == true)
    				{
    					cout << "H";
    				}
    				else
    				{
    					cout << ".";
    				}
    			}
    			cout << endl;
    		}
    
    
    }
    Thank you.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    assigns the box to that location of the jar
    in this place you have 4 loop while using only 2

    jar[y+1][x] = jar[y][x];
    y+1 is overwritten, so on the next iteration of the loop it has not the value you expect. move box starting from the right column - not left

    and move down starting with the last row, not the first.

    Maybe start with the simple example - try to write memmove that works for overlaping regeons to see when you start copy from the first char and when - from the last. You will need to use the same principle
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    What you mean four loops, the last two are to delete the line on the top.

    I really don't see how starting from the bottom would matter. Can someone suggest a correction?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Fredir View Post
    What you mean four loops, the last two are to delete the line on the top.

    I really don't see how starting from the bottom would matter. Can someone suggest a correction?
    Yeah, when moving to the left or up start copy from lowest to highest index.
    When moving right or down copy from highest to lowest index.
    But that has been said before.
    Kurt

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    can anyone explain why that makes a difference?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, let's look at it then:
    Code:
    if(direction == 's')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y+1][x] = jar[y][x];
    			}
    			for(int z = firstCol; z < firstCol + 4; z++)
    				jar[firstRow][z] = false;
    	}
    I realize everything here is boolean, but let's look at integers. Suppose jar[y][x] = y, so jar[1][x] = 1, jar[4][x] = 4, and so on; and let's also suppose that firstRow = 1. What happens?

    y goes from 1 through 4; in each of our columns we first set jar[2][x] to jar[1][x], so jar[2][x] is now 1. Then we set jar[3][x] to jar[2][x], so jar[3][x] is now 1 (remember that we just changed jar[2][x]!); then jar[4][x] to jar[3][x] = 1, and jar[5][x] to jar[4][x] = 1.

    So instead of moving four rows down one each, we moved one row down four times. This is why, when we are moving data, we must loop against the direction of the move; otherwise, we will overwrite data that hasn't gotten moved yet.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Imagine you have an array with values {1, 2, 3, 4, 0}. Now suppose you wanted to move the first four values right. If you go copy values left to right then after the first iteration you'll have {1, 1, 3, 4, 0}, after second {1, 1, 1, 4, 0} and finally {1, 1, 1, 1, 1}. If you iterated from end to beginning then the iterations would result in:
    {1, 2, 3, 4, 4}
    {1, 2, 3, 3, 4}
    {1, 2, 2, 3, 4}
    {1, 1, 2, 3, 4}
    and you'll have to do something about the first element.
    Last edited by anon; 12-07-2007 at 04:41 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by Fredir View Post
    can anyone explain why that makes a difference?
    this is the simplest possible example that demonstrates why:
    Imagine you have a 1D array with 3 items;
    Code:
    int x[3] = {10, 20, 30};
    Now, you want to move the first two items over by one place, dropping whatever value was previously in the last spot, and filling the first spot with zero. i.e. it should end up with this:
    (0, 10, 20)
    This seems so simple that we don't even need a loop, we can simply unroll the loop in our head and would come up with this:
    Code:
    x[1] = x[0];
    x[2] = x[1];
    x[0] = 0;
    Okay, now try executing that:
    Code:
    x[1] = x[0];
    // The array now holds (10, 10, 30)
    x[2] = x[1];
    // The array now holds (10, 10, 10)
    x[0] = 0;
    // The array now holds (0, 10, 10)
    Oh no!!!

    Now you write the backwards copying case.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Thank you for the help everyone--I decremented the indexes on the down and right shift. Here is the updated code:

    Code:
    // Practice 2.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    #define MAX_HEIGHT 20
    #define MAX_WIDTH 10
    #define MAX_SIZE 4
    
    void main()
    {
    	int k =0;
    	int l =0;
    	int firstRow = 0; //these will come as parameters
    	int firstCol = 3;
    
    
    //------------------------For testing-------------------//
    	bool jar[MAX_HEIGHT][MAX_WIDTH];
    	for(int i = 0; i < MAX_HEIGHT; i++)
    	{
    		for(int j = 0; j < MAX_WIDTH; j++)
    		{
    			jar[i][j] = false;
    		}
    	} 
    		
    
    
    	bool box[MAX_SIZE][MAX_SIZE];
    
    	for(int i = 0; i < MAX_SIZE; i++)
    		for(int j = 0; j < MAX_SIZE; j++)
    	{
    		box[i][j] = false;
    
    	} 
    		box[1][2] = true;
    		box[1][3] = true;
    		box[2][1] = true;
    		box[2][2] = true;
    
    	//print box
    	for(int row =0; row < MAX_SIZE; row++)
    	{
    		for (int col = 0; col < MAX_SIZE; col ++)
    			cout << box[row][col];
    		cout << endl;
    	}
    //-------------------------------------------------------------//
    		for(k = 0; k < MAX_SIZE; k++) //assigns the box to that location of the jar
    			for(l = 0; l < MAX_SIZE; l++)
    				for(int n = firstRow; n < firstRow + 4; n++)
    					for(int m = firstCol; m < firstCol + 4; m++)
    					{
    						if(jar [k][l+3] == false && box [k][l] == true)
    						{jar [k][l+3] = true;
    						}
    						if(jar [k][l+3] == true && box [k][l] == false)
    						{jar [k][l+3] = true;
    						}
    						
    					}
    //prints the original array for testing
    
    
    	for (int i = 0; i < MAX_HEIGHT; i++)
    		{
    			for(int j = 0; j < MAX_WIDTH; j++) 
    			{
    				if(jar[i][j] == true)
    				{
    					cout << "H";
    				}
    				else
    				{
    					cout << ".";
    				}
    			}
    			cout << endl;
    		}
    
    //Main function: After key is input, move array on jar.
    	char direction;
    	cin >> direction;
    
    	if(direction == 's')
    	{
    		for(int y = firstRow + 3; y>=0; y--)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y+1][x] = jar[y][x];
    			}
    			for(int z = firstCol; z < firstCol + 4; z++)
    				jar[firstRow][z] = false;
    			//score++;
    	}
    
    	else if(direction == 'd')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol + 3; x >=0; x--)
    			{
    				jar[y][x+1] = jar[y][x];
    			}
    			for(int z = firstRow; z < firstRow + 4; z++)
    				jar[z][firstCol] = false;
    
    	}
    	else if(direction == 'a')
    	{
    		for(int y = firstRow; y < firstRow + 4; y++)
    			for(int x = firstCol; x < firstCol + 4; x++)
    			{
    				jar[y][x-1] = jar[y][x];
    			}
    			for(int z = firstRow; z < firstRow + 4; z++)
    				jar[z][firstCol + 3] = false;
    		
    	}
    
    //---------------Prints array again for testing----------------------//
    	for (int i = 0; i < MAX_HEIGHT; i++)
    		{
    			for(int j = 0; j < MAX_WIDTH; j++) 
    			{
    				if(jar[i][j] == true)
    				{
    					cout << "H";
    				}
    				else
    				{
    					cout << ".";
    				}
    			}
    			cout << endl;
    		}
    
    
    }



    --> Does anyone see anything wrong with it? (Especially with that 4 nested for loops to overlap the box and jar.) I admit I just guesses at some point and the loop stated working.

    Thanks.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Also, I added an new if thing for rotation but it doesn't seem to be working. I am kind of losing my mind over this right now. If anyone can shed some light on this statement as well, I would highly appreciate it.
    Code:
    else if (direction == 'w')
    	{
    		for(int i=0; i<4; i++)
    			for(int j=0; j<4; j++)
    				box[3-j][ i ]=box[ i ][j];
    
    			for(int row =0; row < MAX_SIZE; row++)
    	{
    		for (int col = 0; col < MAX_SIZE; col ++)
    			cout << box[row][col];
    		cout << endl;
    	}
    
    	}

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,454
    you still have 4 loop while using 2:
    Code:
    		for(k = 0; k < MAX_SIZE; k++) //assigns the box to that location of the jar
    			for(l = 0; l < MAX_SIZE; l++)
    				for(int n = firstRow; n < firstRow + 4; n++)
    					for(int m = firstCol; m < firstCol + 4; m++)
    					{
    						if(jar [k][l+3] == false && box [k][l] == true)
    						{jar [k][l+3] = true;
    						}
    						if(jar [k][l+3] == true && box [k][l] == false)
    						{jar [k][l+3] = true;
    						}
    						
    					}
    And do not tell me you are deleting something here. m and n are not used.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Ok, but regarding my last post about the rotation part, why is it not displaying anything at all on the jar array?

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Is this going to be a tetris-like game? I recently wrote one (code can be found in the game downloads) and I found it to be easier to manipulate the moving block as an array of coordinate-pairs in its own small bounding box and updating its relative position and checking collisions against the tetris field.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Yes, I am doing something similar.
    Last edited by Fredir; 12-08-2007 at 06:51 PM.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    Can anyone spot anything wrong with the 'w' (rotation) part???

    Thank you so much.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. Replies: 5
    Last Post: 01-31-2006, 12:54 AM
  4. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  5. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM

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