Hiya. I have this program that gives me alternating squares that switch from full squares to hollow. The problem I am having is that the squares that should be hollow are showing up with numbers in them like this. Anyone know what my problem is?

***
***
***
0000
1111
2222
3333
*****
*****
*****
*****
*****
000000
111111
222222
333333
444444

I'm trying to make it look like this.

***
***
***
****
*  *
*  *
****
*****
*****
*****
*****
*****
******
*    *
*    *
*    *
*    *
******
#include <iostream>
using namespace std;


			
void alternatingSquare(unsigned n);
void square(unsigned a);
void hollowSquare(unsigned a);


int main(){

	for (unsigned size = 3; size <= 6; ++size)
		alternatingSquare(size);

		cout << endl;


	system("pause");
	return 1;
}
	void  alternatingSquare(unsigned n){
				static bool solid;
				((solid=!solid) == true ? square : hollowSquare) (n);
		}
	void square(unsigned n){
		for(unsigned i = 0; i< n; ++i){
				for(unsigned j = 0; j< n; ++j)
						cout << '*';
						cout << endl; }
	}
	void hollowSquare(unsigned n){
		for (unsigned i = 0; i < n; ++i){
			for(unsigned j = 0; j < n; ++j)	  
				cout <<  i ==0 || i==n-2 || j==0 || j==n-1 ? ' ' : '1';
				  cout << endl;
		}
	}

Edited 6 Years Ago by bookmark: n/a

For starters, I would try rewriting it in a more conventional way, you'll probably understand it better. Think about the open square as a line at the top row and bottom row, and one square in the first and one in the last column for all other rows.

This way is not wrong, but one of the tenets of software design is providing the ability for someone else (or yourself!) to read and understand your code later on.

P.S. (and this is not directed at you by any means) I wish comp sci professors would come up with something original, these exercises are tired.

Edited 6 Years Ago by jonsca: n/a

The indentation is off and therefore hard to read/debug. Here's the program with better indentation on the loops:

#include <iostream>
using namespace std;


			
void alternatingSquare(unsigned n);
void square(unsigned a);
void hollowSquare(unsigned a);


int main(){

	for (unsigned size = 3; size <= 6; ++size)
		alternatingSquare(size);

	cout << endl;


	system("pause");
	return 1;
}

void  alternatingSquare(unsigned n){
	static bool solid;
				((solid=!solid) == true ? square : hollowSquare) (n);
}

void square(unsigned n){
	for(unsigned i = 0; i< n; ++i){
	        for(unsigned j = 0; j< n; ++j)
			cout << '*';
		cout << endl; }
}

void hollowSquare(unsigned n){
	for (unsigned i = 0; i < n; ++i){
		for(unsigned j = 0; j < n; ++j)	  
			cout <<  i ==0 || i==n-2 || j==0 || j==n-1 ? ' ' : '1';
		cout << endl;
	}
}

I'm guessing it's an operator precedence issue:

cout <<  i ==0 || i==n-2 || j==0 || j==n-1 ? ' ' : '1';

Clearly this is supposed to display a blank or a 1 (is the 1 supposed to be an asterisk?), so if you are getting 0, 1, 2, 3, it doesn't realize that it's supposed to display 0 or 1. Sticking some parentheses in there might help, but I'd break it into more than one line.

This article has been dead for over six months. Start a new discussion instead.