-1

need a bit of help here for a class exercise:

I've been tasked with writing C++ code to get a pyramid that, when run, will create a set pyramid of X's and 0's in specific spots. As of now, I have a pyramid of just X's, but cannot figure out how to get the 0's where I want them. Any help on this would be greatly appreciated.

This is what I'm supposed to get when run:

X
X X
X 0 X
X 0 0 X
X X X X X

This is what I have thus far:

#include <iostream>
using namespace std;

int main()
{
     int i, j;

     for (i = 0; i < 5; ++i)
     {
          for (j = 0; j <= i; ++j)
               cout << " X ";
          cout << endl;
     }
     return 0;
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Ashendis
  • 25
  • 1
  • 6

3 Answers3

2

Close, now you need to print O's somewhere.

The first row is easy, you just print an 'X'.

Then for every row after that (except the last row), (hint, loop required), you want to print an X and then O's until you get to the end where you wish to print another X. For this, think about how many O's are needed for each row (hint, another loop required).

Then last row is all X's and don't forget to add a newline at the end of each row.

If you need further help, feel free to comment.

Winestone
  • 1,500
  • 9
  • 19
  • Not sure if this answers the question or simply states what is obvious in OP's example. – ChiefTwoPencils Oct 13 '14 at 08:46
  • @ChiefTwoPencils Well I'm trying not to give him the code as it's a class excercise, I'll add some more detail in case he/she doesn't understand as well as you and I do. :) – Winestone Oct 13 '14 at 08:47
  • Almost got it, but still getting 0's in wrong spot... Do I need to alter my boundaries, or something like that? – Ashendis Oct 13 '14 at 09:45
1
#include <iostream>
//using namespace std; //Shouldn't really use this

int main()
{
 int i, j;
 int height = 5;
 for (i = 0; i < height; ++i)
 {
      for (j = 0; j <= i; ++j)
      { //Braces are great
           if (j == 0 || j == i || i == height - 1)   //If at either the 1st or last place in the row, or on the last row.
           {
               std::cout << " X ";
           }else                   //Otherwise
           {
               std::cout << " O ";
           }
      }
      std::cout << std::endl; //we could use "/n" here in place of std::endl
 }
 return 0;
}

Ok, a few things, you shouldn't really use using namespace std; because of good reasons that I can't explain as well as here, so have a read. Do't forget to put braces after things (your inner for loop). Yes, they're not technically needed, but they make it more readable, and it stops you messing up if you come to change it and forget to put them in.

After that, it's just finishing it off, and adding in a check to see which position that we're in so that we can decide to print an X or an O. As another thing, I've altered it so that the height is in a variable. This means that you'll only have to change it in one place if you want to change the height of the pyramid.

Community
  • 1
  • 1
Yann
  • 978
  • 2
  • 12
  • 31
  • don't forget ``std::endl`` ;) – Winestone Oct 13 '14 at 08:52
  • @Winestone Oops, crap, I always forget that :P Sorry for posting the code, I get the point of not just giving the answer, but I was reasoning that this is a Q&A, as opposed to Q&hint website ;) – Yann Oct 13 '14 at 08:54
  • Don't worry, no hard feelings XD – Winestone Oct 13 '14 at 08:56
  • Change ``i < 5`` to ``i < 4`` and do the last row separately? – Winestone Oct 13 '14 at 09:01
  • 1
    @ChiefTwoPencils I've fixed it, I hadn't run the code, and I forgot about the last line. – Yann Oct 13 '14 at 09:02
  • I figured I needed an "if/else" statement, I just wasn't sure what the wording would be like. But I put my iteration of the above, and I'm getting the 0's at the end still for the second and third rows. >.> – Ashendis Oct 13 '14 at 09:38
  • @Ashendis Uh, you shouldn't be, if you're running the code that I posted. Are you sure that you haven't made a mistake copying it? – Yann Oct 13 '14 at 09:40
  • ... Derp... I put j == 1 instead of j == i. Fixed it. This is what I get for doing assignments at 6 in the morning. XD Cheers. – Ashendis Oct 13 '14 at 09:49
0

You just need to print O's when 'j' is between 1 and i-1.

Hint: This check should appear in your 'j' loop.

johnkork
  • 659
  • 5
  • 19