Thread: Rotating function for array

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

    Rotating function for array

    I am trying to make a rotating function for a 4X4 array:

    Here is what I have so far, but I need the array to be a bool functuion that displays some zeros, so far it is only displaying 1s, any ideas?

    Code:
    
    
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    #include "header.h"
    
    
    using namespace std;
    
    #define MAX_SIZE 4
    
    void main()
    {
    
    	bool box[MAX_SIZE][MAX_SIZE];
    	bool newbox [MAX_SIZE][MAX_SIZE];
    
    	int col;
    	int row;
    
    	for(int col =0; col < MAX_SIZE; col++)
    	{
    		cout << "Enter characters (4)" << col +1 << ":";
    		string s;
    		cin >> s;
    
    		/*if ( s.length() < MAX_SIZE)
    		{
    			for (int k = s.length(); k < MAX_SIZE; k++)
    				s = s + " ";
    		}*/
    
    		for(int row = 0; row < MAX_SIZE && row < s.length(); row++)
    		{
    			box[row][col] = s[row];
    		}
    	}
    
    	//print box
    	for(int col =0; col < MAX_SIZE; col++)
    	{
    		for (row = 0; row < MAX_SIZE; row ++)
    			cout << box[row][col] << " ";
    		cout << endl;
    	}
    	//rotate
    
    	for(int i=0; i<4; i++)
    		for(int j=0; j<4; j++)
    			newbox[3-j][ i ]=box[ i ][j];
    
    	/*for(int i = 0; i < MAX_SIZE; i++)
    	{ 
    		for(int j = 0; j < MAX_SIZE; j++)
    		{
    			newbox[i][j] = box [(MAX_SIZE -1)-j][i];
    		}
    	}*/
    
    	//copy to newbox
    
    	/*for(int col = 0; col < MAX_SIZE; col++)
    	{
    	for(int row = 0; row < MAX_SIZE; row++)
    	{
    	box [row][col] = newbox[row][col];
    	}
    	}*/
    	cout << "After rotation:" << endl;
    	//print newbox
    	for(int col =0; col < MAX_SIZE; col++)
    	{
    		for (row = 0; row < MAX_SIZE; row ++)
    			cout << newbox[row][col] << " ";
    		cout << endl;
    	}
    	
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    	bool box[MAX_SIZE][MAX_SIZE];
    	bool newbox [MAX_SIZE][MAX_SIZE];
    
    	int col;
    	int row;
    
    	for(int col =0; col < MAX_SIZE; col++)
    	{
    		cout << "Enter characters (4)" << col +1 << ":";
    		string s;
    		cin >> s;
    
    		/*if ( s.length() < MAX_SIZE)
    		{
    			for (int k = s.length(); k < MAX_SIZE; k++)
    				s = s + " ";
    		}*/
    
    		for(int row = 0; row < MAX_SIZE && row < s.length(); row++)
    		{
    			box[row][col] = s[row];
    		}
    	}
    You are trying to assign characters to a bool array. I'm guessing the string you enter is something like: "0110"? You probably want to convert the '0'/'1' characters into numeric 0/1 values before you do the assignment... so maybe:
    Code:
    box[row][col] = s[row]-'0';
    ... or something.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    The reason I wanted bool is just so I know which spots actually have info inside. SO, if there is a blank space, I would want it to display 0.

    Can anyone suggest a solution for that? as of now, my code doesn't handle a space input either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM