You are here:

C++/C++ Square Program


I'm trying to write code for the following square:


The user will input a single variable, n, to input a dimension.  

My code so far:

#include <iostream>
using namespace std;

int main() {
   int n = 0;
   int row, X, loop;
   while (n < 1 || n > 25)  {
         cout << "Enter a dimensional value (1-25):  ";
         cin >> n;

   for (row = 1; row <= n; row++)  {
       X = n - row + 1;
         for (loop = 1; loop <= X; loop++)
         cout << "X";
         cout << endl;  
   for (row = 1; row <= n; n--)  {
         for (loop = 1; loop <= X; loop++)
         cout << "X";
         cout << endl;  
   return 0;

I would greatly appreciate some help from an expert.  Please note that I am a newbie, and therefore, only know the basics such as loops statements and such.  Haven't even learned functions yet.  Thank you!

OK, it looks like you have it working through getting a dimensional value. Now to sort out your loops.

You can think of the square as rows and columns, so the first thing I would do to make it easier to program and understand is to rename one of your ints to "col" - you already have "row". For reasons that should become clear, you only need three variables: one to hold the user's input of the dimension size ("n"), one to count rows ("row"), and one to count columns (I would use "col").

Now, you can think of the outer FOR loop as keeping track of your rows (since you print one complete row at a time rather than one complete column at a time), and the inner FOR loop as keeping track of your columns. So for every cycle through the outer FOR loop, you want it to complete one row. You want it to cycle through this FOR loop n times, so your initial FOR clause is right on:

   for (row = 1; row <= n; row++)

Now, you can exploit the power of nested loops! Since this will cycle once for each row, all you need to do now is set up an inner FOR loop that will cycle once for each column.

       for (col = 1; col <= n; col++)

In your program above, you did put a FOR loop within your outer FOR loop, but you included an unnecessary variable (X), and you didn't enclose the body of the FOR loop in {}. Also, you have it printing an endline every time through, so you would get a vertical line of Xs rather than a square.

So for your code to run, and print a square of "X"s, you could change what you have by getting rid of the variable X and not including the endline command. I am using the variable "col" instead of "loop", for clarity, but it works the same:

       for (col = 1; col <= n; col++)
         //you don't need {} here if there is only one line, but - see below*
         cout << "X";

Remember this is the inner FOR loop within the outer FOR loop that accounts for rows.

So, with just that, you will print out a square of Xs. Notice you don't need a second outer FOR loop, as you have included.

Now, the only thing left to do is have it print an "O" in each of the right places instead of an "X". What are the right places? Coordinates [1,1], [2,2], [3,3] etc. What's the pattern? In each case, "row" == "col", right? So in the inner FOR loop, tell it that if row == col, print an "O", otherwise print an "X". That is most easily accomplished with an IF - ELSE conditional. *This is why you will need more than one line in your inner FOR loop, and therefore you need to put it all in {}.

So, an outline of your program might look like this:

- Print "Enter a dimensional value: "
- Get value in n
- (optional) Carriage return (endl) //formatting option
- For (each row):
-     For (each column)
-         if row == column, print an "O"
-         else, print an "X"
-     End inner FOR loop
-     Carriage return - must have this here, otherwise you don't get your square, just a line!
- End outer FOR loop
- (optional) Carriage return(s) //formatting
- pause and return 0

And you are done!

I hope that helps. If you need any more help with this, or have any other questions, please feel free to ask, and include your latest code.
Happy programming!  


All Answers

Answers by Expert:

Ask Experts


Oshi Jager


I can: - help debug code, or make it more efficient - help with concepts, key terms and commands, and object design/inheritance - give suggestions for subroutines or object calls - suggest overall organization techniques and/or data organization I have a fairly good concept of program flow, so if you have ideas, but don't know how to get started... I've had to do a lot of that, and I can help.


Primarily, 36 units in Computer Science at university. Also, I started programming games in C and C++ back when the Commodore 64 was the best game machine out there, and that makes me old... for those of you who don't know the reference. I have intermittently kept up with my programming, so try me out.

As of 2 years ago: Association for Computing Machinery (ACM)

Related classes at Cal State University: - Object Oriented Programming (3 units) [basically C/C++] - Database Design and Implementation (3) [all theory and SQL oriented] - Discrete Mathematics (3) [pure theory] - Computer Organization (3) [cpu functionality and programming] - Data Structures (3) [in C++, but also in general theory] - Computer Architecture (3) [the PC from top to bottom] - Java Programming (3) - Programming Languages (3) [history] - Telecommunications and Networking (3)

©2016 All rights reserved.