logoISU  

CS256 - Principles of Structured Design

Fall 2021

Displaying ./code/cs256su21code/jun03/square2.c

/*

	This is similar to square.c, execpt that this prints
	an outline of the square rather than a filled square


*/

#include <stdio.h>

int main()
{
	int i, j, size;

	printf("Size of Square: ");
	scanf("%d", &size);

	// i loop deals with rows, j loop deals with columns
	
	/*
		see square.c  for comments on nested for loops as 
		that will carry over here too
	*/
	for(i = 1; i <= size; i++){
		for(j = 1; j <= size; j++){
			/*
				Here we need a compound if statement so that we can tell the computer
				when to print a star, and when to print a space.

				This if statement makes use of a logical OR '||' to link the
				conditions together. Here, if any one of these conditions is 
				true, we print a star, else, we print a space.

				In this case, we only want to print stars if we're in the first or last
				row, or if we are in the first or last column.

				In this loop, first row/column (denoted by i and j respectively), is equal
				to 1 and the last row/column will be equal to what the user inputted in for
				size in the loop.

				So, if the user inputs 5, then the program will print a star if either i or j equal
				either 1 or 5. If none of these are true, we print a space.

			*/
			if(i == 1 || i == size || j == 1 || j == size)
			   printf("*");
			else
			   printf(" ");		
		}
		printf("\n");
	}

	/*

		REMINDER: a single equals sign '=' is an assignment operator, you use that to assign something 
		          to a variable.

				  a double equals sign '==' is a comparison operator, you use that to compare two values
				  to determine whether they equal each other or not.

				  a common error many programmers make is getting these two similar-looking operators
				  mixed-up.

	*/
	return 0;
}